PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ RASTER_Contour()

Datum RASTER_Contour ( PG_FUNCTION_ARGS  )

Definition at line 458 of file rtpg_gdal.c.

459 {
460  /* For return values */
461  typedef struct gdal_contour_result_t {
462  size_t ncontours;
463  struct rt_contour_t *contours;
464  } gdal_contour_result_t;
465 
466  FuncCallContext *funcctx;
467 
468  if (SRF_IS_FIRSTCALL())
469  {
470  MemoryContext oldcontext;
471  TupleDesc tupdesc;
472  gdal_contour_result_t *result;
473  rt_pgraster *pgraster = NULL;
474 
475  /* For reading the raster */
476  int src_srid = SRID_UNKNOWN;
477  char *src_srs = NULL;
478  rt_raster raster = NULL;
479  int num_bands;
480  int band, rv;
481 
482  /* For reading the levels[] */
483  ArrayType *array;
484  size_t array_size = 0;
485 
486  /* For the level parameters */
487  double level_base = 0.0;
488  double level_interval = 100.0;
489  double *fixed_levels = NULL;
490  size_t fixed_levels_count = 0;
491 
492  /* for the polygonize flag */
493  bool polygonize = false;
494 
495  /* create a function context for cross-call persistence */
496  funcctx = SRF_FIRSTCALL_INIT();
497 
498  /* switch to memory context appropriate for multiple function calls */
499  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
500 
501  /* To carry the output from rt_raster_gdal_contour */
502  result = palloc0(sizeof(gdal_contour_result_t));
503 
504  /* Build a tuple descriptor for our return result */
505  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
506  MemoryContextSwitchTo(oldcontext);
507  ereport(ERROR, (
508  errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
509  errmsg(
510  "function returning record called in context "
511  "that cannot accept type record"
512  )
513  ));
514  }
515  BlessTupleDesc(tupdesc);
516  funcctx->tuple_desc = tupdesc;
517 
518  /* Read the raster */
519  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
520  raster = rt_raster_deserialize(pgraster, FALSE);
521  num_bands = rt_raster_get_num_bands(raster);
522  src_srid = clamp_srid(rt_raster_get_srid(raster));
523  src_srs = rtpg_getSR(src_srid);
524 
525  /* Read the band number */
526  band = PG_GETARG_INT32(1);
527  if (band < 1 || band > num_bands) {
528  elog(ERROR, "%s: band number must be between 1 and %u inclusive", __func__, num_bands);
529  }
530 
531  /* Read the level_interval */
532  level_interval = PG_GETARG_FLOAT8(2);
533 
534  /* Read the level_base */
535  level_base = PG_GETARG_FLOAT8(3);
536 
537  if (level_interval <= 0.0) {
538  elog(ERROR, "%s: level interval must be greater than zero", __func__);
539  }
540 
541  /* Read the polygonize flag */
542  polygonize = PG_GETARG_BOOL(5);
543 
544  /* Read the levels array */
545  array = PG_GETARG_ARRAYTYPE_P(4);
546  array_size = ArrayGetNItems(ARR_NDIM(array), ARR_DIMS(array));
547  if (array_size > 0) {
548  Datum value;
549  bool isnull;
550  ArrayIterator iterator = array_create_iterator(array, 0, NULL);
551  fixed_levels = palloc0(array_size * sizeof(double));
552  while (array_iterate(iterator, &value, &isnull))
553  {
554  /* Skip nulls */
555  if (isnull)
556  continue;
557 
558  /* Can out if for some reason we are about to blow memory */
559  if (fixed_levels_count >= array_size)
560  break;
561 
562  fixed_levels[fixed_levels_count++] = DatumGetFloat8(value);
563  }
564  }
565 
566  /* Run the contouring routine */
568  /* input parameters */
569  raster,
570  band,
571  src_srid,
572  src_srs,
573  level_interval,
574  level_base,
575  fixed_levels_count,
576  fixed_levels,
577  polygonize,
578  /* output parameters */
579  &(result->ncontours),
580  &(result->contours)
581  );
582 
583  /* No-op on bad return */
584  if (rv == FALSE) {
585  PG_RETURN_NULL();
586  }
587 
588  funcctx->user_fctx = result;
589  funcctx->max_calls = result->ncontours;
590  MemoryContextSwitchTo(oldcontext);
591  }
592 
593  /* stuff done on every call of the function */
594  funcctx = SRF_PERCALL_SETUP();
595 
596  /* do when there is more left to send */
597  if (funcctx->call_cntr < funcctx->max_calls) {
598 
599  HeapTuple tuple;
600  Datum srf_result;
601  Datum values[3] = {0, 0, 0};
602  bool nulls[3] = {0, 0, 0};
603 
604  gdal_contour_result_t *result = funcctx->user_fctx;
605  struct rt_contour_t c = result->contours[funcctx->call_cntr];
606 
607  if (c.geom) {
608  values[0] = PointerGetDatum(c.geom);
609  values[1] = Int32GetDatum(c.id);
610  values[2] = Float8GetDatum(c.elevation);
611  }
612  else {
613  nulls[0] = true;
614  nulls[1] = true;
615  nulls[2] = true;
616  }
617 
618  /* return a tuple */
619  tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
620  srf_result = HeapTupleGetDatum(tuple);
621  SRF_RETURN_NEXT(funcctx, srf_result);
622  }
623  else {
624  SRF_RETURN_DONE(funcctx);
625  }
626 }
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition: cu_print.c:262
#define FALSE
Definition: dbfopen.c:72
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:215
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...
Definition: lwutil.c:333
int32_t rt_raster_get_srid(rt_raster raster)
Get raster's SRID.
Definition: rt_raster.c:360
uint16_t rt_raster_get_num_bands(rt_raster raster)
Definition: rt_raster.c:376
int rt_raster_gdal_contour(rt_raster src_raster, int src_band, int src_srid, const char *src_srs, double contour_interval, double contour_base, int fixed_level_count, double *fixed_levels, int polygonize, size_t *ncontours, struct rt_contour_t **contours)
Return palloc'ed list of contours.
Definition: rt_gdal.c:114
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
Definition: rt_serialize.c:725
int value
Definition: genraster.py:62
band
Definition: ovdump.py:58
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
Definition: rtrowdump.py:121
char * rtpg_getSR(int32_t srid)
GSERIALIZED * geom
Definition: librtcore.h:1749
double elevation
Definition: librtcore.h:1750
Struct definitions.
Definition: librtcore.h:2403

References ovdump::band, clamp_srid(), rt_contour_t::elevation, FALSE, rt_contour_t::geom, rt_contour_t::id, rtrowdump::raster, result, rt_raster_deserialize(), rt_raster_gdal_contour(), rt_raster_get_num_bands(), rt_raster_get_srid(), rtpg_getSR(), SRID_UNKNOWN, and genraster::value.

Here is the call graph for this function: