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

Dump raster

Definition at line 1348 of file rt_pg.c.

References FALSE, gserialized_from_lwgeom(), lwgeom_free(), lwpoly_as_lwgeom(), pixval::nband, POSTGIS_RT_DEBUG, POSTGIS_RT_DEBUGF, rtrowdump::raster, result, 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.

1348  {
1349  FuncCallContext *funcctx;
1350  TupleDesc tupdesc;
1351  rt_geomval geomval;
1352  rt_geomval geomval2;
1353  int call_cntr;
1354  int max_calls;
1355 
1356  /* stuff done only on the first call of the function */
1357  if (SRF_IS_FIRSTCALL()) {
1358  MemoryContext oldcontext;
1359  int numbands;
1360  rt_pgraster *pgraster = NULL;
1361  rt_raster raster = NULL;
1362  int nband;
1363  bool exclude_nodata_value = TRUE;
1364  int nElements;
1365 
1366  POSTGIS_RT_DEBUG(2, "RASTER_dumpAsPolygons first call");
1367 
1368  /* create a function context for cross-call persistence */
1369  funcctx = SRF_FIRSTCALL_INIT();
1370 
1371  /* switch to memory context appropriate for multiple function calls */
1372  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1373 
1374  /* Get input arguments */
1375  if (PG_ARGISNULL(0)) {
1376  MemoryContextSwitchTo(oldcontext);
1377  SRF_RETURN_DONE(funcctx);
1378  }
1379  pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
1380  raster = rt_raster_deserialize(pgraster, FALSE);
1381  if (!raster) {
1382  PG_FREE_IF_COPY(pgraster, 0);
1383  ereport(ERROR, (
1384  errcode(ERRCODE_OUT_OF_MEMORY),
1385  errmsg("Could not deserialize raster")
1386  ));
1387  MemoryContextSwitchTo(oldcontext);
1388  SRF_RETURN_DONE(funcctx);
1389  }
1390 
1391  if (!PG_ARGISNULL(1))
1392  nband = PG_GETARG_UINT32(1);
1393  else
1394  nband = 1; /* By default, first band */
1395 
1396  POSTGIS_RT_DEBUGF(3, "band %d", nband);
1397  numbands = rt_raster_get_num_bands(raster);
1398 
1399  if (nband < 1 || nband > numbands) {
1400  elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
1401  rt_raster_destroy(raster);
1402  PG_FREE_IF_COPY(pgraster, 0);
1403  MemoryContextSwitchTo(oldcontext);
1404  SRF_RETURN_DONE(funcctx);
1405  }
1406 
1407  if (!PG_ARGISNULL(2))
1408  exclude_nodata_value = PG_GETARG_BOOL(2);
1409 
1410  /* see if band is NODATA */
1411  if (rt_band_get_isnodata_flag(rt_raster_get_band(raster, nband - 1))) {
1412  POSTGIS_RT_DEBUGF(3, "Band at index %d is NODATA. Returning NULL", nband);
1413  rt_raster_destroy(raster);
1414  PG_FREE_IF_COPY(pgraster, 0);
1415  MemoryContextSwitchTo(oldcontext);
1416  SRF_RETURN_DONE(funcctx);
1417  }
1418 
1419  /* Polygonize raster */
1420 
1424  geomval = rt_raster_gdal_polygonize(raster, nband - 1, exclude_nodata_value, &nElements);
1425  rt_raster_destroy(raster);
1426  PG_FREE_IF_COPY(pgraster, 0);
1427  if (NULL == geomval) {
1428  ereport(ERROR, (
1429  errcode(ERRCODE_NO_DATA_FOUND),
1430  errmsg("Could not polygonize raster")
1431  ));
1432  MemoryContextSwitchTo(oldcontext);
1433  SRF_RETURN_DONE(funcctx);
1434  }
1435 
1436  POSTGIS_RT_DEBUGF(3, "raster dump, %d elements returned", nElements);
1437 
1438  /* Store needed information */
1439  funcctx->user_fctx = geomval;
1440 
1441  /* total number of tuples to be returned */
1442  funcctx->max_calls = nElements;
1443 
1444  /* Build a tuple descriptor for our result type */
1445  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
1446  ereport(ERROR, (
1447  errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1448  errmsg("function returning record called in context that cannot accept type record")
1449  ));
1450  }
1451 
1452  BlessTupleDesc(tupdesc);
1453  funcctx->tuple_desc = tupdesc;
1454 
1455  MemoryContextSwitchTo(oldcontext);
1456  }
1457 
1458  /* stuff done on every call of the function */
1459  funcctx = SRF_PERCALL_SETUP();
1460 
1461  call_cntr = funcctx->call_cntr;
1462  max_calls = funcctx->max_calls;
1463  tupdesc = funcctx->tuple_desc;
1464  geomval2 = funcctx->user_fctx;
1465 
1466  /* do when there is more left to send */
1467  if (call_cntr < max_calls) {
1468  int values_length = 2;
1469  Datum values[values_length];
1470  bool nulls[values_length];
1471  HeapTuple tuple;
1472  Datum result;
1473 
1474  GSERIALIZED *gser = NULL;
1475  size_t gser_size = 0;
1476 
1477  POSTGIS_RT_DEBUGF(3, "call number %d", call_cntr);
1478 
1479  memset(nulls, FALSE, sizeof(bool) * values_length);
1480 
1481  /* convert LWGEOM to GSERIALIZED */
1482  gser = gserialized_from_lwgeom(lwpoly_as_lwgeom(geomval2[call_cntr].geom), 0, &gser_size);
1483  lwgeom_free(lwpoly_as_lwgeom(geomval2[call_cntr].geom));
1484 
1485  values[0] = PointerGetDatum(gser);
1486  values[1] = Float8GetDatum(geomval2[call_cntr].val);
1487 
1488  /* build a tuple */
1489  tuple = heap_form_tuple(tupdesc, values, nulls);
1490 
1491  /* make the tuple into a datum */
1492  result = HeapTupleGetDatum(tuple);
1493 
1494  SRF_RETURN_NEXT(funcctx, result);
1495  }
1496  /* do when there is no more left */
1497  else {
1498  pfree(geomval2);
1499  SRF_RETURN_DONE(funcctx);
1500  }
1501 }
int rt_raster_get_num_bands(rt_raster raster)
Definition: rt_api.c:5677
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition: rt_api.c:5387
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1006
tuple raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
Definition: rtrowdump.py:123
int rt_band_get_isnodata_flag(rt_band band)
Get isnodata flag value.
Definition: rt_api.c:2042
char ** result
Definition: liblwgeom.h:218
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
Definition: lwgeom.c:239
#define POSTGIS_RT_DEBUG(level, msg)
Definition: rt_pg.h:58
tuple nband
Definition: pixval.py:52
GSERIALIZED * gserialized_from_lwgeom(LWGEOM *geom, int is_geodetic, size_t *size)
Allocate a new GSERIALIZED from an LWGEOM.
Definition: g_serialized.c:908
rt_band rt_raster_get_band(rt_raster raster, int n)
Return Nth band, or NULL if unavailable.
Definition: rt_api.c:5686
#define FALSE
Definition: dbfopen.c:169
Struct definitions.
Definition: rt_api.h:2175
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition: rt_pg.h:62
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
Definition: rt_api.c:8350
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_api.c:6175
#define TRUE
Definition: dbfopen.c:170

Here is the call graph for this function: