PostGIS  3.0.6dev-r@@SVN_REVISION@@

◆ RASTER_dumpAsPolygons()

Datum RASTER_dumpAsPolygons ( PG_FUNCTION_ARGS  )

Dump raster

Definition at line 191 of file rtpg_geometry.c.

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

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: