PostGIS  3.0.6dev-r@@SVN_REVISION@@

◆ RASTER_getPixelPolygons()

Datum RASTER_getPixelPolygons ( PG_FUNCTION_ARGS  )

Definition at line 352 of file rtpg_geometry.c.

353 {
354  FuncCallContext *funcctx;
355  TupleDesc tupdesc;
356  rt_pixel pix = NULL;
357  rt_pixel pix2;
358  int call_cntr;
359  int max_calls;
360  int i = 0;
361 
362  /* stuff done only on the first call of the function */
363  if (SRF_IS_FIRSTCALL()) {
364  MemoryContext oldcontext;
365  rt_pgraster *pgraster = NULL;
366  rt_raster raster = NULL;
367  rt_band band = NULL;
368  int nband = 1;
369  int numbands;
370  bool hasband = TRUE;
371  bool exclude_nodata_value = TRUE;
372  bool nocolumnx = FALSE;
373  bool norowy = FALSE;
374  int x = 0;
375  int y = 0;
376  int bounds[4] = {0};
377  int pixcount = 0;
378  double value = 0;
379  int isnodata = 0;
380 
381  LWPOLY *poly;
382 
383  POSTGIS_RT_DEBUG(3, "RASTER_getPixelPolygons first call");
384 
385  /* create a function context for cross-call persistence */
386  funcctx = SRF_FIRSTCALL_INIT();
387 
388  /* switch to memory context appropriate for multiple function calls */
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  /* band */
398  if (PG_ARGISNULL(1))
399  hasband = FALSE;
400  else {
401  nband = PG_GETARG_INT32(1);
402  hasband = TRUE;
403  }
404 
405  /* column */
406  if (PG_ARGISNULL(2))
407  nocolumnx = TRUE;
408  else {
409  bounds[0] = PG_GETARG_INT32(2);
410  bounds[1] = bounds[0];
411  }
412 
413  /* row */
414  if (PG_ARGISNULL(3))
415  norowy = TRUE;
416  else {
417  bounds[2] = PG_GETARG_INT32(3);
418  bounds[3] = bounds[2];
419  }
420 
421  /* exclude NODATA */
422  if (!PG_ARGISNULL(4))
423  exclude_nodata_value = PG_GETARG_BOOL(4);
424 
425  raster = rt_raster_deserialize(pgraster, FALSE);
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  /* raster empty, return NULL */
437  if (rt_raster_is_empty(raster)) {
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  /* band specified, load band and info */
446  if (hasband) {
447  numbands = rt_raster_get_num_bands(raster);
448  POSTGIS_RT_DEBUGF(3, "band %d", nband);
449  POSTGIS_RT_DEBUGF(3, "# of bands %d", numbands);
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  /* set bounds if columnx, rowy not set */
473  if (nocolumnx) {
474  bounds[0] = 1;
475  bounds[1] = rt_raster_get_width(raster);
476  }
477  if (norowy) {
478  bounds[2] = 1;
479  bounds[3] = rt_raster_get_height(raster);
480  }
481  POSTGIS_RT_DEBUGF(3, "bounds (min x, max x, min y, max y) = (%d, %d, %d, %d)",
482  bounds[0], bounds[1], bounds[2], bounds[3]);
483 
484  /* rowy */
485  pixcount = 0;
486  for (y = bounds[2]; y <= bounds[3]; y++) {
487  /* columnx */
488  for (x = bounds[0]; x <= bounds[1]; x++) {
489 
490  value = 0;
491  isnodata = TRUE;
492 
493  if (hasband) {
494  if (rt_band_get_pixel(band, x - 1, y - 1, &value, &isnodata) != ES_NONE) {
495 
496  for (i = 0; i < pixcount; i++)
497  lwgeom_free(pix[i].geom);
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  /* don't continue if pixel is NODATA and to exclude NODATA */
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  /* geometry */
517  poly = rt_raster_pixel_as_polygon(raster, x - 1, y - 1);
518  if (!poly) {
519  for (i = 0; i < pixcount; i++)
520  lwgeom_free(pix[i].geom);
521  if (pixcount) pfree(pix);
522 
523  if (hasband) rt_band_destroy(band);
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 
538  lwpoly_free(poly);
539  if (hasband) rt_band_destroy(band);
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  }
547  pix[pixcount].geom = (LWGEOM *) poly;
548  POSTGIS_RT_DEBUGF(5, "poly @ %p", poly);
549  POSTGIS_RT_DEBUGF(5, "geom @ %p", pix[pixcount].geom);
550 
551  /* x, y */
552  pix[pixcount].x = x;
553  pix[pixcount].y = y;
554 
555  /* value */
556  pix[pixcount].value = value;
557 
558  /* NODATA */
559  if (hasband) {
560  if (exclude_nodata_value)
561  pix[pixcount].nodata = isnodata;
562  else
563  pix[pixcount].nodata = FALSE;
564  }
565  else {
566  pix[pixcount].nodata = isnodata;
567  }
568 
569  pixcount++;
570  }
571  }
572 
573  if (hasband) rt_band_destroy(band);
575  PG_FREE_IF_COPY(pgraster, 0);
576 
577  /* shortcut if no pixcount */
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  /* Store needed information */
585  funcctx->user_fctx = pix;
586 
587  /* total number of tuples to be returned */
588  funcctx->max_calls = pixcount;
589  POSTGIS_RT_DEBUGF(3, "pixcount = %d", pixcount);
590 
591  /* Build a tuple descriptor for our result type */
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  /* stuff done on every call of the function */
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  /* do when there is more left to send */
614  if (call_cntr < max_calls) {
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 }
#define TRUE
Definition: dbfopen.c:169
#define FALSE
Definition: dbfopen.c:168
GSERIALIZED * gserialized_from_lwgeom(LWGEOM *geom, size_t *size)
Allocate a new GSERIALIZED from an LWGEOM.
Definition: gserialized.c:222
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1138
void lwpoly_free(LWPOLY *poly)
Definition: lwpoly.c:175
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
Definition: rt_band.c:674
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
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition: rt_raster.c:82
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
@ ES_NONE
Definition: librtcore.h:180
void rt_band_destroy(rt_band band)
Destroy a raster band.
Definition: rt_band.c:340
uint16_t rt_raster_get_num_bands(rt_raster raster)
Definition: rt_raster.c:372
uint16_t rt_raster_get_height(rt_raster raster)
Definition: rt_raster.c:129
uint16_t rt_raster_get_width(rt_raster raster)
Definition: rt_raster.c:121
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
Definition: rt_serialize.c:725
int rt_raster_is_empty(rt_raster raster)
Return TRUE if the raster is empty.
Definition: rt_raster.c:1329
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
Definition: rt_raster.c:381
int value
Definition: genraster.py:62
band
Definition: ovdump.py:58
nband
Definition: pixval.py:53
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
Definition: rtrowdump.py:121
#define VALUES_LENGTH
#define POSTGIS_RT_DEBUG(level, msg)
Definition: rtpostgis.h:61
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition: rtpostgis.h:65
LWGEOM * geom
Definition: librtcore.h:2341
double value
Definition: librtcore.h:2339
uint8_t nodata
Definition: librtcore.h:2338
Struct definitions.
Definition: librtcore.h:2251

References ovdump::band, ES_NONE, FALSE, rt_pixel_t::geom, gserialized_from_lwgeom(), lwgeom_free(), lwpoly_free(), pixval::nband, rt_pixel_t::nodata, POSTGIS_RT_DEBUG, POSTGIS_RT_DEBUGF, rtrowdump::raster, 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, rt_pixel_t::value, genraster::value, VALUES_LENGTH, rt_pixel_t::x, pixval::x, rt_pixel_t::y, and pixval::y.

Here is the call graph for this function: