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;
187 if (NULL == request || !strlen(request))
188 return GDALVersionInfo(
"RELEASE_NAME");
190 return GDALVersionInfo(request);
198 assert(name != NULL && strlen(name) > 0);
200 if (strcmp(name,
"UNION") == 0)
202 else if (strcmp(name,
"FIRST") == 0)
204 else if (strcmp(name,
"SECOND") == 0)
206 else if (strcmp(name,
"LAST") == 0)
208 else if (strcmp(name,
"CUSTOM") == 0)
219 OGRSpatialReferenceH hsrs;
224 hsrs = OSRNewSpatialReference(NULL);
225 if (OSRSetFromUserInput(hsrs, srs) == OGRERR_NONE) {
227 OSRExportToProj4(hsrs, &rtn);
229 OSRExportToWkt(hsrs, &rtn);
232 rterror(
"rt_util_gdal_convert_sr: Could not process the provided srs: %s", srs);
236 OSRDestroySpatialReference(hsrs);
238 rterror(
"rt_util_gdal_convert_sr: Could not process the provided srs: %s", srs);
250 OGRSpatialReferenceH hsrs;
251 OGRErr rtn = OGRERR_NONE;
255 hsrs = OSRNewSpatialReference(NULL);
256 rtn = OSRSetFromUserInput(hsrs, srs);
257 OSRDestroySpatialReference(hsrs);
259 if (rtn == OGRERR_NONE)
277 const char *srs = NULL;
279 assert(authname != NULL);
280 assert(authcode != NULL);
285 srs = GDALGetProjectionRef(hds);
286 if (srs != NULL && srs[0] !=
'\0') {
287 OGRSpatialReferenceH hSRS = OSRNewSpatialReference(NULL);
289 if (OSRSetFromUserInput(hSRS, srs) == OGRERR_NONE) {
290 const char* pszAuthorityName = OSRGetAuthorityName(hSRS, NULL);
291 const char* pszAuthorityCode = OSRGetAuthorityCode(hSRS, NULL);
293 if (pszAuthorityName != NULL && pszAuthorityCode != NULL) {
294 size_t authorityName_len = strlen(pszAuthorityName) +1;
295 size_t authorityCode_len = strlen(pszAuthorityCode) + 1;
296 *authname =
rtalloc(
sizeof(
char) * authorityName_len);
297 *authcode =
rtalloc(
sizeof(
char) * authorityCode_len);
299 if (*authname == NULL || *authcode == NULL) {
300 rterror(
"rt_util_gdal_sr_auth_info: Could not allocate memory for auth name and code");
301 if (*authname != NULL)
rtdealloc(*authname);
302 if (*authcode != NULL)
rtdealloc(*authcode);
303 OSRDestroySpatialReference(hSRS);
307 strncpy(*authname, pszAuthorityName, authorityName_len);
308 strncpy(*authcode, pszAuthorityCode, authorityCode_len);
312 OSRDestroySpatialReference(hSRS);
343 static int registered = 0;
345 if (registered && !force_register_all) {
346 RASTER_DEBUG(3,
"Already called once... not calling GDALAllRegister");
362 int count = GDALGetDriverCount();
364 GDALDriverH hdrv = NULL;
366 if (drv == NULL || !strlen(drv) ||
count < 1)
369 for (i = 0; i <
count; i++) {
370 hdrv = GDALGetDriver(i);
371 if (hdrv == NULL)
continue;
373 if (strcmp(drv, GDALGetDriverShortName(hdrv)) == 0)
391 GDALAccess fn_access,
394 char *vsi_options_str =
rtoptions(
"gdal_vsi_options");
397 if (vsi_options_str && strlen(vsi_options_str) > 0) {
400 rtinfo(
"postgis.gdal_vsi_options is set");
401 memset(olist, 0,
sizeof(olist));
406 for (i = 0; i < sz; i += 2)
408 char *key = olist[i];
409 char *val = olist[i+1];
412 if (strcmp(key,
"gdal_skip") == 0) {
413 rtwarn(
"Unable to set GDAL_SKIP config option");
416 rtinfo(
"CPLSetConfigOption(%s)", key);
417 CPLSetConfigOption(key, val);
422 unsigned int open_flags;
427 rterror(
"rt_util_gdal_open: Cannot open file. All GDAL drivers disabled");
434 (strstr(fn,
"/vsi") != NULL) &&
435 (strstr(fn,
"/vsimem") == NULL) &&
443 open_flags = GDAL_OF_RASTER
444 | GDAL_OF_VERBOSE_ERROR
445 | (fn_access == GA_Update ? GDAL_OF_UPDATE : 0)
446 | (shared ? GDAL_OF_SHARED : 0);
448 return GDALOpenEx(fn ,
463 ext->
MinX = env.MinX;
464 ext->
MaxX = env.MaxX;
465 ext->
MinY = env.MinY;
466 ext->
MaxY = env.MaxY;
479 env->MinX = ext.
MinX;
480 env->MaxX = ext.
MaxX;
481 env->MinY = ext.
MinY;
482 env->MaxY = ext.
MaxY;
496 rterror(
"rt_util_envelope_to_lwpoly: Out of memory building envelope's geometry");
501 rterror(
"rt_util_envelope_to_lwpoly: Out of memory building envelope's geometry ring");
530 rterror(
"rt_util_envelope_to_lwpoly: Could not build envelope's geometry");
541 if (gt1 == NULL || gt2 == NULL)
544 for (k = 0; k < 6; k++) {
568 for (i = 1; i < 3; i++) {
585 rc = (maxc - rgb[0]) / diff;
586 gc = (maxc - rgb[1]) / diff;
587 bc = (maxc - rgb[2]) / diff;
591 else if (
DBL_EQ(rgb[1], maxc))
596 h = modf((h / 6.0), &junk);
625 i = floor(hsv[0] * 6.);
626 f = (hsv[0] * 6.0) - i;
627 p = v * (1. - hsv[1]);
628 q = v * (1. - hsv[1] * f);
629 t = v * (1. - hsv[1] * (1. - f));
678 int32_t checkvalint, uint32_t checkvaluint,
679 float checkvalfloat,
double checkvaldouble,
693 if (fabs(checkvalint - initialvalue) >= 1) {
694 #if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0
695 rtwarn(
"Value set for %s band got clamped from %f to %d",
697 initialvalue, checkvalint
702 else if (checkvalint != initialvalue)
704 #if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0
705 rtwarn(
"Value set for %s band got truncated from %f to %d",
707 initialvalue, checkvalint
715 if (fabs(checkvaluint - initialvalue) >= 1) {
716 #if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0
717 rtwarn(
"Value set for %s band got clamped from %f to %u",
719 initialvalue, checkvaluint
724 else if (checkvaluint != initialvalue)
726 #if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0
727 rtwarn(
"Value set for %s band got truncated from %f to %u",
729 initialvalue, checkvaluint
741 if (
FLT_NEQ(checkvalfloat, initialvalue)) {
742 #if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0
743 rtwarn(
"Value set for %s band got converted from %f to %f",
745 initialvalue, checkvalfloat
753 if (
FLT_NEQ(checkvaldouble, initialvalue)) {
754 #if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0
755 rtwarn(
"Value set for %s band got converted from %f to %f",
757 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,...)
Wrappers used for reporting errors and info.
void * rtalloc(size_t size)
Wrappers used for managing memory.
#define RASTER_DEBUG(level, msg)
void rtinfo(const char *fmt,...)
#define POSTGIS_RT_4BUIMAX
char * rtoptions(const char *varname)
Wrappers used for options.
void rtwarn(const char *fmt,...)
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)