50 return (int8_t)fmin(fmax((value), SCHAR_MIN), SCHAR_MAX);
55 return (
uint8_t)fmin(fmax((value), 0), UCHAR_MAX);
60 return (int16_t)fmin(fmax((value), SHRT_MIN), SHRT_MAX);
65 return (uint16_t)fmin(fmax((value), 0), USHRT_MAX);
70 return (int32_t)fmin(fmax((value), INT_MIN), INT_MAX);
75 return (
uint32_t)fmin(fmax((value), 0), UINT_MAX);
80 return (
float)fmin(fmax((value), -FLT_MAX), FLT_MAX);
92 assert(algname != NULL && strlen(algname) > 0);
94 if (strcmp(algname,
"NEARESTNEIGHBOUR") == 0)
95 return GRA_NearestNeighbour;
96 else if (strcmp(algname,
"NEARESTNEIGHBOR") == 0)
97 return GRA_NearestNeighbour;
98 else if (strcmp(algname,
"BILINEAR") == 0)
100 else if (strcmp(algname,
"CUBICSPLINE") == 0)
101 return GRA_CubicSpline;
102 else if (strcmp(algname,
"CUBIC") == 0)
104 else if (strcmp(algname,
"LANCZOS") == 0)
107 return GRA_NearestNeighbour;
181 if (NULL == request || !strlen(request))
182 return GDALVersionInfo(
"RELEASE_NAME");
184 return GDALVersionInfo(request);
192 assert(name != NULL && strlen(name) > 0);
194 if (strcmp(name,
"UNION") == 0)
196 else if (strcmp(name,
"FIRST") == 0)
198 else if (strcmp(name,
"SECOND") == 0)
200 else if (strcmp(name,
"LAST") == 0)
202 else if (strcmp(name,
"CUSTOM") == 0)
213 OGRSpatialReferenceH hsrs;
218 hsrs = OSRNewSpatialReference(NULL);
219 if (OSRSetFromUserInput(hsrs, srs) == OGRERR_NONE) {
221 OSRExportToProj4(hsrs, &rtn);
223 OSRExportToWkt(hsrs, &rtn);
226 rterror(
"rt_util_gdal_convert_sr: Could not process the provided srs: %s", srs);
230 OSRDestroySpatialReference(hsrs);
232 rterror(
"rt_util_gdal_convert_sr: Could not process the provided srs: %s", srs);
244 OGRSpatialReferenceH hsrs;
245 OGRErr rtn = OGRERR_NONE;
249 hsrs = OSRNewSpatialReference(NULL);
250 rtn = OSRSetFromUserInput(hsrs, srs);
251 OSRDestroySpatialReference(hsrs);
253 if (rtn == OGRERR_NONE)
271 const char *srs = NULL;
273 assert(authname != NULL);
274 assert(authcode != NULL);
279 srs = GDALGetProjectionRef(hds);
280 if (srs != NULL && srs[0] !=
'\0') {
281 OGRSpatialReferenceH hSRS = OSRNewSpatialReference(NULL);
283 if (OSRSetFromUserInput(hSRS, srs) == OGRERR_NONE) {
284 const char* pszAuthorityName = OSRGetAuthorityName(hSRS, NULL);
285 const char* pszAuthorityCode = OSRGetAuthorityCode(hSRS, NULL);
287 if (pszAuthorityName != NULL && pszAuthorityCode != NULL) {
288 *authname =
rtalloc(
sizeof(
char) * (strlen(pszAuthorityName) + 1));
289 *authcode =
rtalloc(
sizeof(
char) * (strlen(pszAuthorityCode) + 1));
291 if (*authname == NULL || *authcode == NULL) {
292 rterror(
"rt_util_gdal_sr_auth_info: Could not allocate memory for auth name and code");
293 if (*authname != NULL)
rtdealloc(*authname);
294 if (*authcode != NULL)
rtdealloc(*authcode);
295 OSRDestroySpatialReference(hSRS);
299 strncpy(*authname, pszAuthorityName, strlen(pszAuthorityName) + 1);
300 strncpy(*authcode, pszAuthorityCode, strlen(pszAuthorityCode) + 1);
304 OSRDestroySpatialReference(hSRS);
335 static int registered = 0;
337 if (registered && !force_register_all) {
338 RASTER_DEBUG(3,
"Already called once... not calling GDALAllRegister");
354 int count = GDALGetDriverCount();
356 GDALDriverH hdrv = NULL;
358 if (drv == NULL || !strlen(drv) || count < 1)
361 for (i = 0; i <
count; i++) {
362 hdrv = GDALGetDriver(i);
363 if (hdrv == NULL)
continue;
365 if (strcmp(drv, GDALGetDriverShortName(hdrv)) == 0)
384 rterror(
"rt_util_gdal_open: Cannot open file. All GDAL drivers disabled");
391 (strstr(fn,
"/vsicurl") != NULL) &&
394 rterror(
"rt_util_gdal_open: Cannot open VSICURL file. VSICURL disabled");
400 return GDALOpenShared(fn, fn_access);
402 return GDALOpen(fn, fn_access);
412 ext->
MinX = env.MinX;
413 ext->
MaxX = env.MaxX;
414 ext->
MinY = env.MinY;
415 ext->
MaxY = env.MaxY;
428 env->MinX = ext.
MinX;
429 env->MaxX = ext.
MaxX;
430 env->MinY = ext.
MinY;
431 env->MaxY = ext.
MaxY;
445 rterror(
"rt_util_envelope_to_lwpoly: Out of memory building envelope's geometry");
450 rterror(
"rt_util_envelope_to_lwpoly: Out of memory building envelope's geometry ring");
479 rterror(
"rt_util_envelope_to_lwpoly: Could not build envelope's geometry");
490 if (gt1 == NULL || gt2 == NULL)
493 for (k = 0; k < 6; k++) {
517 for (i = 1; i < 3; i++) {
534 rc = (maxc - rgb[0]) / diff;
535 gc = (maxc - rgb[1]) / diff;
536 bc = (maxc - rgb[2]) / diff;
540 else if (
DBL_EQ(rgb[1], maxc))
545 h = modf((h / 6.0), &junk);
574 i = floor(hsv[0] * 6.);
575 f = (hsv[0] * 6.0) - i;
576 p = v * (1. - hsv[1]);
577 q = v * (1. - hsv[1] * f);
578 t = v * (1. - hsv[1] * (1. - f));
627 int32_t checkvalint,
uint32_t checkvaluint,
628 float checkvalfloat,
double checkvaldouble,
642 if (fabs(checkvalint - initialvalue) >= 1) {
643 #if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0 644 rtwarn(
"Value set for %s band got clamped from %f to %d",
646 initialvalue, checkvalint
651 else if (
FLT_NEQ(checkvalint, initialvalue)) {
652 #if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0 653 rtwarn(
"Value set for %s band got truncated from %f to %d",
655 initialvalue, checkvalint
663 if (fabs(checkvaluint - initialvalue) >= 1) {
664 #if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0 665 rtwarn(
"Value set for %s band got clamped from %f to %u",
667 initialvalue, checkvaluint
672 else if (
FLT_NEQ(checkvaluint, initialvalue)) {
673 #if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0 674 rtwarn(
"Value set for %s band got truncated from %f to %u",
676 initialvalue, checkvaluint
688 if (
FLT_NEQ(checkvalfloat, initialvalue)) {
689 #if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0 690 rtwarn(
"Value set for %s band got converted from %f to %f",
692 initialvalue, checkvalfloat
700 if (
FLT_NEQ(checkvaldouble, initialvalue)) {
701 #if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0 702 rtwarn(
"Value set for %s band got converted from %f to %f",
704 initialvalue, checkvaldouble
void ptarray_set_point4d(POINTARRAY *pa, int n, const POINT4D *p4d)
int rt_util_gdal_driver_registered(const char *drv)
rt_extenttype rt_util_extent_type(const char *name)
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 POSTGIS_RT_2BUIMAX
void rt_util_from_ogr_envelope(OGREnvelope env, rt_envelope *ext)
int rt_util_gdal_supported_sr(const char *srs)
void rterror(const char *fmt,...)
Wrappers used for reporting errors and info.
void * rtalloc(size_t size)
Wrappers used for managing memory.
rt_errorstate
Enum definitions.
uint8_t rt_util_clamp_to_1BB(double value)
const char * rt_util_gdal_version(const char *request)
#define POSTGIS_RT_4BUIMAX
GDALDatasetH rt_util_gdal_open(const char *fn, GDALAccess fn_access, int shared)
void rt_util_to_ogr_envelope(rt_envelope ext, OGREnvelope *env)
int rt_util_same_geotransform_matrix(double *gt1, double *gt2)
void rtwarn(const char *fmt,...)
LWPOLY * lwpoly_construct(int srid, GBOX *bbox, uint32_t nrings, POINTARRAY **points)
uint16_t rt_util_clamp_to_16BUI(double value)
float rt_util_clamp_to_32F(double value)
int32_t rt_util_clamp_to_32BSI(double value)
rt_pixtype rt_util_gdal_datatype_to_pixtype(GDALDataType gdt)
Convert GDALDataType to rt_pixtype.
#define SRID_UNKNOWN
Unknown SRID value.
GDALDataType rt_util_pixtype_to_gdal_datatype(rt_pixtype pt)
Convert rt_pixtype to GDALDataType.
GDALResampleAlg rt_util_gdal_resample_alg(const char *algname)
Convert cstring name to GDAL Resample Algorithm.
char * rt_util_gdal_convert_sr(const char *srs, int proj4)
char * gdal_enabled_drivers
rt_errorstate rt_util_rgb_to_hsv(double rgb[3], double hsv[3])
#define POSTGIS_RT_1BBMAX
int rt_util_gdal_configured(void)
uint8_t rt_util_clamp_to_2BUI(double value)
rt_errorstate rt_util_hsv_to_rgb(double hsv[3], double rgb[3])
void rtdealloc(void *mem)
#define RASTER_DEBUG(level, msg)
This library is the generic raster handling section of PostGIS.
const char * rt_pixtype_name(rt_pixtype pixtype)
int16_t rt_util_clamp_to_16BSI(double value)
uint8_t rt_util_clamp_to_8BUI(double value)
int rt_util_gdal_register_all(int force_register_all)
LWPOLY * rt_util_envelope_to_lwpoly(rt_envelope env)
int rt_util_dbl_trunc_warning(double initialvalue, int32_t checkvalint, uint32_t checkvaluint, float checkvalfloat, double checkvaldouble, rt_pixtype pixtype)
uint8_t rt_util_clamp_to_4BUI(double value)
int8_t rt_util_clamp_to_8BSI(double value)
rt_errorstate rt_util_gdal_sr_auth_info(GDALDatasetH hds, char **authname, char **authcode)
Get auth name and code.
uint32_t rt_util_clamp_to_32BUI(double value)