353{
354 FuncCallContext *funcctx;
355 TupleDesc tupdesc;
358 int call_cntr;
359 int max_calls;
360 int i = 0;
361
362
363 if (SRF_IS_FIRSTCALL()) {
364 MemoryContext oldcontext;
369 int numbands;
371 bool exclude_nodata_value =
TRUE;
372 bool nocolumnx =
FALSE;
376 int bounds[4] = {0};
377 int pixcount = 0;
379 int isnodata = 0;
380
382
384
385
386 funcctx = SRF_FIRSTCALL_INIT();
387
388
389 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
390
391 if (PG_ARGISNULL(0)) {
392 MemoryContextSwitchTo(oldcontext);
393 SRF_RETURN_DONE(funcctx);
394 }
395 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
396
397
398 if (PG_ARGISNULL(1))
400 else {
401 nband = PG_GETARG_INT32(1);
403 }
404
405
406 if (PG_ARGISNULL(2))
408 else {
409 bounds[0] = PG_GETARG_INT32(2);
410 bounds[1] = bounds[0];
411 }
412
413
414 if (PG_ARGISNULL(3))
416 else {
417 bounds[2] = PG_GETARG_INT32(3);
418 bounds[3] = bounds[2];
419 }
420
421
422 if (!PG_ARGISNULL(4))
423 exclude_nodata_value = PG_GETARG_BOOL(4);
424
426 if (!raster) {
427 PG_FREE_IF_COPY(pgraster, 0);
428 ereport(ERROR, (
429 errcode(ERRCODE_OUT_OF_MEMORY),
430 errmsg("Could not deserialize raster")
431 ));
432 MemoryContextSwitchTo(oldcontext);
433 SRF_RETURN_DONE(funcctx);
434 }
435
436
438 elog(NOTICE, "Raster is empty. Returning NULL");
440 PG_FREE_IF_COPY(pgraster, 0);
441 MemoryContextSwitchTo(oldcontext);
442 SRF_RETURN_DONE(funcctx);
443 }
444
445
446 if (hasband) {
450
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);
457 }
458
460 if (!band) {
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);
466 }
467
469 exclude_nodata_value =
FALSE;
470 }
471
472
473 if (nocolumnx) {
474 bounds[0] = 1;
476 }
477 if (norowy) {
478 bounds[2] = 1;
480 }
482 bounds[0], bounds[1], bounds[2], bounds[3]);
483
484
485 pixcount = 0;
486 for (y = bounds[2];
y <= bounds[3];
y++) {
487
488 for (x = bounds[0];
x <= bounds[1];
x++) {
489
492
493 if (hasband) {
495
496 for (i = 0; i < pixcount; i++)
498 if (pixcount) pfree(pix);
499
502 PG_FREE_IF_COPY(pgraster, 0);
503
504 MemoryContextSwitchTo(oldcontext);
505 elog(ERROR, "RASTER_getPixelPolygons: Could not get pixel value");
506 SRF_RETURN_DONE(funcctx);
507 }
508
509
510 if (isnodata && exclude_nodata_value) {
511 POSTGIS_RT_DEBUG(5,
"pixel value is NODATA and exclude_nodata_value = TRUE");
512 continue;
513 }
514 }
515
516
518 if (!poly) {
519 for (i = 0; i < pixcount; i++)
521 if (pixcount) pfree(pix);
522
525 PG_FREE_IF_COPY(pgraster, 0);
526
527 MemoryContextSwitchTo(oldcontext);
528 elog(ERROR, "RASTER_getPixelPolygons: Could not get pixel polygon");
529 SRF_RETURN_DONE(funcctx);
530 }
531
532 if (!pixcount)
533 pix = palloc(
sizeof(
struct rt_pixel_t) * (pixcount + 1));
534 else
535 pix = repalloc(pix,
sizeof(
struct rt_pixel_t) * (pixcount + 1));
536 if (pix == NULL) {
537
541 PG_FREE_IF_COPY(pgraster, 0);
542
543 MemoryContextSwitchTo(oldcontext);
544 elog(ERROR, "RASTER_getPixelPolygons: Could not allocate memory for storing pixel polygons");
545 SRF_RETURN_DONE(funcctx);
546 }
550
551
554
555
557
558
559 if (hasband) {
560 if (exclude_nodata_value)
561 pix[pixcount].
nodata = isnodata;
562 else
564 }
565 else {
566 pix[pixcount].
nodata = isnodata;
567 }
568
569 pixcount++;
570 }
571 }
572
575 PG_FREE_IF_COPY(pgraster, 0);
576
577
578 if (pixcount < 1) {
579 elog(NOTICE, "No pixels found for band %d", nband);
580 MemoryContextSwitchTo(oldcontext);
581 SRF_RETURN_DONE(funcctx);
582 }
583
584
585 funcctx->user_fctx = pix;
586
587
588 funcctx->max_calls = pixcount;
590
591
592 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
593 ereport(ERROR, (
594 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
595 errmsg("function returning record called in context that cannot accept type record")
596 ));
597 }
598
599 BlessTupleDesc(tupdesc);
600 funcctx->tuple_desc = tupdesc;
601
602 MemoryContextSwitchTo(oldcontext);
603 }
604
605
606 funcctx = SRF_PERCALL_SETUP();
607
608 call_cntr = funcctx->call_cntr;
609 max_calls = funcctx->max_calls;
610 tupdesc = funcctx->tuple_desc;
611 pix2 = funcctx->user_fctx;
612
613
614 if (call_cntr < max_calls) {
617 HeapTuple tuple;
618 Datum result;
619
621 size_t gser_size = 0;
622
624
626
627
630
631 values[0] = PointerGetDatum(gser);
632 if (pix2[call_cntr].nodata)
634 else
635 values[1] = Float8GetDatum(pix2[call_cntr].value);
636 values[2] = Int32GetDatum(pix2[call_cntr].x);
637 values[3] = Int32GetDatum(pix2[call_cntr].y);
638
639
640 tuple = heap_form_tuple(tupdesc, values, nulls);
641
642
643 result = HeapTupleGetDatum(tuple);
644
645 SRF_RETURN_NEXT(funcctx, result);
646 }
647
648 else {
649 pfree(pix2);
650 SRF_RETURN_DONE(funcctx);
651 }
652}
GSERIALIZED * gserialized_from_lwgeom(LWGEOM *geom, size_t *size)
Allocate a new GSERIALIZED from an LWGEOM.
void lwgeom_free(LWGEOM *geom)
void lwpoly_free(LWPOLY *poly)
LWPOLY * rt_raster_pixel_as_polygon(rt_raster raster, int x, int y)
Get a raster pixel as a polygon.
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
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.
void rt_band_destroy(rt_band band)
Destroy a raster band.
uint16_t rt_raster_get_num_bands(rt_raster raster)
uint16_t rt_raster_get_height(rt_raster raster)
uint16_t rt_raster_get_width(rt_raster raster)
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.
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
#define POSTGIS_RT_DEBUG(level, msg)
#define POSTGIS_RT_DEBUGF(level, msg,...)