PostGIS  2.5.0beta2dev-r@@SVN_REVISION@@

◆ RASTER_getPixelPolygons()

Datum RASTER_getPixelPolygons ( PG_FUNCTION_ARGS  )

Definition at line 351 of file rtpg_geometry.c.

References ovdump::band, ES_NONE, FALSE, rt_pixel_t::geom, gserialized_from_lwgeom(), lwgeom_free(), lwpoly_free(), pixval::nband, rt_pixel_t::nodata, PG_FUNCTION_INFO_V1(), POSTGIS_RT_DEBUG, POSTGIS_RT_DEBUGF, rtrowdump::raster, RASTER_getPolygon(), rt_band_destroy(), rt_band_get_hasnodata_flag(), rt_band_get_pixel(), rt_raster_deserialize(), rt_raster_destroy(), rt_raster_get_band(), rt_raster_get_height(), rt_raster_get_num_bands(), rt_raster_get_width(), rt_raster_is_empty(), rt_raster_pixel_as_polygon(), TRUE, genraster::value, rt_pixel_t::value, pixval::x, rt_pixel_t::x, pixval::y, and rt_pixel_t::y.

Referenced by RASTER_dumpAsPolygons().

352 {
353  FuncCallContext *funcctx;
354  TupleDesc tupdesc;
355  rt_pixel pix = NULL;
356  rt_pixel pix2;
357  int call_cntr;
358  int max_calls;
359  int i = 0;
360 
361  /* stuff done only on the first call of the function */
362  if (SRF_IS_FIRSTCALL()) {
363  MemoryContext oldcontext;
364  rt_pgraster *pgraster = NULL;
365  rt_raster raster = NULL;
366  rt_band band = NULL;
367  int nband = 1;
368  int numbands;
369  bool hasband = TRUE;
370  bool exclude_nodata_value = TRUE;
371  bool nocolumnx = FALSE;
372  bool norowy = FALSE;
373  int x = 0;
374  int y = 0;
375  int bounds[4] = {0};
376  int pixcount = 0;
377  double value = 0;
378  int isnodata = 0;
379 
380  LWPOLY *poly;
381 
382  POSTGIS_RT_DEBUG(3, "RASTER_getPixelPolygons first call");
383 
384  /* create a function context for cross-call persistence */
385  funcctx = SRF_FIRSTCALL_INIT();
386 
387  /* switch to memory context appropriate for multiple function calls */
388  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
389 
390  if (PG_ARGISNULL(0)) {
391  MemoryContextSwitchTo(oldcontext);
392  SRF_RETURN_DONE(funcctx);
393  }
394  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
395 
396  /* band */
397  if (PG_ARGISNULL(1))
398  hasband = FALSE;
399  else {
400  nband = PG_GETARG_INT32(1);
401  hasband = TRUE;
402  }
403 
404  /* column */
405  if (PG_ARGISNULL(2))
406  nocolumnx = TRUE;
407  else {
408  bounds[0] = PG_GETARG_INT32(2);
409  bounds[1] = bounds[0];
410  }
411 
412  /* row */
413  if (PG_ARGISNULL(3))
414  norowy = TRUE;
415  else {
416  bounds[2] = PG_GETARG_INT32(3);
417  bounds[3] = bounds[2];
418  }
419 
420  /* exclude NODATA */
421  if (!PG_ARGISNULL(4))
422  exclude_nodata_value = PG_GETARG_BOOL(4);
423 
424  raster = rt_raster_deserialize(pgraster, FALSE);
425  if (!raster) {
426  PG_FREE_IF_COPY(pgraster, 0);
427  ereport(ERROR, (
428  errcode(ERRCODE_OUT_OF_MEMORY),
429  errmsg("Could not deserialize raster")
430  ));
431  MemoryContextSwitchTo(oldcontext);
432  SRF_RETURN_DONE(funcctx);
433  }
434 
435  /* raster empty, return NULL */
436  if (rt_raster_is_empty(raster)) {
437  elog(NOTICE, "Raster is empty. Returning NULL");
438  rt_raster_destroy(raster);
439  PG_FREE_IF_COPY(pgraster, 0);
440  MemoryContextSwitchTo(oldcontext);
441  SRF_RETURN_DONE(funcctx);
442  }
443 
444  /* band specified, load band and info */
445  if (hasband) {
446  numbands = rt_raster_get_num_bands(raster);
447  POSTGIS_RT_DEBUGF(3, "band %d", nband);
448  POSTGIS_RT_DEBUGF(3, "# of bands %d", numbands);
449 
450  if (nband < 1 || nband > numbands) {
451  elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
452  rt_raster_destroy(raster);
453  PG_FREE_IF_COPY(pgraster, 0);
454  MemoryContextSwitchTo(oldcontext);
455  SRF_RETURN_DONE(funcctx);
456  }
457 
458  band = rt_raster_get_band(raster, nband - 1);
459  if (!band) {
460  elog(NOTICE, "Could not find band at index %d. Returning NULL", nband);
461  rt_raster_destroy(raster);
462  PG_FREE_IF_COPY(pgraster, 0);
463  MemoryContextSwitchTo(oldcontext);
464  SRF_RETURN_DONE(funcctx);
465  }
466 
467  if (!rt_band_get_hasnodata_flag(band))
468  exclude_nodata_value = FALSE;
469  }
470 
471  /* set bounds if columnx, rowy not set */
472  if (nocolumnx) {
473  bounds[0] = 1;
474  bounds[1] = rt_raster_get_width(raster);
475  }
476  if (norowy) {
477  bounds[2] = 1;
478  bounds[3] = rt_raster_get_height(raster);
479  }
480  POSTGIS_RT_DEBUGF(3, "bounds (min x, max x, min y, max y) = (%d, %d, %d, %d)",
481  bounds[0], bounds[1], bounds[2], bounds[3]);
482 
483  /* rowy */
484  pixcount = 0;
485  for (y = bounds[2]; y <= bounds[3]; y++) {
486  /* columnx */
487  for (x = bounds[0]; x <= bounds[1]; x++) {
488 
489  value = 0;
490  isnodata = TRUE;
491 
492  if (hasband) {
493  if (rt_band_get_pixel(band, x - 1, y - 1, &value, &isnodata) != ES_NONE) {
494 
495  for (i = 0; i < pixcount; i++)
496  lwgeom_free(pix[i].geom);
497  if (pixcount) pfree(pix);
498 
499  rt_band_destroy(band);
500  rt_raster_destroy(raster);
501  PG_FREE_IF_COPY(pgraster, 0);
502 
503  MemoryContextSwitchTo(oldcontext);
504  elog(ERROR, "RASTER_getPixelPolygons: Could not get pixel value");
505  SRF_RETURN_DONE(funcctx);
506  }
507 
508  /* don't continue if pixel is NODATA and to exclude NODATA */
509  if (isnodata && exclude_nodata_value) {
510  POSTGIS_RT_DEBUG(5, "pixel value is NODATA and exclude_nodata_value = TRUE");
511  continue;
512  }
513  }
514 
515  /* geometry */
516  poly = rt_raster_pixel_as_polygon(raster, x - 1, y - 1);
517  if (!poly) {
518  for (i = 0; i < pixcount; i++)
519  lwgeom_free(pix[i].geom);
520  if (pixcount) pfree(pix);
521 
522  if (hasband) rt_band_destroy(band);
523  rt_raster_destroy(raster);
524  PG_FREE_IF_COPY(pgraster, 0);
525 
526  MemoryContextSwitchTo(oldcontext);
527  elog(ERROR, "RASTER_getPixelPolygons: Could not get pixel polygon");
528  SRF_RETURN_DONE(funcctx);
529  }
530 
531  if (!pixcount)
532  pix = palloc(sizeof(struct rt_pixel_t) * (pixcount + 1));
533  else
534  pix = repalloc(pix, sizeof(struct rt_pixel_t) * (pixcount + 1));
535  if (pix == NULL) {
536 
537  lwpoly_free(poly);
538  if (hasband) rt_band_destroy(band);
539  rt_raster_destroy(raster);
540  PG_FREE_IF_COPY(pgraster, 0);
541 
542  MemoryContextSwitchTo(oldcontext);
543  elog(ERROR, "RASTER_getPixelPolygons: Could not allocate memory for storing pixel polygons");
544  SRF_RETURN_DONE(funcctx);
545  }
546  pix[pixcount].geom = (LWGEOM *) poly;
547  POSTGIS_RT_DEBUGF(5, "poly @ %p", poly);
548  POSTGIS_RT_DEBUGF(5, "geom @ %p", pix[pixcount].geom);
549 
550  /* x, y */
551  pix[pixcount].x = x;
552  pix[pixcount].y = y;
553 
554  /* value */
555  pix[pixcount].value = value;
556 
557  /* NODATA */
558  if (hasband) {
559  if (exclude_nodata_value)
560  pix[pixcount].nodata = isnodata;
561  else
562  pix[pixcount].nodata = FALSE;
563  }
564  else {
565  pix[pixcount].nodata = isnodata;
566  }
567 
568  pixcount++;
569  }
570  }
571 
572  if (hasband) rt_band_destroy(band);
573  rt_raster_destroy(raster);
574  PG_FREE_IF_COPY(pgraster, 0);
575 
576  /* shortcut if no pixcount */
577  if (pixcount < 1) {
578  elog(NOTICE, "No pixels found for band %d", nband);
579  MemoryContextSwitchTo(oldcontext);
580  SRF_RETURN_DONE(funcctx);
581  }
582 
583  /* Store needed information */
584  funcctx->user_fctx = pix;
585 
586  /* total number of tuples to be returned */
587  funcctx->max_calls = pixcount;
588  POSTGIS_RT_DEBUGF(3, "pixcount = %d", pixcount);
589 
590  /* Build a tuple descriptor for our result type */
591  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
592  ereport(ERROR, (
593  errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
594  errmsg("function returning record called in context that cannot accept type record")
595  ));
596  }
597 
598  BlessTupleDesc(tupdesc);
599  funcctx->tuple_desc = tupdesc;
600 
601  MemoryContextSwitchTo(oldcontext);
602  }
603 
604  /* stuff done on every call of the function */
605  funcctx = SRF_PERCALL_SETUP();
606 
607  call_cntr = funcctx->call_cntr;
608  max_calls = funcctx->max_calls;
609  tupdesc = funcctx->tuple_desc;
610  pix2 = funcctx->user_fctx;
611 
612  /* do when there is more left to send */
613  if (call_cntr < max_calls) {
614  int values_length = 4;
615  Datum values[values_length];
616  bool nulls[values_length];
617  HeapTuple tuple;
618  Datum result;
619 
620  GSERIALIZED *gser = NULL;
621  size_t gser_size = 0;
622 
623  POSTGIS_RT_DEBUGF(3, "call number %d", call_cntr);
624 
625  memset(nulls, FALSE, sizeof(bool) * values_length);
626 
627  /* convert LWGEOM to GSERIALIZED */
628  gser = gserialized_from_lwgeom(pix2[call_cntr].geom, &gser_size);
629  lwgeom_free(pix2[call_cntr].geom);
630 
631  values[0] = PointerGetDatum(gser);
632  if (pix2[call_cntr].nodata)
633  nulls[1] = TRUE;
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  /* build a tuple */
640  tuple = heap_form_tuple(tupdesc, values, nulls);
641 
642  /* make the tuple into a datum */
643  result = HeapTupleGetDatum(tuple);
644 
645  SRF_RETURN_NEXT(funcctx, result);
646  }
647  /* do when there is no more left */
648  else {
649  pfree(pix2);
650  SRF_RETURN_DONE(funcctx);
651  }
652 }
uint16_t rt_raster_get_num_bands(rt_raster raster)
Definition: rt_raster.c:372
raster
Be careful!! Zeros function&#39;s input parameter can be a (height x width) array, not (width x height): ...
Definition: rtrowdump.py:121
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
band
Definition: ovdump.py:57
LWGEOM * geom
Definition: librtcore.h:2340
double value
Definition: librtcore.h:2338
void rt_band_destroy(rt_band band)
Destroy a raster band.
Definition: rt_band.c:340
int rt_raster_is_empty(rt_raster raster)
Return TRUE if the raster is empty.
Definition: rt_raster.c:1334
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition: rtpostgis.h:65
nband
Definition: pixval.py:52
void lwpoly_free(LWPOLY *poly)
Definition: lwpoly.c:175
rt_errorstate rt_band_get_pixel(rt_band band, int x, int y, double *value, int *nodata)
Get pixel value.
Definition: rt_band.c:1221
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
Definition: rt_raster.c:381
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
Definition: rt_band.c:674
LWPOLY * rt_raster_pixel_as_polygon(rt_raster raster, int x, int y)
Get a raster pixel as a polygon.
Definition: rt_geometry.c:610
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition: rt_raster.c:82
uint8_t nodata
Definition: librtcore.h:2337
uint16_t rt_raster_get_width(rt_raster raster)
Definition: rt_raster.c:121
#define FALSE
Definition: dbfopen.c:168
Struct definitions.
Definition: librtcore.h:2250
int value
Definition: genraster.py:61
uint16_t rt_raster_get_height(rt_raster raster)
Definition: rt_raster.c:129
#define POSTGIS_RT_DEBUG(level, msg)
Definition: rtpostgis.h:61
GSERIALIZED * gserialized_from_lwgeom(LWGEOM *geom, size_t *size)
Allocate a new GSERIALIZED from an LWGEOM.
#define TRUE
Definition: dbfopen.c:169
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
Definition: rt_serialize.c:717
Here is the call graph for this function:
Here is the caller graph for this function: