33 #include <utils/lsyscache.h>
34 #include <utils/array.h>
35 #include <catalog/pg_type.h>
36 #include <utils/builtins.h>
38 #include "../../postgis_config.h"
40 #include "access/htup_details.h"
41 #include "lwgeom_pg.h"
86 PG_FREE_IF_COPY(pgraster, 0);
87 elog(ERROR,
"RASTER_envelope: Could not deserialize raster");
94 PG_FREE_IF_COPY(pgraster, 0);
97 elog(ERROR,
"RASTER_envelope: Could not get raster's envelope");
100 else if (geom == NULL) {
101 elog(NOTICE,
"Raster's envelope is NULL");
108 SET_VARSIZE(gser, gser_size);
109 PG_RETURN_POINTER(gser);
128 bool minhull =
FALSE;
142 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
147 PG_FREE_IF_COPY(pgraster, 0);
148 elog(ERROR,
"RASTER_convex_hull: Could not deserialize raster");
158 if (!PG_ARGISNULL(1)) {
159 nband = PG_GETARG_INT32(1);
161 elog(NOTICE,
"Invalid band index (must use 1-based). Returning NULL");
163 PG_FREE_IF_COPY(pgraster, 0);
173 PG_FREE_IF_COPY(pgraster, 0);
176 elog(ERROR,
"RASTER_convex_hull: Could not get raster's convex hull");
179 else if (geom == NULL) {
180 elog(NOTICE,
"Raster's convex hull is NULL");
187 SET_VARSIZE(gser, gser_size);
188 PG_RETURN_POINTER(gser);
191 #define VALUES_LENGTH 2
195 FuncCallContext *funcctx;
203 if (SRF_IS_FIRSTCALL()) {
204 MemoryContext oldcontext;
209 bool exclude_nodata_value =
TRUE;
215 funcctx = SRF_FIRSTCALL_INIT();
218 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
221 if (PG_ARGISNULL(0)) {
222 MemoryContextSwitchTo(oldcontext);
223 SRF_RETURN_DONE(funcctx);
225 pgraster = (
rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
228 PG_FREE_IF_COPY(pgraster, 0);
230 errcode(ERRCODE_OUT_OF_MEMORY),
231 errmsg(
"Could not deserialize raster")
233 MemoryContextSwitchTo(oldcontext);
234 SRF_RETURN_DONE(funcctx);
237 if (!PG_ARGISNULL(1))
238 nband = PG_GETARG_UINT32(1);
245 if (nband < 1 || nband > numbands) {
246 elog(NOTICE,
"Invalid band index (must use 1-based). Returning empty set");
248 PG_FREE_IF_COPY(pgraster, 0);
249 MemoryContextSwitchTo(oldcontext);
250 SRF_RETURN_DONE(funcctx);
253 if (!PG_ARGISNULL(2))
254 exclude_nodata_value = PG_GETARG_BOOL(2);
260 PG_FREE_IF_COPY(pgraster, 0);
261 MemoryContextSwitchTo(oldcontext);
262 SRF_RETURN_DONE(funcctx);
272 PG_FREE_IF_COPY(pgraster, 0);
273 if (NULL == geomval) {
275 errcode(ERRCODE_NO_DATA_FOUND),
276 errmsg(
"Could not polygonize raster")
278 MemoryContextSwitchTo(oldcontext);
279 SRF_RETURN_DONE(funcctx);
285 funcctx->user_fctx = geomval;
288 funcctx->max_calls = nElements;
291 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
293 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
294 errmsg(
"function returning record called in context that cannot accept type record")
298 BlessTupleDesc(tupdesc);
299 funcctx->tuple_desc = tupdesc;
301 MemoryContextSwitchTo(oldcontext);
305 funcctx = SRF_PERCALL_SETUP();
307 call_cntr = funcctx->call_cntr;
308 max_calls = funcctx->max_calls;
309 tupdesc = funcctx->tuple_desc;
310 geomval2 = funcctx->user_fctx;
313 if (call_cntr < max_calls) {
320 size_t gser_size = 0;
330 values[0] = PointerGetDatum(gser);
331 values[1] = Float8GetDatum(geomval2[call_cntr].val);
334 tuple = heap_form_tuple(tupdesc, values, nulls);
337 result = HeapTupleGetDatum(tuple);
339 SRF_RETURN_NEXT(funcctx,
result);
344 SRF_RETURN_DONE(funcctx);
349 #define VALUES_LENGTH 4
357 FuncCallContext *funcctx;
366 if (SRF_IS_FIRSTCALL()) {
367 MemoryContext oldcontext;
374 bool exclude_nodata_value =
TRUE;
375 bool nocolumnx =
FALSE;
389 funcctx = SRF_FIRSTCALL_INIT();
392 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
394 if (PG_ARGISNULL(0)) {
395 MemoryContextSwitchTo(oldcontext);
396 SRF_RETURN_DONE(funcctx);
398 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
404 nband = PG_GETARG_INT32(1);
412 bounds[0] = PG_GETARG_INT32(2);
413 bounds[1] = bounds[0];
420 bounds[2] = PG_GETARG_INT32(3);
421 bounds[3] = bounds[2];
425 if (!PG_ARGISNULL(4))
426 exclude_nodata_value = PG_GETARG_BOOL(4);
430 PG_FREE_IF_COPY(pgraster, 0);
432 errcode(ERRCODE_OUT_OF_MEMORY),
433 errmsg(
"Could not deserialize raster")
435 MemoryContextSwitchTo(oldcontext);
436 SRF_RETURN_DONE(funcctx);
441 elog(NOTICE,
"Raster is empty. Returning empty set");
443 PG_FREE_IF_COPY(pgraster, 0);
444 MemoryContextSwitchTo(oldcontext);
445 SRF_RETURN_DONE(funcctx);
454 if (nband < 1 || nband > numbands) {
455 elog(NOTICE,
"Invalid band index (must use 1-based). Returning empty set");
457 PG_FREE_IF_COPY(pgraster, 0);
458 MemoryContextSwitchTo(oldcontext);
459 SRF_RETURN_DONE(funcctx);
464 elog(NOTICE,
"Could not find band at index %d. Returning empty set",
nband);
466 PG_FREE_IF_COPY(pgraster, 0);
467 MemoryContextSwitchTo(oldcontext);
468 SRF_RETURN_DONE(funcctx);
472 exclude_nodata_value =
FALSE;
485 bounds[0], bounds[1], bounds[2], bounds[3]);
489 for (
y = bounds[2];
y <= bounds[3];
y++) {
491 for (
x = bounds[0];
x <= bounds[1];
x++) {
499 for (i = 0; i < pixcount; i++)
501 if (pixcount) pfree(pix);
505 PG_FREE_IF_COPY(pgraster, 0);
507 MemoryContextSwitchTo(oldcontext);
508 elog(ERROR,
"RASTER_getPixelPolygons: Could not get pixel value");
509 SRF_RETURN_DONE(funcctx);
513 if (isnodata && exclude_nodata_value) {
514 POSTGIS_RT_DEBUG(5,
"pixel value is NODATA and exclude_nodata_value = TRUE");
522 for (i = 0; i < pixcount; i++)
524 if (pixcount) pfree(pix);
528 PG_FREE_IF_COPY(pgraster, 0);
530 MemoryContextSwitchTo(oldcontext);
531 elog(ERROR,
"RASTER_getPixelPolygons: Could not get pixel polygon");
532 SRF_RETURN_DONE(funcctx);
536 pix = palloc(
sizeof(
struct rt_pixel_t) * (pixcount + 1));
538 pix = repalloc(pix,
sizeof(
struct rt_pixel_t) * (pixcount + 1));
544 PG_FREE_IF_COPY(pgraster, 0);
546 MemoryContextSwitchTo(oldcontext);
547 elog(ERROR,
"RASTER_getPixelPolygons: Could not allocate memory for storing pixel polygons");
548 SRF_RETURN_DONE(funcctx);
563 if (exclude_nodata_value)
564 pix[pixcount].
nodata = isnodata;
569 pix[pixcount].
nodata = isnodata;
578 PG_FREE_IF_COPY(pgraster, 0);
582 elog(NOTICE,
"No pixels found for band %d",
nband);
583 MemoryContextSwitchTo(oldcontext);
584 SRF_RETURN_DONE(funcctx);
588 funcctx->user_fctx = pix;
591 funcctx->max_calls = pixcount;
595 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
597 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
598 errmsg(
"function returning record called in context that cannot accept type record")
602 BlessTupleDesc(tupdesc);
603 funcctx->tuple_desc = tupdesc;
605 MemoryContextSwitchTo(oldcontext);
609 funcctx = SRF_PERCALL_SETUP();
611 call_cntr = funcctx->call_cntr;
612 max_calls = funcctx->max_calls;
613 tupdesc = funcctx->tuple_desc;
614 pix2 = funcctx->user_fctx;
617 if (call_cntr < max_calls) {
624 size_t gser_size = 0;
634 values[0] = PointerGetDatum(gser);
635 if (pix2[call_cntr].nodata)
638 values[1] = Float8GetDatum(pix2[call_cntr].
value);
639 values[2] = Int32GetDatum(pix2[call_cntr].
x);
640 values[3] = Int32GetDatum(pix2[call_cntr].
y);
643 tuple = heap_form_tuple(tupdesc, values, nulls);
646 result = HeapTupleGetDatum(tuple);
648 SRF_RETURN_NEXT(funcctx,
result);
653 SRF_RETURN_DONE(funcctx);
658 #define VALUES_LENGTH 4
666 FuncCallContext *funcctx;
674 if (SRF_IS_FIRSTCALL()) {
675 MemoryContext oldcontext;
681 bool hasband =
FALSE;
682 bool exclude_nodata_value =
TRUE;
683 bool nocolumnx =
TRUE;
697 funcctx = SRF_FIRSTCALL_INIT();
700 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
703 if (PG_ARGISNULL(0)) {
704 MemoryContextSwitchTo(oldcontext);
705 SRF_RETURN_DONE(funcctx);
707 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
710 if (!PG_ARGISNULL(1)) {
711 nband = PG_GETARG_INT32(1);
716 if (!PG_ARGISNULL(2)) {
717 bounds[0] = PG_GETARG_INT32(2);
718 bounds[1] = bounds[0];
723 if (!PG_ARGISNULL(3)) {
724 bounds[2] = PG_GETARG_INT32(3);
725 bounds[3] = bounds[2];
730 if (!PG_ARGISNULL(4))
731 exclude_nodata_value = PG_GETARG_BOOL(4);
736 PG_FREE_IF_COPY(pgraster, 0);
738 errcode(ERRCODE_OUT_OF_MEMORY),
739 errmsg(
"Could not deserialize raster")
741 MemoryContextSwitchTo(oldcontext);
742 SRF_RETURN_DONE(funcctx);
747 elog(NOTICE,
"Raster is empty. Returning empty set");
749 PG_FREE_IF_COPY(pgraster, 0);
750 MemoryContextSwitchTo(oldcontext);
751 SRF_RETURN_DONE(funcctx);
761 if (nband < 1 || nband > numbands) {
762 elog(NOTICE,
"Invalid band index (must use 1-based). Returning empty set");
764 PG_FREE_IF_COPY(pgraster, 0);
765 MemoryContextSwitchTo(oldcontext);
766 SRF_RETURN_DONE(funcctx);
772 elog(NOTICE,
"Could not find band at index %d. Returning empty set",
nband);
775 PG_FREE_IF_COPY(pgraster, 0);
777 MemoryContextSwitchTo(oldcontext);
778 SRF_RETURN_DONE(funcctx);
782 exclude_nodata_value =
FALSE;
795 bounds[0], bounds[1], bounds[2], bounds[3]);
799 for (
y = bounds[2];
y <= bounds[3];
y++) {
801 for (
x = bounds[0];
x <= bounds[1];
x++) {
809 for (i = 0; i < pixcount; i++)
811 if (pixcount) pfree(pix);
815 PG_FREE_IF_COPY(pgraster, 0);
817 MemoryContextSwitchTo(oldcontext);
818 elog(ERROR,
"RASTER_getPixelCentroids: Could not get pixel value");
819 SRF_RETURN_DONE(funcctx);
823 if (isnodata && exclude_nodata_value) {
824 POSTGIS_RT_DEBUG(5,
"pixel value is NODATA and exclude_nodata_value = TRUE");
833 for (i = 0; i < pixcount; i++)
835 if (pixcount) pfree(pix);
839 PG_FREE_IF_COPY(pgraster, 0);
841 MemoryContextSwitchTo(oldcontext);
842 elog(ERROR,
"RASTER_getPixelCentroids: Could not get pixel centroid");
843 SRF_RETURN_DONE(funcctx);
848 pix = palloc(
sizeof(
struct rt_pixel_t) * (pixcount + 1));
851 pix = repalloc(pix,
sizeof(
struct rt_pixel_t) * (pixcount + 1));
858 PG_FREE_IF_COPY(pgraster, 0);
860 MemoryContextSwitchTo(oldcontext);
861 elog(ERROR,
"RASTER_getPixelCentroids: Could not allocate memory for storing pixel centroids");
862 SRF_RETURN_DONE(funcctx);
879 if (exclude_nodata_value)
880 pix[pixcount].
nodata = isnodata;
885 pix[pixcount].
nodata = isnodata;
896 PG_FREE_IF_COPY(pgraster, 0);
900 elog(NOTICE,
"No pixels found for band %d",
nband);
901 MemoryContextSwitchTo(oldcontext);
902 SRF_RETURN_DONE(funcctx);
906 funcctx->user_fctx = pix;
909 funcctx->max_calls = pixcount;
913 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
915 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
916 errmsg(
"function returning record called in context that cannot accept type record")
920 BlessTupleDesc(tupdesc);
921 funcctx->tuple_desc = tupdesc;
923 MemoryContextSwitchTo(oldcontext);
927 funcctx = SRF_PERCALL_SETUP();
929 call_cntr = funcctx->call_cntr;
930 max_calls = funcctx->max_calls;
931 tupdesc = funcctx->tuple_desc;
932 pix2 = funcctx->user_fctx;
935 if (call_cntr < max_calls) {
942 size_t gser_size = 0;
953 values[0] = PointerGetDatum(gser);
954 if (pix2[call_cntr].nodata)
957 values[1] = Float8GetDatum(pix2[call_cntr].
value);
958 values[2] = Int32GetDatum(pix2[call_cntr].
x);
959 values[3] = Int32GetDatum(pix2[call_cntr].
y);
962 tuple = heap_form_tuple(tupdesc, values, nulls);
965 result = HeapTupleGetDatum(tuple);
967 SRF_RETURN_NEXT(funcctx,
result);
972 SRF_RETURN_DONE(funcctx);
993 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
997 PG_FREE_IF_COPY(pgraster, 0);
998 elog(ERROR,
"RASTER_getPolygon: Could not deserialize raster");
1004 if (num_bands < 1) {
1005 elog(NOTICE,
"Raster provided has no bands");
1007 PG_FREE_IF_COPY(pgraster, 0);
1012 if (!PG_ARGISNULL(1))
1013 nband = PG_GETARG_INT32(1);
1014 if (nband < 1 || nband > num_bands) {
1015 elog(NOTICE,
"Invalid band index (must use 1-based). Returning NULL");
1017 PG_FREE_IF_COPY(pgraster, 0);
1024 PG_FREE_IF_COPY(pgraster, 0);
1027 elog(ERROR,
"RASTER_getPolygon: Could not get raster band's surface");
1030 else if (surface == NULL) {
1031 elog(NOTICE,
"Raster is empty or all pixels of band are NODATA. Returning NULL");
1038 PG_RETURN_POINTER(rtn);
1056 double scale[2] = {0};
1057 double *scale_x = NULL;
1058 double *scale_y = NULL;
1076 text *pixeltypetext = NULL;
1077 char *pixeltype = NULL;
1080 uint32_t pixtypes_len = 0;
1082 double *values = NULL;
1083 uint32_t values_len = 0;
1085 uint8_t *hasnodatas = NULL;
1086 double *nodatavals = NULL;
1087 uint32_t nodatavals_len = 0;
1089 double ulw[2] = {0};
1090 double *ul_xw = NULL;
1091 double *ul_yw = NULL;
1093 double gridw[2] = {0};
1094 double *grid_xw = NULL;
1095 double *grid_yw = NULL;
1097 double skew[2] = {0};
1098 double *skew_x = NULL;
1099 double *skew_y = NULL;
1101 char **options = NULL;
1102 int options_len = 0;
1104 uint32_t num_bands = 0;
1114 if (PG_ARGISNULL(0))
1117 gser = PG_GETARG_GSERIALIZED_P(0);
1131 PG_FREE_IF_COPY(gser, 0);
1143 SET_VARSIZE(pgrast, pgrast->
size);
1144 PG_RETURN_POINTER(pgrast);
1148 if (!PG_ARGISNULL(1)) {
1149 scale[0] = PG_GETARG_FLOAT8(1);
1151 scale_x = &scale[0];
1155 if (!PG_ARGISNULL(2)) {
1156 scale[1] = PG_GETARG_FLOAT8(2);
1158 scale_y = &scale[1];
1160 POSTGIS_RT_DEBUGF(3,
"RASTER_asRaster: scale (x, y) = %f, %f", scale[0], scale[1]);
1163 if (!PG_ARGISNULL(3)) {
1164 dim[0] = PG_GETARG_INT32(3);
1165 if (dim[0] < 0) dim[0] = 0;
1166 if (dim[0] != 0) dim_x = &dim[0];
1170 if (!PG_ARGISNULL(4)) {
1171 dim[1] = PG_GETARG_INT32(4);
1172 if (dim[1] < 0) dim[1] = 0;
1173 if (dim[1] != 0) dim_y = &dim[1];
1178 if (!PG_ARGISNULL(5)) {
1179 array = PG_GETARG_ARRAYTYPE_P(5);
1180 etype = ARR_ELEMTYPE(array);
1181 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
1189 PG_FREE_IF_COPY(gser, 0);
1191 elog(ERROR,
"RASTER_asRaster: Invalid data type for pixeltype");
1196 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
1202 for (i = 0, j = 0; i < n; i++) {
1211 pixeltypetext = (text *) DatumGetPointer(e[i]);
1212 if (NULL == pixeltypetext)
break;
1213 pixeltype = text_to_cstring(pixeltypetext);
1221 if (strlen(pixeltype)) {
1228 PG_FREE_IF_COPY(gser, 0);
1230 elog(ERROR,
"RASTER_asRaster: Invalid pixel type provided: %s", pixeltype);
1234 pixtypes[j] = pixtype;
1241 pixtypes = repalloc(pixtypes, j *
sizeof(
rt_pixtype));
1251 #if POSTGIS_DEBUG_LEVEL > 0
1252 for (uint32_t u = 0; u < pixtypes_len; u++)
1257 if (!PG_ARGISNULL(6)) {
1258 array = PG_GETARG_ARRAYTYPE_P(6);
1259 etype = ARR_ELEMTYPE(array);
1260 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
1268 if (pixtypes_len) pfree(pixtypes);
1271 PG_FREE_IF_COPY(gser, 0);
1273 elog(ERROR,
"RASTER_asRaster: Invalid data type for value");
1278 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
1282 values = (
double *) palloc(
sizeof(
double) * n);
1283 for (i = 0, j = 0; i < n; i++) {
1291 values[j] = (double) DatumGetFloat4(e[i]);
1294 values[j] = (double) DatumGetFloat8(e[i]);
1304 values = repalloc(values, j *
sizeof(
double));
1314 #if POSTGIS_DEBUG_LEVEL > 0
1315 for (uint32_t u = 0; u < values_len; u++)
1320 if (!PG_ARGISNULL(7)) {
1321 array = PG_GETARG_ARRAYTYPE_P(7);
1322 etype = ARR_ELEMTYPE(array);
1323 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
1331 if (pixtypes_len) pfree(pixtypes);
1332 if (values_len) pfree(values);
1335 PG_FREE_IF_COPY(gser, 0);
1337 elog(ERROR,
"RASTER_asRaster: Invalid data type for nodataval");
1342 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
1346 nodatavals = (
double *) palloc(
sizeof(
double) * n);
1347 hasnodatas = (uint8_t *) palloc(
sizeof(uint8_t) * n);
1348 for (i = 0, j = 0; i < n; i++) {
1359 nodatavals[j] = (double) DatumGetFloat4(e[i]);
1362 nodatavals[j] = (double) DatumGetFloat8(e[i]);
1373 nodatavals = repalloc(nodatavals, j *
sizeof(
double));
1374 hasnodatas = repalloc(hasnodatas, j *
sizeof(uint8_t));
1386 #if POSTGIS_DEBUG_LEVEL > 0
1387 for (uint32_t u = 0; u < nodatavals_len; u++)
1395 if (!PG_ARGISNULL(8)) {
1396 ulw[0] = PG_GETARG_FLOAT8(8);
1401 if (!PG_ARGISNULL(9)) {
1402 ulw[1] = PG_GETARG_FLOAT8(9);
1405 POSTGIS_RT_DEBUGF(3,
"RASTER_asRaster: upperleft (x, y) = %f, %f", ulw[0], ulw[1]);
1408 if (!PG_ARGISNULL(10)) {
1409 gridw[0] = PG_GETARG_FLOAT8(10);
1410 grid_xw = &gridw[0];
1414 if (!PG_ARGISNULL(11)) {
1415 gridw[1] = PG_GETARG_FLOAT8(11);
1416 grid_yw = &gridw[1];
1418 POSTGIS_RT_DEBUGF(3,
"RASTER_asRaster: grid (x, y) = %f, %f", gridw[0], gridw[1]);
1425 (scale_x == NULL && scale_y != NULL) ||
1426 (scale_x != NULL && scale_y == NULL)
1428 elog(NOTICE,
"Values must be provided for both X and Y of scale if one is specified");
1435 (dim_x == NULL && dim_y != NULL) ||
1436 (dim_x != NULL && dim_y == NULL)
1438 elog(NOTICE,
"Values must be provided for both width and height if one is specified");
1445 (scale_x != NULL && scale_y != NULL) &&
1446 (dim_x != NULL && dim_y != NULL)
1448 elog(NOTICE,
"Values provided for X and Y of scale and width and height. Using the width and height");
1456 (scale_x == NULL && scale_y == NULL) &&
1457 (dim_x == NULL && dim_y == NULL)
1459 elog(NOTICE,
"Values must be provided for X and Y of scale or width and height");
1466 (ul_xw == NULL && ul_yw != NULL) ||
1467 (ul_xw != NULL && ul_yw == NULL)
1469 elog(NOTICE,
"Values must be provided for both X and Y when specifying the upper-left corner");
1476 (grid_xw == NULL && grid_yw != NULL) ||
1477 (grid_xw != NULL && grid_yw == NULL)
1479 elog(NOTICE,
"Values must be provided for both X and Y when specifying the alignment");
1486 (ul_xw != NULL && ul_yw != NULL) &&
1487 (grid_xw != NULL && grid_yw != NULL)
1489 elog(NOTICE,
"Values provided for both X and Y of upper-left corner and alignment. Using the values of upper-left corner");
1498 if (pixtypes_len) pfree(pixtypes);
1499 if (values_len) pfree(values);
1500 if (nodatavals_len) {
1506 PG_FREE_IF_COPY(gser, 0);
1512 if (!PG_ARGISNULL(12)) {
1513 skew[0] = PG_GETARG_FLOAT8(12);
1519 if (!PG_ARGISNULL(13)) {
1520 skew[1] = PG_GETARG_FLOAT8(13);
1524 POSTGIS_RT_DEBUGF(3,
"RASTER_asRaster: skew (x, y) = %f, %f", skew[0], skew[1]);
1527 if (!PG_ARGISNULL(14) && PG_GETARG_BOOL(14) ==
TRUE) {
1528 if (options_len == 0) {
1530 options = (
char **) palloc(
sizeof(
char *) * options_len);
1534 options = (
char **) repalloc(options,
sizeof(
char *) * options_len);
1537 options[options_len - 1] = palloc(
sizeof(
char*) * (strlen(
"ALL_TOUCHED=TRUE") + 1));
1538 strcpy(options[options_len - 1],
"ALL_TOUCHED=TRUE");
1543 options = (
char **) repalloc(options,
sizeof(
char *) * options_len);
1544 options[options_len - 1] = NULL;
1555 if (pixtypes_len) pfree(pixtypes);
1556 if (values_len) pfree(values);
1557 if (nodatavals_len) {
1561 if (options_len) pfree(options);
1564 PG_FREE_IF_COPY(gser, 0);
1566 elog(ERROR,
"RASTER_asRaster: Could not find srtext for SRID (%d)",
srid);
1576 num_bands =
MIN(pixtypes_len, values_len);
1577 num_bands =
MIN(num_bands, nodatavals_len);
1585 (pixtypes_len == values_len) &&
1586 (values_len == nodatavals_len)
1590 "Imbalanced number of values provided for pixeltype (%d), value (%d) and nodataval (%d). Using the first %d values of each parameter",
1602 PG_FREE_IF_COPY(gser, 0);
1628 if (pixtypes_len) pfree(pixtypes);
1629 if (values_len) pfree(values);
1630 if (nodatavals_len) {
1634 if (options_len) pfree(options);
1637 elog(ERROR,
"RASTER_asRaster: Could not rasterize geometry");
1647 if (NULL == pgrast) PG_RETURN_NULL();
1651 SET_VARSIZE(pgrast, pgrast->
size);
1652 PG_RETURN_POINTER(pgrast);
char result[OUT_DOUBLE_BUFFER_SIZE]
int32_t gserialized_get_srid(const GSERIALIZED *g)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
GSERIALIZED * gserialized_from_lwgeom(LWGEOM *geom, size_t *size)
Allocate a new GSERIALIZED from an LWGEOM.
int lwgeom_ndims(const LWGEOM *geom)
Return the number of dimensions (2, 3, 4) in a geometry.
void lwpoint_free(LWPOINT *pt)
void lwgeom_free(LWGEOM *geom)
void lwmpoly_free(LWMPOLY *mpoly)
#define LWSIZE_GET(varsize)
Macro for reading the size from the GSERIALIZED size attribute.
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
LWGEOM * lwmpoly_as_lwgeom(const LWMPOLY *obj)
void lwpoly_free(LWPOLY *poly)
#define SRID_UNKNOWN
Unknown SRID value.
lwvarlena_t * lwgeom_to_wkb_varlena(const LWGEOM *geom, uint8_t variant)
int32_t clamp_srid(int32_t srid)
Return a valid SRID from an arbitrary integer Raises a notice if what comes out is different from wha...
LWGEOM * lwgeom_force_2d(const LWGEOM *geom)
Strip out the Z/M components of an LWGEOM.
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
rt_pixtype rt_pixtype_index_from_name(const char *pixname)
rt_errorstate rt_band_get_pixel(rt_band band, int x, int y, double *value, int *nodata)
Get pixel value.
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
LWPOLY * rt_raster_pixel_as_polygon(rt_raster raster, int x, int y)
Get a raster pixel as a polygon.
LWPOINT * rt_raster_pixel_as_centroid_point(rt_raster rast, int x, int y)
Get a raster pixel centroid point.
int rt_band_get_isnodata_flag(rt_band band)
Get isnodata flag value.
rt_errorstate rt_raster_surface(rt_raster raster, int nband, LWMPOLY **surface)
Get a raster as a surface (multipolygon).
rt_raster rt_raster_new(uint32_t width, uint32_t height)
Construct a raster with given dimensions.
int rt_raster_has_band(rt_raster raster, int nband)
Return TRUE if the raster has a band of this number.
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
rt_geomval rt_raster_gdal_polygonize(rt_raster raster, int nband, int exclude_nodata_value, int *pnElements)
Returns a set of "geomval" value, one for each group of pixel sharing the same value for the provided...
rt_raster rt_raster_gdal_rasterize(const unsigned char *wkb, uint32_t wkb_len, const char *srs, uint32_t num_bands, rt_pixtype *pixtype, double *init, double *value, double *nodata, uint8_t *hasnodata, int *width, int *height, double *scale_x, double *scale_y, double *ul_xw, double *ul_yw, double *grid_xw, double *grid_yw, double *skew_x, double *skew_y, char **options)
Return a raster of the provided geometry.
void rt_band_destroy(rt_band band)
Destroy a raster band.
uint16_t rt_raster_get_num_bands(rt_raster raster)
rt_errorstate rt_raster_get_convex_hull(rt_raster raster, LWGEOM **hull)
Get raster's convex hull.
uint16_t rt_raster_get_height(rt_raster raster)
void rt_raster_set_srid(rt_raster raster, int32_t srid)
Set raster's SRID.
uint16_t rt_raster_get_width(rt_raster raster)
rt_errorstate rt_raster_get_envelope_geom(rt_raster raster, LWGEOM **env)
Get raster's envelope as a geometry.
rt_errorstate rt_raster_get_perimeter(rt_raster raster, int nband, LWGEOM **perimeter)
Get raster perimeter.
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
int rt_raster_is_empty(rt_raster raster)
Return TRUE if the raster is empty.
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
static int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members)
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
Datum RASTER_dumpAsPolygons(PG_FUNCTION_ARGS)
Datum RASTER_getPolygon(PG_FUNCTION_ARGS)
Datum RASTER_convex_hull(PG_FUNCTION_ARGS)
Datum RASTER_envelope(PG_FUNCTION_ARGS)
Datum RASTER_getPixelCentroids(PG_FUNCTION_ARGS)
Datum RASTER_asRaster(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(RASTER_envelope)
Datum RASTER_getPixelPolygons(PG_FUNCTION_ARGS)
char * rtpg_getSR(int32_t srid)
char * rtpg_trim(const char *input)
#define POSTGIS_RT_DEBUG(level, msg)
#define POSTGIS_RT_DEBUGF(level, msg,...)