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);
82 return (
float)fmin(fmax((
value), -FLT_MAX), FLT_MAX);
94 assert(algname != NULL && strlen(algname) > 0);
96 if (strcmp(algname,
"NEARESTNEIGHBOUR") == 0)
97 return GRA_NearestNeighbour;
98 else if (strcmp(algname,
"NEARESTNEIGHBOR") == 0)
99 return GRA_NearestNeighbour;
100 else if (strcmp(algname,
"BILINEAR") == 0)
102 else if (strcmp(algname,
"CUBICSPLINE") == 0)
103 return GRA_CubicSpline;
104 else if (strcmp(algname,
"CUBIC") == 0)
106 else if (strcmp(algname,
"LANCZOS") == 0)
109 return GRA_NearestNeighbour;
183 if (NULL == request || !strlen(request))
184 return GDALVersionInfo(
"RELEASE_NAME");
186 return GDALVersionInfo(request);
194 assert(name != NULL && strlen(name) > 0);
196 if (strcmp(name,
"UNION") == 0)
198 else if (strcmp(name,
"FIRST") == 0)
200 else if (strcmp(name,
"SECOND") == 0)
202 else if (strcmp(name,
"LAST") == 0)
204 else if (strcmp(name,
"CUSTOM") == 0)
215 OGRSpatialReferenceH hsrs;
220 hsrs = OSRNewSpatialReference(NULL);
221 if (OSRSetFromUserInput(hsrs, srs) == OGRERR_NONE) {
223 OSRExportToProj4(hsrs, &rtn);
225 OSRExportToWkt(hsrs, &rtn);
228 rterror(
"rt_util_gdal_convert_sr: Could not process the provided srs: %s", srs);
232 OSRDestroySpatialReference(hsrs);
234 rterror(
"rt_util_gdal_convert_sr: Could not process the provided srs: %s", srs);
246 OGRSpatialReferenceH hsrs;
247 OGRErr rtn = OGRERR_NONE;
251 hsrs = OSRNewSpatialReference(NULL);
252 rtn = OSRSetFromUserInput(hsrs, srs);
253 OSRDestroySpatialReference(hsrs);
255 if (rtn == OGRERR_NONE)
273 const char *srs = NULL;
275 assert(authname != NULL);
276 assert(authcode != NULL);
281 srs = GDALGetProjectionRef(hds);
282 if (srs != NULL && srs[0] !=
'\0') {
283 OGRSpatialReferenceH hSRS = OSRNewSpatialReference(NULL);
285 if (OSRSetFromUserInput(hSRS, srs) == OGRERR_NONE) {
286 const char* pszAuthorityName = OSRGetAuthorityName(hSRS, NULL);
287 const char* pszAuthorityCode = OSRGetAuthorityCode(hSRS, NULL);
289 if (pszAuthorityName != NULL && pszAuthorityCode != NULL) {
290 size_t authorityName_len = strlen(pszAuthorityName) +1;
291 size_t authorityCode_len = strlen(pszAuthorityCode) + 1;
292 *authname =
rtalloc(
sizeof(
char) * authorityName_len);
293 *authcode =
rtalloc(
sizeof(
char) * authorityCode_len);
295 if (*authname == NULL || *authcode == NULL) {
296 rterror(
"rt_util_gdal_sr_auth_info: Could not allocate memory for auth name and code");
297 if (*authname != NULL)
rtdealloc(*authname);
298 if (*authcode != NULL)
rtdealloc(*authcode);
299 OSRDestroySpatialReference(hSRS);
303 strncpy(*authname, pszAuthorityName, authorityName_len);
304 strncpy(*authcode, pszAuthorityCode, authorityCode_len);
308 OSRDestroySpatialReference(hSRS);
339 static int registered = 0;
341 if (registered && !force_register_all) {
342 RASTER_DEBUG(3,
"Already called once... not calling GDALAllRegister");
358 int count = GDALGetDriverCount();
360 GDALDriverH hdrv = NULL;
362 if (drv == NULL || !strlen(drv) ||
count < 1)
365 for (i = 0; i <
count; i++) {
366 hdrv = GDALGetDriver(i);
367 if (hdrv == NULL)
continue;
369 if (strcmp(drv, GDALGetDriverShortName(hdrv)) == 0)
388 rterror(
"rt_util_gdal_open: Cannot open file. All GDAL drivers disabled");
395 (strstr(fn,
"/vsicurl") != NULL) &&
398 rterror(
"rt_util_gdal_open: Cannot open VSICURL file. VSICURL disabled");
404 return GDALOpenShared(fn, fn_access);
406 return GDALOpen(fn, fn_access);
416 ext->
MinX = env.MinX;
417 ext->
MaxX = env.MaxX;
418 ext->
MinY = env.MinY;
419 ext->
MaxY = env.MaxY;
432 env->MinX = ext.
MinX;
433 env->MaxX = ext.
MaxX;
434 env->MinY = ext.
MinY;
435 env->MaxY = ext.
MaxY;
449 rterror(
"rt_util_envelope_to_lwpoly: Out of memory building envelope's geometry");
454 rterror(
"rt_util_envelope_to_lwpoly: Out of memory building envelope's geometry ring");
483 rterror(
"rt_util_envelope_to_lwpoly: Could not build envelope's geometry");
494 if (gt1 == NULL || gt2 == NULL)
497 for (k = 0; k < 6; k++) {
521 for (i = 1; i < 3; i++) {
538 rc = (maxc - rgb[0]) / diff;
539 gc = (maxc - rgb[1]) / diff;
540 bc = (maxc - rgb[2]) / diff;
544 else if (
DBL_EQ(rgb[1], maxc))
549 h = modf((h / 6.0), &junk);
578 i = floor(hsv[0] * 6.);
579 f = (hsv[0] * 6.0) - i;
580 p = v * (1. - hsv[1]);
581 q = v * (1. - hsv[1] * f);
582 t = v * (1. - hsv[1] * (1. - f));
631 int32_t checkvalint, uint32_t checkvaluint,
632 float checkvalfloat,
double checkvaldouble,
646 if (fabs(checkvalint - initialvalue) >= 1) {
647 #if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0
648 rtwarn(
"Value set for %s band got clamped from %f to %d",
650 initialvalue, checkvalint
655 else if (checkvalint != initialvalue)
657 #if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0
658 rtwarn(
"Value set for %s band got truncated from %f to %d",
660 initialvalue, checkvalint
668 if (fabs(checkvaluint - initialvalue) >= 1) {
669 #if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0
670 rtwarn(
"Value set for %s band got clamped from %f to %u",
672 initialvalue, checkvaluint
677 else if (checkvaluint != initialvalue)
679 #if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0
680 rtwarn(
"Value set for %s band got truncated from %f to %u",
682 initialvalue, checkvaluint
694 if (
FLT_NEQ(checkvalfloat, initialvalue)) {
695 #if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0
696 rtwarn(
"Value set for %s band got converted from %f to %f",
698 initialvalue, checkvalfloat
706 if (
FLT_NEQ(checkvaldouble, initialvalue)) {
707 #if POSTGIS_RASTER_WARN_ON_TRUNCATION > 0
708 rtwarn(
"Value set for %s band got converted from %f to %f",
710 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...
#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)
#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)