192 FuncCallContext *funcctx;
200 if (SRF_IS_FIRSTCALL()) {
201 MemoryContext oldcontext;
206 bool exclude_nodata_value =
TRUE;
212 funcctx = SRF_FIRSTCALL_INIT();
215 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
218 if (PG_ARGISNULL(0)) {
219 MemoryContextSwitchTo(oldcontext);
220 SRF_RETURN_DONE(funcctx);
222 pgraster = (
rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
225 PG_FREE_IF_COPY(pgraster, 0);
227 errcode(ERRCODE_OUT_OF_MEMORY),
228 errmsg(
"Could not deserialize raster")
230 MemoryContextSwitchTo(oldcontext);
231 SRF_RETURN_DONE(funcctx);
234 if (!PG_ARGISNULL(1))
235 nband = PG_GETARG_UINT32(1);
242 if (nband < 1 || nband > numbands) {
243 elog(NOTICE,
"Invalid band index (must use 1-based). Returning NULL");
245 PG_FREE_IF_COPY(pgraster, 0);
246 MemoryContextSwitchTo(oldcontext);
247 SRF_RETURN_DONE(funcctx);
250 if (!PG_ARGISNULL(2))
251 exclude_nodata_value = PG_GETARG_BOOL(2);
257 PG_FREE_IF_COPY(pgraster, 0);
258 MemoryContextSwitchTo(oldcontext);
259 SRF_RETURN_DONE(funcctx);
269 PG_FREE_IF_COPY(pgraster, 0);
270 if (NULL == geomval) {
272 errcode(ERRCODE_NO_DATA_FOUND),
273 errmsg(
"Could not polygonize raster")
275 MemoryContextSwitchTo(oldcontext);
276 SRF_RETURN_DONE(funcctx);
282 funcctx->user_fctx = geomval;
285 funcctx->max_calls = nElements;
288 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
290 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
291 errmsg(
"function returning record called in context that cannot accept type record")
295 BlessTupleDesc(tupdesc);
296 funcctx->tuple_desc = tupdesc;
298 MemoryContextSwitchTo(oldcontext);
302 funcctx = SRF_PERCALL_SETUP();
304 call_cntr = funcctx->call_cntr;
305 max_calls = funcctx->max_calls;
306 tupdesc = funcctx->tuple_desc;
307 geomval2 = funcctx->user_fctx;
310 if (call_cntr < max_calls) {
317 size_t gser_size = 0;
327 values[0] = PointerGetDatum(gser);
328 values[1] = Float8GetDatum(geomval2[call_cntr].val);
331 tuple = heap_form_tuple(tupdesc, values, nulls);
334 result = HeapTupleGetDatum(tuple);
336 SRF_RETURN_NEXT(funcctx, result);
341 SRF_RETURN_DONE(funcctx);
354 FuncCallContext *funcctx;
363 if (SRF_IS_FIRSTCALL()) {
364 MemoryContext oldcontext;
371 bool exclude_nodata_value =
TRUE;
372 bool nocolumnx =
FALSE;
386 funcctx = SRF_FIRSTCALL_INIT();
389 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
391 if (PG_ARGISNULL(0)) {
392 MemoryContextSwitchTo(oldcontext);
393 SRF_RETURN_DONE(funcctx);
395 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
401 nband = PG_GETARG_INT32(1);
409 bounds[0] = PG_GETARG_INT32(2);
410 bounds[1] = bounds[0];
417 bounds[2] = PG_GETARG_INT32(3);
418 bounds[3] = bounds[2];
422 if (!PG_ARGISNULL(4))
423 exclude_nodata_value = PG_GETARG_BOOL(4);
427 PG_FREE_IF_COPY(pgraster, 0);
429 errcode(ERRCODE_OUT_OF_MEMORY),
430 errmsg(
"Could not deserialize raster")
432 MemoryContextSwitchTo(oldcontext);
433 SRF_RETURN_DONE(funcctx);
438 elog(NOTICE,
"Raster is empty. Returning NULL");
440 PG_FREE_IF_COPY(pgraster, 0);
441 MemoryContextSwitchTo(oldcontext);
442 SRF_RETURN_DONE(funcctx);
451 if (nband < 1 || nband > numbands) {
452 elog(NOTICE,
"Invalid band index (must use 1-based). Returning NULL");
454 PG_FREE_IF_COPY(pgraster, 0);
455 MemoryContextSwitchTo(oldcontext);
456 SRF_RETURN_DONE(funcctx);
461 elog(NOTICE,
"Could not find band at index %d. Returning NULL", nband);
463 PG_FREE_IF_COPY(pgraster, 0);
464 MemoryContextSwitchTo(oldcontext);
465 SRF_RETURN_DONE(funcctx);
469 exclude_nodata_value =
FALSE;
482 bounds[0], bounds[1], bounds[2], bounds[3]);
486 for (y = bounds[2]; y <= bounds[3]; y++) {
488 for (x = bounds[0]; x <= bounds[1]; x++) {
496 for (i = 0; i < pixcount; i++)
498 if (pixcount) pfree(pix);
502 PG_FREE_IF_COPY(pgraster, 0);
504 MemoryContextSwitchTo(oldcontext);
505 elog(ERROR,
"RASTER_getPixelPolygons: Could not get pixel value");
506 SRF_RETURN_DONE(funcctx);
510 if (isnodata && exclude_nodata_value) {
511 POSTGIS_RT_DEBUG(5,
"pixel value is NODATA and exclude_nodata_value = TRUE");
519 for (i = 0; i < pixcount; i++)
521 if (pixcount) pfree(pix);
525 PG_FREE_IF_COPY(pgraster, 0);
527 MemoryContextSwitchTo(oldcontext);
528 elog(ERROR,
"RASTER_getPixelPolygons: Could not get pixel polygon");
529 SRF_RETURN_DONE(funcctx);
533 pix = palloc(
sizeof(
struct rt_pixel_t) * (pixcount + 1));
535 pix = repalloc(pix,
sizeof(
struct rt_pixel_t) * (pixcount + 1));
541 PG_FREE_IF_COPY(pgraster, 0);
543 MemoryContextSwitchTo(oldcontext);
544 elog(ERROR,
"RASTER_getPixelPolygons: Could not allocate memory for storing pixel polygons");
545 SRF_RETURN_DONE(funcctx);
556 pix[pixcount].
value = value;
560 if (exclude_nodata_value)
561 pix[pixcount].
nodata = isnodata;
566 pix[pixcount].
nodata = isnodata;
575 PG_FREE_IF_COPY(pgraster, 0);
579 elog(NOTICE,
"No pixels found for band %d", nband);
580 MemoryContextSwitchTo(oldcontext);
581 SRF_RETURN_DONE(funcctx);
585 funcctx->user_fctx = pix;
588 funcctx->max_calls = pixcount;
592 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
594 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
595 errmsg(
"function returning record called in context that cannot accept type record")
599 BlessTupleDesc(tupdesc);
600 funcctx->tuple_desc = tupdesc;
602 MemoryContextSwitchTo(oldcontext);
606 funcctx = SRF_PERCALL_SETUP();
608 call_cntr = funcctx->call_cntr;
609 max_calls = funcctx->max_calls;
610 tupdesc = funcctx->tuple_desc;
611 pix2 = funcctx->user_fctx;
614 if (call_cntr < max_calls) {
621 size_t gser_size = 0;
631 values[0] = PointerGetDatum(gser);
632 if (pix2[call_cntr].nodata)
635 values[1] = Float8GetDatum(pix2[call_cntr].value);
636 values[2] = Int32GetDatum(pix2[call_cntr].x);
637 values[3] = Int32GetDatum(pix2[call_cntr].y);
640 tuple = heap_form_tuple(tupdesc, values, nulls);
643 result = HeapTupleGetDatum(tuple);
645 SRF_RETURN_NEXT(funcctx, result);
650 SRF_RETURN_DONE(funcctx);
735 double scale[2] = {0};
736 double *scale_x = NULL;
737 double *scale_y = NULL;
755 text *pixeltypetext = NULL;
756 char *pixeltype = NULL;
759 uint32_t pixtypes_len = 0;
761 double *values = NULL;
762 uint32_t values_len = 0;
764 uint8_t *hasnodatas = NULL;
765 double *nodatavals = NULL;
766 uint32_t nodatavals_len = 0;
769 double *ul_xw = NULL;
770 double *ul_yw = NULL;
772 double gridw[2] = {0};
773 double *grid_xw = NULL;
774 double *grid_yw = NULL;
776 double skew[2] = {0};
777 double *skew_x = NULL;
778 double *skew_y = NULL;
780 char **options = NULL;
783 uint32_t num_bands = 0;
796 gser = PG_GETARG_GSERIALIZED_P(0);
810 PG_FREE_IF_COPY(gser, 0);
822 SET_VARSIZE(pgrast, pgrast->
size);
823 PG_RETURN_POINTER(pgrast);
827 if (!PG_ARGISNULL(1)) {
828 scale[0] = PG_GETARG_FLOAT8(1);
834 if (!PG_ARGISNULL(2)) {
835 scale[1] = PG_GETARG_FLOAT8(2);
839 POSTGIS_RT_DEBUGF(3,
"RASTER_asRaster: scale (x, y) = %f, %f", scale[0], scale[1]);
842 if (!PG_ARGISNULL(3)) {
843 dim[0] = PG_GETARG_INT32(3);
844 if (dim[0] < 0) dim[0] = 0;
845 if (dim[0] != 0) dim_x = &dim[0];
849 if (!PG_ARGISNULL(4)) {
850 dim[1] = PG_GETARG_INT32(4);
851 if (dim[1] < 0) dim[1] = 0;
852 if (dim[1] != 0) dim_y = &dim[1];
857 if (!PG_ARGISNULL(5)) {
858 array = PG_GETARG_ARRAYTYPE_P(5);
859 etype = ARR_ELEMTYPE(array);
860 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
868 PG_FREE_IF_COPY(gser, 0);
870 elog(ERROR,
"RASTER_asRaster: Invalid data type for pixeltype");
875 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
881 for (i = 0, j = 0; i < n; i++) {
890 pixeltypetext = (text *) DatumGetPointer(e[i]);
891 if (NULL == pixeltypetext)
break;
900 if (strlen(pixeltype)) {
907 PG_FREE_IF_COPY(gser, 0);
909 elog(ERROR,
"RASTER_asRaster: Invalid pixel type provided: %s", pixeltype);
913 pixtypes[j] = pixtype;
920 pixtypes = repalloc(pixtypes, j *
sizeof(
rt_pixtype));
930#if POSTGIS_DEBUG_LEVEL > 0
931 for (uint32_t u = 0; u < pixtypes_len; u++)
936 if (!PG_ARGISNULL(6)) {
937 array = PG_GETARG_ARRAYTYPE_P(6);
938 etype = ARR_ELEMTYPE(array);
939 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
947 if (pixtypes_len) pfree(pixtypes);
950 PG_FREE_IF_COPY(gser, 0);
952 elog(ERROR,
"RASTER_asRaster: Invalid data type for value");
957 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
961 values = (
double *) palloc(
sizeof(
double) * n);
962 for (i = 0, j = 0; i < n; i++) {
970 values[j] = (double) DatumGetFloat4(e[i]);
973 values[j] = (double) DatumGetFloat8(e[i]);
983 values = repalloc(values, j *
sizeof(
double));
993#if POSTGIS_DEBUG_LEVEL > 0
994 for (uint32_t u = 0; u < values_len; u++)
999 if (!PG_ARGISNULL(7)) {
1000 array = PG_GETARG_ARRAYTYPE_P(7);
1001 etype = ARR_ELEMTYPE(array);
1002 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
1010 if (pixtypes_len) pfree(pixtypes);
1011 if (values_len) pfree(values);
1014 PG_FREE_IF_COPY(gser, 0);
1016 elog(ERROR,
"RASTER_asRaster: Invalid data type for nodataval");
1021 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
1025 nodatavals = (
double *) palloc(
sizeof(
double) * n);
1026 hasnodatas = (uint8_t *) palloc(
sizeof(uint8_t) * n);
1027 for (i = 0, j = 0; i < n; i++) {
1038 nodatavals[j] = (double) DatumGetFloat4(e[i]);
1041 nodatavals[j] = (double) DatumGetFloat8(e[i]);
1052 nodatavals = repalloc(nodatavals, j *
sizeof(
double));
1053 hasnodatas = repalloc(hasnodatas, j *
sizeof(uint8_t));
1065#if POSTGIS_DEBUG_LEVEL > 0
1066 for (uint32_t u = 0; u < nodatavals_len; u++)
1074 if (!PG_ARGISNULL(8)) {
1075 ulw[0] = PG_GETARG_FLOAT8(8);
1080 if (!PG_ARGISNULL(9)) {
1081 ulw[1] = PG_GETARG_FLOAT8(9);
1084 POSTGIS_RT_DEBUGF(3,
"RASTER_asRaster: upperleft (x, y) = %f, %f", ulw[0], ulw[1]);
1087 if (!PG_ARGISNULL(10)) {
1088 gridw[0] = PG_GETARG_FLOAT8(10);
1089 grid_xw = &gridw[0];
1093 if (!PG_ARGISNULL(11)) {
1094 gridw[1] = PG_GETARG_FLOAT8(11);
1095 grid_yw = &gridw[1];
1097 POSTGIS_RT_DEBUGF(3,
"RASTER_asRaster: grid (x, y) = %f, %f", gridw[0], gridw[1]);
1104 (scale_x == NULL && scale_y != NULL) ||
1105 (scale_x != NULL && scale_y == NULL)
1107 elog(NOTICE,
"Values must be provided for both X and Y of scale if one is specified");
1114 (dim_x == NULL && dim_y != NULL) ||
1115 (dim_x != NULL && dim_y == NULL)
1117 elog(NOTICE,
"Values must be provided for both width and height if one is specified");
1124 (scale_x != NULL && scale_y != NULL) &&
1125 (dim_x != NULL && dim_y != NULL)
1127 elog(NOTICE,
"Values provided for X and Y of scale and width and height. Using the width and height");
1135 (scale_x == NULL && scale_y == NULL) &&
1136 (dim_x == NULL && dim_y == NULL)
1138 elog(NOTICE,
"Values must be provided for X and Y of scale or width and height");
1145 (ul_xw == NULL && ul_yw != NULL) ||
1146 (ul_xw != NULL && ul_yw == NULL)
1148 elog(NOTICE,
"Values must be provided for both X and Y when specifying the upper-left corner");
1155 (grid_xw == NULL && grid_yw != NULL) ||
1156 (grid_xw != NULL && grid_yw == NULL)
1158 elog(NOTICE,
"Values must be provided for both X and Y when specifying the alignment");
1165 (ul_xw != NULL && ul_yw != NULL) &&
1166 (grid_xw != NULL && grid_yw != NULL)
1168 elog(NOTICE,
"Values provided for both X and Y of upper-left corner and alignment. Using the values of upper-left corner");
1177 if (pixtypes_len) pfree(pixtypes);
1178 if (values_len) pfree(values);
1179 if (nodatavals_len) {
1185 PG_FREE_IF_COPY(gser, 0);
1191 if (!PG_ARGISNULL(12)) {
1192 skew[0] = PG_GETARG_FLOAT8(12);
1198 if (!PG_ARGISNULL(13)) {
1199 skew[1] = PG_GETARG_FLOAT8(13);
1203 POSTGIS_RT_DEBUGF(3,
"RASTER_asRaster: skew (x, y) = %f, %f", skew[0], skew[1]);
1206 if (!PG_ARGISNULL(14) && PG_GETARG_BOOL(14) ==
TRUE) {
1207 if (options_len == 0) {
1209 options = (
char **) palloc(
sizeof(
char *) * options_len);
1213 options = (
char **) repalloc(options,
sizeof(
char *) * options_len);
1216 options[options_len - 1] = palloc(
sizeof(
char*) * (strlen(
"ALL_TOUCHED=TRUE") + 1));
1217 strcpy(options[options_len - 1],
"ALL_TOUCHED=TRUE");
1222 options = (
char **) repalloc(options,
sizeof(
char *) * options_len);
1223 options[options_len - 1] = NULL;
1234 if (pixtypes_len) pfree(pixtypes);
1235 if (values_len) pfree(values);
1236 if (nodatavals_len) {
1240 if (options_len) pfree(options);
1243 PG_FREE_IF_COPY(gser, 0);
1245 elog(ERROR,
"RASTER_asRaster: Could not find srtext for SRID (%d)",
srid);
1255 num_bands =
MIN(pixtypes_len, values_len);
1256 num_bands =
MIN(num_bands, nodatavals_len);
1264 (pixtypes_len == values_len) &&
1265 (values_len == nodatavals_len)
1269 "Imbalanced number of values provided for pixeltype (%d), value (%d) and nodataval (%d). Using the first %d values of each parameter",
1281 PG_FREE_IF_COPY(gser, 0);
1287 (uint32_t) wkb_len, srs,
1288 num_bands, pixtypes,
1290 nodatavals, hasnodatas,
1299 if (pixtypes_len) pfree(pixtypes);
1300 if (values_len) pfree(values);
1301 if (nodatavals_len) {
1305 if (options_len) pfree(options);
1308 elog(ERROR,
"RASTER_asRaster: Could not rasterize geometry");
1318 if (NULL == pgrast) PG_RETURN_NULL();
1322 SET_VARSIZE(pgrast, pgrast->
size);
1323 PG_RETURN_POINTER(pgrast);
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.