50 return (int8_t)fmin(fmax((
value), SCHAR_MIN), SCHAR_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);
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 size_t authorityName_len = strlen(pszAuthorityName) +1;
289 size_t authorityCode_len = strlen(pszAuthorityCode) + 1;
290 *authname =
rtalloc(
sizeof(
char) * authorityName_len);
291 *authcode =
rtalloc(
sizeof(
char) * authorityCode_len);
293 if (*authname == NULL || *authcode == NULL) {
294 rterror(
"rt_util_gdal_sr_auth_info: Could not allocate memory for auth name and code");
295 if (*authname != NULL)
rtdealloc(*authname);
296 if (*authcode != NULL)
rtdealloc(*authcode);
297 OSRDestroySpatialReference(hSRS);
301 strncpy(*authname, pszAuthorityName, authorityName_len);
302 strncpy(*authcode, pszAuthorityCode, authorityCode_len);
306 OSRDestroySpatialReference(hSRS);
337 static int registered = 0;
339 if (registered && !force_register_all) {
340 RASTER_DEBUG(3,
"Already called once... not calling GDALAllRegister");
356 int count = GDALGetDriverCount();
358 GDALDriverH hdrv = NULL;
360 if (drv == NULL || !strlen(drv) ||
count < 1)
363 for (i = 0; i <
count; i++) {
364 hdrv = GDALGetDriver(i);
365 if (hdrv == NULL)
continue;
367 if (strcmp(drv, GDALGetDriverShortName(hdrv)) == 0)
386 rterror(
"rt_util_gdal_open: Cannot open file. All GDAL drivers disabled");
393 (strstr(fn,
"/vsicurl") != NULL) &&
396 rterror(
"rt_util_gdal_open: Cannot open VSICURL file. VSICURL disabled");
402 return GDALOpenShared(fn, fn_access);
404 return GDALOpen(fn, fn_access);
414 ext->
MinX = env.MinX;
415 ext->
MaxX = env.MaxX;
416 ext->
MinY = env.MinY;
417 ext->
MaxY = env.MaxY;
430 env->MinX = ext.
MinX;
431 env->MaxX = ext.
MaxX;
432 env->MinY = ext.
MinY;
433 env->MaxY = ext.
MaxY;
447 rterror(
"rt_util_envelope_to_lwpoly: Out of memory building envelope's geometry");
452 rterror(
"rt_util_envelope_to_lwpoly: Out of memory building envelope's geometry ring");
481 rterror(
"rt_util_envelope_to_lwpoly: Could not build envelope's geometry");
492 if (gt1 == NULL || gt2 == NULL)
495 for (k = 0; k < 6; k++) {
519 for (i = 1; i < 3; i++) {
536 rc = (maxc - rgb[0]) / diff;
537 gc = (maxc - rgb[1]) / diff;
538 bc = (maxc - rgb[2]) / diff;
542 else if (
DBL_EQ(rgb[1], maxc))
547 h = modf((h / 6.0), &junk);
576 i = floor(hsv[0] * 6.);
577 f = (hsv[0] * 6.0) - i;
578 p = v * (1. - hsv[1]);
579 q = v * (1. - hsv[1] * f);
580 t = v * (1. - hsv[1] * (1. - f));
629 int32_t checkvalint,
uint32_t checkvaluint,
630 float checkvalfloat,
double checkvaldouble,
644 if (fabs(checkvalint - initialvalue) >= 1) {
645 #if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0
646 rtwarn(
"Value set for %s band got clamped from %f to %d",
648 initialvalue, checkvalint
653 else if (
FLT_NEQ(checkvalint, initialvalue)) {
654 #if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0
655 rtwarn(
"Value set for %s band got truncated from %f to %d",
657 initialvalue, checkvalint
665 if (fabs(checkvaluint - initialvalue) >= 1) {
666 #if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0
667 rtwarn(
"Value set for %s band got clamped from %f to %u",
669 initialvalue, checkvaluint
674 else if (
FLT_NEQ(checkvaluint, initialvalue)) {
675 #if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0
676 rtwarn(
"Value set for %s band got truncated from %f to %u",
678 initialvalue, checkvaluint
690 if (
FLT_NEQ(checkvalfloat, initialvalue)) {
691 #if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0
692 rtwarn(
"Value set for %s band got converted from %f to %f",
694 initialvalue, checkvalfloat
702 if (
FLT_NEQ(checkvaldouble, initialvalue)) {
703 #if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0
704 rtwarn(
"Value set for %s band got converted from %f to %f",
706 initialvalue, checkvaldouble
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...
LWPOLY * lwpoly_construct(int srid, GBOX *bbox, uint32_t nrings, POINTARRAY **points)
#define SRID_UNKNOWN
Unknown SRID value.
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)
#define POSTGIS_RT_4BUIMAX
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.
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)