PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ RASTER_valueCount()

Datum RASTER_valueCount ( PG_FUNCTION_ARGS  )

Definition at line 1446 of file rtpg_statistics.c.

1446  {
1447  FuncCallContext *funcctx;
1448  TupleDesc tupdesc;
1449 
1450  int i;
1451  rt_valuecount vcnts;
1452  rt_valuecount vcnts2;
1453  int call_cntr;
1454  int max_calls;
1455 
1456  /* first call of function */
1457  if (SRF_IS_FIRSTCALL()) {
1458  MemoryContext oldcontext;
1459 
1460  rt_pgraster *pgraster = NULL;
1461  rt_raster raster = NULL;
1462  rt_band band = NULL;
1463  int32_t bandindex = 0;
1464  int num_bands = 0;
1465  bool exclude_nodata_value = TRUE;
1466  double *search_values = NULL;
1467  uint32_t search_values_count = 0;
1468  double roundto = 0;
1469  uint32_t count;
1470 
1471  int j;
1472  int n;
1473 
1474  ArrayType *array;
1475  Oid etype;
1476  Datum *e;
1477  bool *nulls;
1478  int16 typlen;
1479  bool typbyval;
1480  char typalign;
1481 
1482  /* create a function context for cross-call persistence */
1483  funcctx = SRF_FIRSTCALL_INIT();
1484 
1485  /* switch to memory context appropriate for multiple function calls */
1486  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1487 
1488  /* pgraster is null, return nothing */
1489  if (PG_ARGISNULL(0)) {
1490  MemoryContextSwitchTo(oldcontext);
1491  SRF_RETURN_DONE(funcctx);
1492  }
1493  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
1494 
1495  raster = rt_raster_deserialize(pgraster, FALSE);
1496  if (!raster) {
1497  PG_FREE_IF_COPY(pgraster, 0);
1498  MemoryContextSwitchTo(oldcontext);
1499  elog(ERROR, "RASTER_valueCount: Cannot deserialize raster");
1500  SRF_RETURN_DONE(funcctx);
1501  }
1502 
1503  /* band index is 1-based */
1504  bandindex = PG_GETARG_INT32(1);
1505  num_bands = rt_raster_get_num_bands(raster);
1506  if (bandindex < 1 || bandindex > num_bands) {
1507  elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
1509  PG_FREE_IF_COPY(pgraster, 0);
1510  MemoryContextSwitchTo(oldcontext);
1511  SRF_RETURN_DONE(funcctx);
1512  }
1513 
1514  /* exclude_nodata_value flag */
1515  if (!PG_ARGISNULL(2))
1516  exclude_nodata_value = PG_GETARG_BOOL(2);
1517 
1518  /* search values */
1519  if (!PG_ARGISNULL(3)) {
1520  array = PG_GETARG_ARRAYTYPE_P(3);
1521  etype = ARR_ELEMTYPE(array);
1522  get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
1523 
1524  switch (etype) {
1525  case FLOAT4OID:
1526  case FLOAT8OID:
1527  break;
1528  default:
1530  PG_FREE_IF_COPY(pgraster, 0);
1531  MemoryContextSwitchTo(oldcontext);
1532  elog(ERROR, "RASTER_valueCount: Invalid data type for values");
1533  SRF_RETURN_DONE(funcctx);
1534  break;
1535  }
1536 
1537  deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
1538  &nulls, &n);
1539 
1540  search_values = palloc(sizeof(double) * n);
1541  for (i = 0, j = 0; i < n; i++) {
1542  if (nulls[i]) continue;
1543 
1544  switch (etype) {
1545  case FLOAT4OID:
1546  search_values[j] = (double) DatumGetFloat4(e[i]);
1547  break;
1548  case FLOAT8OID:
1549  search_values[j] = (double) DatumGetFloat8(e[i]);
1550  break;
1551  }
1552 
1553  POSTGIS_RT_DEBUGF(5, "search_values[%d] = %f", j, search_values[j]);
1554  j++;
1555  }
1556  search_values_count = j;
1557 
1558  if (j < 1) {
1559  pfree(search_values);
1560  search_values = NULL;
1561  }
1562  }
1563 
1564  /* roundto */
1565  if (!PG_ARGISNULL(4)) {
1566  roundto = PG_GETARG_FLOAT8(4);
1567  if (roundto < 0.) roundto = 0;
1568  }
1569 
1570  /* get band */
1571  band = rt_raster_get_band(raster, bandindex - 1);
1572  if (!band) {
1573  elog(NOTICE, "Cannot find band at index %d. Returning NULL", bandindex);
1575  PG_FREE_IF_COPY(pgraster, 0);
1576  MemoryContextSwitchTo(oldcontext);
1577  SRF_RETURN_DONE(funcctx);
1578  }
1579 
1580  /* get counts of values */
1581  vcnts = rt_band_get_value_count(band, (int) exclude_nodata_value, search_values, search_values_count, roundto, NULL, &count);
1584  PG_FREE_IF_COPY(pgraster, 0);
1585  if (NULL == vcnts || !count) {
1586  elog(NOTICE, "Cannot count the values for band at index %d", bandindex);
1587  MemoryContextSwitchTo(oldcontext);
1588  SRF_RETURN_DONE(funcctx);
1589  }
1590 
1591  POSTGIS_RT_DEBUGF(3, "%d value counts returned", count);
1592 
1593  /* Store needed information */
1594  funcctx->user_fctx = vcnts;
1595 
1596  /* total number of tuples to be returned */
1597  funcctx->max_calls = count;
1598 
1599  /* Build a tuple descriptor for our result type */
1600  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
1601  ereport(ERROR, (
1602  errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1603  errmsg(
1604  "function returning record called in context "
1605  "that cannot accept type record"
1606  )
1607  ));
1608  }
1609 
1610  BlessTupleDesc(tupdesc);
1611  funcctx->tuple_desc = tupdesc;
1612 
1613  MemoryContextSwitchTo(oldcontext);
1614  }
1615 
1616  /* stuff done on every call of the function */
1617  funcctx = SRF_PERCALL_SETUP();
1618 
1619  call_cntr = funcctx->call_cntr;
1620  max_calls = funcctx->max_calls;
1621  tupdesc = funcctx->tuple_desc;
1622  vcnts2 = funcctx->user_fctx;
1623 
1624  /* do when there is more left to send */
1625  if (call_cntr < max_calls) {
1626  Datum values[VALUES_LENGTH];
1627  bool nulls[VALUES_LENGTH];
1628  HeapTuple tuple;
1629  Datum result;
1630 
1631  POSTGIS_RT_DEBUGF(3, "Result %d", call_cntr);
1632 
1633  memset(nulls, FALSE, sizeof(bool) * VALUES_LENGTH);
1634 
1635  values[0] = Float8GetDatum(vcnts2[call_cntr].value);
1636  values[1] = UInt32GetDatum(vcnts2[call_cntr].count);
1637  values[2] = Float8GetDatum(vcnts2[call_cntr].percent);
1638 
1639  /* build a tuple */
1640  tuple = heap_form_tuple(tupdesc, values, nulls);
1641 
1642  /* make the tuple into a datum */
1643  result = HeapTupleGetDatum(tuple);
1644 
1645  SRF_RETURN_NEXT(funcctx, result);
1646  }
1647  /* do when there is no more left */
1648  else {
1649  pfree(vcnts2);
1650  SRF_RETURN_DONE(funcctx);
1651  }
1652 }
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition: cu_print.c:262
#define TRUE
Definition: dbfopen.c:73
#define FALSE
Definition: dbfopen.c:72
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition: rt_raster.c:86
void rt_band_destroy(rt_band band)
Destroy a raster band.
Definition: rt_band.c:340
uint16_t rt_raster_get_num_bands(rt_raster raster)
Definition: rt_raster.c:376
rt_valuecount rt_band_get_value_count(rt_band band, int exclude_nodata_value, double *search_values, uint32_t search_values_count, double roundto, uint32_t *rtn_total, uint32_t *rtn_count)
Count the number of times provided value(s) occur in the band.
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:385
int value
Definition: genraster.py:62
int count
Definition: genraster.py:57
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
#define VALUES_LENGTH
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition: rtpostgis.h:69
Struct definitions.
Definition: librtcore.h:2403

References ovdump::band, genraster::count, FALSE, POSTGIS_RT_DEBUGF, rtrowdump::raster, result, rt_band_destroy(), rt_band_get_value_count(), rt_raster_deserialize(), rt_raster_destroy(), rt_raster_get_band(), rt_raster_get_num_bands(), TRUE, genraster::value, and VALUES_LENGTH.

Here is the call graph for this function: