PostGIS  2.5.0dev-r@@SVN_REVISION@@
Datum RASTER_dumpAsPolygons ( PG_FUNCTION_ARGS  )

Dump raster

Definition at line 192 of file rtpg_geometry.c.

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(), and TRUE.

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