PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ RASTER_dumpAsPolygons()

Datum RASTER_dumpAsPolygons ( PG_FUNCTION_ARGS  )

Dump raster

Definition at line 194 of file rtpg_geometry.c.

194  {
195  FuncCallContext *funcctx;
196  TupleDesc tupdesc;
197  rt_geomval geomval;
198  rt_geomval geomval2;
199  int call_cntr;
200  int max_calls;
201 
202  /* stuff done only on the first call of the function */
203  if (SRF_IS_FIRSTCALL()) {
204  MemoryContext oldcontext;
205  int numbands;
206  rt_pgraster *pgraster = NULL;
207  rt_raster raster = NULL;
208  int nband;
209  bool exclude_nodata_value = TRUE;
210  int nElements;
211 
212  POSTGIS_RT_DEBUG(2, "RASTER_dumpAsPolygons first call");
213 
214  /* create a function context for cross-call persistence */
215  funcctx = SRF_FIRSTCALL_INIT();
216 
217  /* switch to memory context appropriate for multiple function calls */
218  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
219 
220  /* Get input arguments */
221  if (PG_ARGISNULL(0)) {
222  MemoryContextSwitchTo(oldcontext);
223  SRF_RETURN_DONE(funcctx);
224  }
225  pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
226  raster = rt_raster_deserialize(pgraster, FALSE);
227  if (!raster) {
228  PG_FREE_IF_COPY(pgraster, 0);
229  ereport(ERROR, (
230  errcode(ERRCODE_OUT_OF_MEMORY),
231  errmsg("Could not deserialize raster")
232  ));
233  MemoryContextSwitchTo(oldcontext);
234  SRF_RETURN_DONE(funcctx);
235  }
236 
237  if (!PG_ARGISNULL(1))
238  nband = PG_GETARG_UINT32(1);
239  else
240  nband = 1; /* By default, first band */
241 
242  POSTGIS_RT_DEBUGF(3, "band %d", nband);
243  numbands = rt_raster_get_num_bands(raster);
244 
245  if (nband < 1 || nband > numbands) {
246  elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
248  PG_FREE_IF_COPY(pgraster, 0);
249  MemoryContextSwitchTo(oldcontext);
250  SRF_RETURN_DONE(funcctx);
251  }
252 
253  if (!PG_ARGISNULL(2))
254  exclude_nodata_value = PG_GETARG_BOOL(2);
255 
256  /* see if band is NODATA */
258  POSTGIS_RT_DEBUGF(3, "Band at index %d is NODATA. Returning NULL", nband);
260  PG_FREE_IF_COPY(pgraster, 0);
261  MemoryContextSwitchTo(oldcontext);
262  SRF_RETURN_DONE(funcctx);
263  }
264 
265  /* Polygonize raster */
266 
270  geomval = rt_raster_gdal_polygonize(raster, nband - 1, exclude_nodata_value, &nElements);
272  PG_FREE_IF_COPY(pgraster, 0);
273  if (NULL == geomval) {
274  ereport(ERROR, (
275  errcode(ERRCODE_NO_DATA_FOUND),
276  errmsg("Could not polygonize raster")
277  ));
278  MemoryContextSwitchTo(oldcontext);
279  SRF_RETURN_DONE(funcctx);
280  }
281 
282  POSTGIS_RT_DEBUGF(3, "raster dump, %d elements returned", nElements);
283 
284  /* Store needed information */
285  funcctx->user_fctx = geomval;
286 
287  /* total number of tuples to be returned */
288  funcctx->max_calls = nElements;
289 
290  /* Build a tuple descriptor for our result type */
291  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
292  ereport(ERROR, (
293  errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
294  errmsg("function returning record called in context that cannot accept type record")
295  ));
296  }
297 
298  BlessTupleDesc(tupdesc);
299  funcctx->tuple_desc = tupdesc;
300 
301  MemoryContextSwitchTo(oldcontext);
302  }
303 
304  /* stuff done on every call of the function */
305  funcctx = SRF_PERCALL_SETUP();
306 
307  call_cntr = funcctx->call_cntr;
308  max_calls = funcctx->max_calls;
309  tupdesc = funcctx->tuple_desc;
310  geomval2 = funcctx->user_fctx;
311 
312  /* do when there is more left to send */
313  if (call_cntr < max_calls) {
314  Datum values[VALUES_LENGTH];
315  bool nulls[VALUES_LENGTH];
316  HeapTuple tuple;
317  Datum result;
318 
319  GSERIALIZED *gser = NULL;
320  size_t gser_size = 0;
321 
322  POSTGIS_RT_DEBUGF(3, "call number %d", call_cntr);
323 
324  memset(nulls, FALSE, sizeof(bool) * VALUES_LENGTH);
325 
326  /* convert LWGEOM to GSERIALIZED */
327  gser = gserialized_from_lwgeom(lwpoly_as_lwgeom(geomval2[call_cntr].geom), &gser_size);
328  lwgeom_free(lwpoly_as_lwgeom(geomval2[call_cntr].geom));
329 
330  values[0] = PointerGetDatum(gser);
331  values[1] = Float8GetDatum(geomval2[call_cntr].val);
332 
333  /* build a tuple */
334  tuple = heap_form_tuple(tupdesc, values, nulls);
335 
336  /* make the tuple into a datum */
337  result = HeapTupleGetDatum(tuple);
338 
339  SRF_RETURN_NEXT(funcctx, result);
340  }
341  /* do when there is no more left */
342  else {
343  pfree(geomval2);
344  SRF_RETURN_DONE(funcctx);
345  }
346 }
#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.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
Definition: lwgeom.c:320
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition: rt_raster.c:82
int rt_band_get_isnodata_flag(rt_band band)
Get isnodata flag value.
Definition: rt_band.c:714
rt_geomval rt_raster_gdal_polygonize(rt_raster raster, int nband, int exclude_nodata_value, int *pnElements)
Returns a set of "geomval" value, one for each group of pixel sharing the same value for the provided...
Definition: rt_geometry.c:940
uint16_t rt_raster_get_num_bands(rt_raster raster)
Definition: rt_raster.c:372
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
Definition: rt_serialize.c:725
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
Definition: rt_raster.c:381
nband
Definition: pixval.py:52
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
Struct definitions.
Definition: librtcore.h:2250

References FALSE, gserialized_from_lwgeom(), lwgeom_free(), lwpoly_as_lwgeom(), pixval::nband, POSTGIS_RT_DEBUG, POSTGIS_RT_DEBUGF, rtrowdump::raster, rt_band_get_isnodata_flag(), rt_raster_deserialize(), rt_raster_destroy(), rt_raster_gdal_polygonize(), rt_raster_get_band(), rt_raster_get_num_bands(), TRUE, and VALUES_LENGTH.

Here is the call graph for this function: