51 return (int8_t)fmin(fmax((
value), SCHAR_MIN), SCHAR_MAX);
56 return (uint8_t)fmin(fmax((
value), 0), UCHAR_MAX);
61 return (int16_t)fmin(fmax((
value), SHRT_MIN), SHRT_MAX);
66 return (uint16_t)fmin(fmax((
value), 0), USHRT_MAX);
71 return (int32_t)fmin(fmax((
value), INT_MIN), INT_MAX);
76 return (uint32_t)fmin(fmax((
value), 0), UINT_MAX);
83 return (
float)fmin(fmax((
value), -FLT_MAX), FLT_MAX);
95 assert(algname != NULL && strlen(algname) > 0);
97 if (strcmp(algname,
"NEARESTNEIGHBOUR") == 0)
98 return GRA_NearestNeighbour;
99 else if (strcmp(algname,
"NEARESTNEIGHBOR") == 0)
100 return GRA_NearestNeighbour;
101 else if (strcmp(algname,
"BILINEAR") == 0)
103 else if (strcmp(algname,
"CUBICSPLINE") == 0)
104 return GRA_CubicSpline;
105 else if (strcmp(algname,
"CUBIC") == 0)
107 else if (strcmp(algname,
"LANCZOS") == 0)
109 else if (strcmp(algname,
"MAX") == 0)
111 else if (strcmp(algname,
"MIN") == 0)
113 return GRA_NearestNeighbour;
132 #if POSTGIS_GDAL_VERSION >= 30700
166 #if POSTGIS_GDAL_VERSION >= 30700
194 if (NULL == request || !strlen(request))
195 return GDALVersionInfo(
"RELEASE_NAME");
197 return GDALVersionInfo(request);
205 assert(name != NULL && strlen(name) > 0);
207 if (strcmp(name,
"UNION") == 0)
209 else if (strcmp(name,
"FIRST") == 0)
211 else if (strcmp(name,
"SECOND") == 0)
213 else if (strcmp(name,
"LAST") == 0)
215 else if (strcmp(name,
"CUSTOM") == 0)
226 OGRSpatialReferenceH hsrs;
231 hsrs = OSRNewSpatialReference(NULL);
232 if (OSRSetFromUserInput(hsrs, srs) == OGRERR_NONE) {
234 OSRExportToProj4(hsrs, &rtn);
236 OSRExportToWkt(hsrs, &rtn);
239 rterror(
"rt_util_gdal_convert_sr: Could not process the provided srs: %s", srs);
243 OSRDestroySpatialReference(hsrs);
245 rterror(
"rt_util_gdal_convert_sr: Could not process the provided srs: %s", srs);
257 OGRSpatialReferenceH hsrs;
258 OGRErr rtn = OGRERR_NONE;
262 hsrs = OSRNewSpatialReference(NULL);
263 rtn = OSRSetFromUserInput(hsrs, srs);
264 OSRDestroySpatialReference(hsrs);
266 if (rtn == OGRERR_NONE)
284 const char *srs = NULL;
286 assert(authname != NULL);
287 assert(authcode != NULL);
292 srs = GDALGetProjectionRef(hds);
293 if (srs != NULL && srs[0] !=
'\0') {
294 OGRSpatialReferenceH hSRS = OSRNewSpatialReference(NULL);
296 if (OSRSetFromUserInput(hSRS, srs) == OGRERR_NONE) {
297 const char* pszAuthorityName = OSRGetAuthorityName(hSRS, NULL);
298 const char* pszAuthorityCode = OSRGetAuthorityCode(hSRS, NULL);
300 if (pszAuthorityName != NULL && pszAuthorityCode != NULL) {
301 size_t authorityName_len = strlen(pszAuthorityName) +1;
302 size_t authorityCode_len = strlen(pszAuthorityCode) + 1;
303 *authname =
rtalloc(
sizeof(
char) * authorityName_len);
304 *authcode =
rtalloc(
sizeof(
char) * authorityCode_len);
306 if (*authname == NULL || *authcode == NULL) {
307 rterror(
"rt_util_gdal_sr_auth_info: Could not allocate memory for auth name and code");
308 if (*authname != NULL)
rtdealloc(*authname);
309 if (*authcode != NULL)
rtdealloc(*authcode);
310 OSRDestroySpatialReference(hSRS);
314 strncpy(*authname, pszAuthorityName, authorityName_len);
315 strncpy(*authcode, pszAuthorityCode, authorityCode_len);
319 OSRDestroySpatialReference(hSRS);
348 static int registered = 0;
350 if (registered && !force_register_all) {
351 RASTER_DEBUG(3,
"Already called once... not calling GDALAllRegister");
367 int count = GDALGetDriverCount();
369 GDALDriverH hdrv = NULL;
371 if (drv == NULL || !strlen(drv) ||
count < 1)
374 for (i = 0; i <
count; i++) {
375 hdrv = GDALGetDriver(i);
376 if (hdrv == NULL)
continue;
378 if (strcmp(drv, GDALGetDriverShortName(hdrv)) == 0)
396 GDALAccess fn_access,
399 char *vsi_options_str =
rtoptions(
"gdal_vsi_options");
402 if (vsi_options_str && strlen(vsi_options_str) > 0) {
405 rtinfo(
"postgis.gdal_vsi_options is set");
406 memset(olist, 0,
sizeof(olist));
411 for (i = 0; i < sz; i += 2)
413 char *key = olist[i];
414 char *val = olist[i+1];
417 if (strcmp(key,
"gdal_skip") == 0) {
418 rtwarn(
"Unable to set GDAL_SKIP config option");
421 rtinfo(
"CPLSetConfigOption(%s)", key);
422 CPLSetConfigOption(key, val);
427 unsigned int open_flags;
432 rterror(
"rt_util_gdal_open: Cannot open file. All GDAL drivers disabled");
439 (strstr(fn,
"/vsi") != NULL) &&
440 (strstr(fn,
"/vsimem") == NULL) &&
448 open_flags = GDAL_OF_RASTER
449 | GDAL_OF_VERBOSE_ERROR
450 | (fn_access == GA_Update ? GDAL_OF_UPDATE : 0)
451 | (shared ? GDAL_OF_SHARED : 0);
453 return GDALOpenEx(fn ,
468 ext->
MinX = env.MinX;
469 ext->
MaxX = env.MaxX;
470 ext->
MinY = env.MinY;
471 ext->
MaxY = env.MaxY;
484 env->MinX = ext.
MinX;
485 env->MaxX = ext.
MaxX;
486 env->MinY = ext.
MinY;
487 env->MaxY = ext.
MaxY;
501 rterror(
"rt_util_envelope_to_lwpoly: Out of memory building envelope's geometry");
506 rterror(
"rt_util_envelope_to_lwpoly: Out of memory building envelope's geometry ring");
535 rterror(
"rt_util_envelope_to_lwpoly: Could not build envelope's geometry");
546 if (gt1 == NULL || gt2 == NULL)
549 for (k = 0; k < 6; k++) {
573 for (i = 1; i < 3; i++) {
590 rc = (maxc - rgb[0]) / diff;
591 gc = (maxc - rgb[1]) / diff;
592 bc = (maxc - rgb[2]) / diff;
596 else if (
DBL_EQ(rgb[1], maxc))
601 h = modf((h / 6.0), &junk);
630 i = floor(hsv[0] * 6.);
631 f = (hsv[0] * 6.0) - i;
632 p = v * (1. - hsv[1]);
633 q = v * (1. - hsv[1] * f);
634 t = v * (1. - hsv[1] * (1. - f));
683 int32_t checkvalint, uint32_t checkvaluint,
684 float checkvalfloat,
double checkvaldouble,
698 if (fabs(checkvalint - initialvalue) >= 1) {
699 #if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0
700 rtwarn(
"Value set for %s band got clamped from %f to %d",
702 initialvalue, checkvalint
707 else if (checkvalint != initialvalue)
709 #if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0
710 rtwarn(
"Value set for %s band got truncated from %f to %d",
712 initialvalue, checkvalint
720 if (fabs(checkvaluint - initialvalue) >= 1) {
721 #if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0
722 rtwarn(
"Value set for %s band got clamped from %f to %u",
724 initialvalue, checkvaluint
729 else if (checkvaluint != initialvalue)
731 #if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0
732 rtwarn(
"Value set for %s band got truncated from %f to %u",
734 initialvalue, checkvaluint
746 if (
FLT_NEQ(checkvalfloat, initialvalue)) {
747 #if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0
748 rtwarn(
"Value set for %s band got converted from %f to %f",
750 initialvalue, checkvalfloat
758 if (
FLT_NEQ(checkvaldouble, initialvalue)) {
759 #if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0
760 rtwarn(
"Value set for %s band got converted from %f to %f",
762 initialvalue, checkvaldouble
char result[OUT_DOUBLE_BUFFER_SIZE]
POINTARRAY * ptarray_construct(char hasz, char hasm, uint32_t npoints)
Construct an empty pointarray, allocating storage and setting the npoints, but not filling in any inf...
#define SRID_UNKNOWN
Unknown SRID value.
LWPOLY * lwpoly_construct(int32_t srid, GBOX *bbox, uint32_t nrings, POINTARRAY **points)
void ptarray_set_point4d(POINTARRAY *pa, uint32_t n, const POINT4D *p4d)
void rterror(const char *fmt,...) __attribute__((format(printf
Wrappers used for reporting errors and info.
void * rtalloc(size_t size)
Wrappers used for managing memory.
void void void char * rtoptions(const char *varname)
Wrappers used for options.
#define RASTER_DEBUG(level, msg)
void void rtinfo(const char *fmt,...) __attribute__((format(printf
void void void rtwarn(const char *fmt,...) __attribute__((format(printf
#define POSTGIS_RT_4BUIMAX
rt_errorstate
Enum definitions.
#define POSTGIS_RT_2BUIMAX
const char * rt_pixtype_name(rt_pixtype pixtype)
#define POSTGIS_RT_1BBMAX
void rtdealloc(void *mem)
This library is the generic raster handling section of PostGIS.
size_t option_list_length(char **olist)
Returns the total number of keys and values in the list.
void option_list_parse(char *input, char **olist)
option_list is a null-terminated list of strings, where every odd string is a key and every even stri...
rt_pixtype rt_util_gdal_datatype_to_pixtype(GDALDataType gdt)
Convert GDALDataType to rt_pixtype.
rt_errorstate rt_util_rgb_to_hsv(double rgb[3], double hsv[3])
int rt_util_gdal_register_all(int force_register_all)
int rt_util_gdal_configured(void)
int8_t rt_util_clamp_to_8BSI(double value)
uint8_t rt_util_clamp_to_1BB(double value)
int32_t rt_util_clamp_to_32BSI(double value)
char * rt_util_gdal_convert_sr(const char *srs, int proj4)
const char * rt_util_gdal_version(const char *request)
rt_extenttype rt_util_extent_type(const char *name)
int rt_util_dbl_trunc_warning(double initialvalue, int32_t checkvalint, uint32_t checkvaluint, float checkvalfloat, double checkvaldouble, rt_pixtype pixtype)
GDALDataType rt_util_pixtype_to_gdal_datatype(rt_pixtype pt)
Convert rt_pixtype to GDALDataType.
uint8_t rt_util_clamp_to_2BUI(double value)
uint8_t rt_util_clamp_to_8BUI(double value)
GDALDatasetH rt_util_gdal_open(const char *fn, GDALAccess fn_access, int shared)
int rt_util_gdal_supported_sr(const char *srs)
int16_t rt_util_clamp_to_16BSI(double value)
GDALResampleAlg rt_util_gdal_resample_alg(const char *algname)
Convert cstring name to GDAL Resample Algorithm.
int rt_util_gdal_driver_registered(const char *drv)
uint8_t rt_util_clamp_to_4BUI(double value)
rt_errorstate rt_util_hsv_to_rgb(double hsv[3], double rgb[3])
void rt_util_to_ogr_envelope(rt_envelope ext, OGREnvelope *env)
rt_errorstate rt_util_gdal_sr_auth_info(GDALDatasetH hds, char **authname, char **authcode)
Get auth name and code.
uint16_t rt_util_clamp_to_16BUI(double value)
uint32_t rt_util_clamp_to_32BUI(double value)
LWPOLY * rt_util_envelope_to_lwpoly(rt_envelope env)
char * gdal_enabled_drivers
int rt_util_same_geotransform_matrix(double *gt1, double *gt2)
float rt_util_clamp_to_32F(double value)
void rt_util_from_ogr_envelope(OGREnvelope env, rt_envelope *ext)