PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ RASTER_pixelOfValue()

Datum RASTER_pixelOfValue ( PG_FUNCTION_ARGS  )

Definition at line 1612 of file rtpg_pixel.c.

References ovdump::band, genraster::count, FALSE, pixval::nband, PG_FUNCTION_INFO_V1(), POSTGIS_RT_DEBUGF, rtrowdump::raster, RASTER_nearestValue(), rt_band_destroy(), rt_band_get_pixel_of_value(), rt_raster_deserialize(), rt_raster_destroy(), rt_raster_get_band(), rt_raster_get_num_bands(), TRUE, genraster::value, rtpg_dumpvalues_arg_t::values, VALUES_LENGTH, pixval::x, rt_pixel_t::x, pixval::y, and rt_pixel_t::y.

1613 {
1614  FuncCallContext *funcctx;
1615  TupleDesc tupdesc;
1616 
1617  rt_pixel pixels = NULL;
1618  rt_pixel pixels2 = NULL;
1619  int count = 0;
1620  int i = 0;
1621  int n = 0;
1622  int call_cntr;
1623  int max_calls;
1624 
1625  if (SRF_IS_FIRSTCALL()) {
1626  MemoryContext oldcontext;
1627 
1628  rt_pgraster *pgraster = NULL;
1629  rt_raster raster = NULL;
1630  rt_band band = NULL;
1631  int nband = 1;
1632  int num_bands = 0;
1633  double *search = NULL;
1634  int nsearch = 0;
1635  double val;
1636  bool exclude_nodata_value = TRUE;
1637 
1638  ArrayType *array;
1639  Oid etype;
1640  Datum *e;
1641  bool *nulls;
1642  int16 typlen;
1643  bool typbyval;
1644  char typalign;
1645 
1646  /* create a function context for cross-call persistence */
1647  funcctx = SRF_FIRSTCALL_INIT();
1648 
1649  /* switch to memory context appropriate for multiple function calls */
1650  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1651 
1652  if (PG_ARGISNULL(0)) {
1653  MemoryContextSwitchTo(oldcontext);
1654  SRF_RETURN_DONE(funcctx);
1655  }
1656  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
1657  raster = rt_raster_deserialize(pgraster, FALSE);
1658  if (!raster) {
1659  PG_FREE_IF_COPY(pgraster, 0);
1660  MemoryContextSwitchTo(oldcontext);
1661  elog(ERROR, "RASTER_pixelOfValue: Could not deserialize raster");
1662  SRF_RETURN_DONE(funcctx);
1663  }
1664 
1665  /* num_bands */
1666  num_bands = rt_raster_get_num_bands(raster);
1667  if (num_bands < 1) {
1668  elog(NOTICE, "Raster provided has no bands");
1669  rt_raster_destroy(raster);
1670  PG_FREE_IF_COPY(pgraster, 0);
1671  MemoryContextSwitchTo(oldcontext);
1672  SRF_RETURN_DONE(funcctx);
1673  }
1674 
1675  /* band index is 1-based */
1676  if (!PG_ARGISNULL(1))
1677  nband = PG_GETARG_INT32(1);
1678  if (nband < 1 || nband > num_bands) {
1679  elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
1680  rt_raster_destroy(raster);
1681  PG_FREE_IF_COPY(pgraster, 0);
1682  MemoryContextSwitchTo(oldcontext);
1683  SRF_RETURN_DONE(funcctx);
1684  }
1685 
1686  /* search values */
1687  array = PG_GETARG_ARRAYTYPE_P(2);
1688  etype = ARR_ELEMTYPE(array);
1689  get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
1690 
1691  switch (etype) {
1692  case FLOAT4OID:
1693  case FLOAT8OID:
1694  break;
1695  default:
1696  rt_raster_destroy(raster);
1697  PG_FREE_IF_COPY(pgraster, 0);
1698  MemoryContextSwitchTo(oldcontext);
1699  elog(ERROR, "RASTER_pixelOfValue: Invalid data type for pixel values");
1700  SRF_RETURN_DONE(funcctx);
1701  break;
1702  }
1703 
1704  deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
1705  &nulls, &n);
1706 
1707  search = palloc(sizeof(double) * n);
1708  for (i = 0, nsearch = 0; i < n; i++) {
1709  if (nulls[i]) continue;
1710 
1711  switch (etype) {
1712  case FLOAT4OID:
1713  val = (double) DatumGetFloat4(e[i]);
1714  break;
1715  case FLOAT8OID:
1716  val = (double) DatumGetFloat8(e[i]);
1717  break;
1718  }
1719 
1720  search[nsearch] = val;
1721  POSTGIS_RT_DEBUGF(3, "search[%d] = %f", nsearch, search[nsearch]);
1722  nsearch++;
1723  }
1724 
1725  /* not searching for anything */
1726  if (nsearch < 1) {
1727  elog(NOTICE, "No search values provided. Returning NULL");
1728  pfree(search);
1729  rt_raster_destroy(raster);
1730  PG_FREE_IF_COPY(pgraster, 0);
1731  MemoryContextSwitchTo(oldcontext);
1732  SRF_RETURN_DONE(funcctx);
1733  }
1734  else if (nsearch < n)
1735  search = repalloc(search, sizeof(double) * nsearch);
1736 
1737  /* exclude_nodata_value flag */
1738  if (!PG_ARGISNULL(3))
1739  exclude_nodata_value = PG_GETARG_BOOL(3);
1740 
1741  /* get band */
1742  band = rt_raster_get_band(raster, nband - 1);
1743  if (!band) {
1744  elog(NOTICE, "Could not find band at index %d. Returning NULL", nband);
1745  rt_raster_destroy(raster);
1746  PG_FREE_IF_COPY(pgraster, 0);
1747  MemoryContextSwitchTo(oldcontext);
1748  SRF_RETURN_DONE(funcctx);
1749  }
1750 
1751  /* get pixels of values */
1753  band, exclude_nodata_value,
1754  search, nsearch,
1755  &pixels
1756  );
1757  pfree(search);
1758  rt_band_destroy(band);
1759  rt_raster_destroy(raster);
1760  PG_FREE_IF_COPY(pgraster, 0);
1761  if (count < 1) {
1762  /* error */
1763  if (count < 0)
1764  elog(NOTICE, "Could not get the pixels of search values for band at index %d", nband);
1765  /* no nearest pixel */
1766  else
1767  elog(NOTICE, "No pixels of search values found for band at index %d", nband);
1768 
1769  MemoryContextSwitchTo(oldcontext);
1770  SRF_RETURN_DONE(funcctx);
1771  }
1772 
1773  /* Store needed information */
1774  funcctx->user_fctx = pixels;
1775 
1776  /* total number of tuples to be returned */
1777  funcctx->max_calls = count;
1778 
1779  /* Build a tuple descriptor for our result type */
1780  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
1781  ereport(ERROR, (
1782  errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1783  errmsg(
1784  "function returning record called in context "
1785  "that cannot accept type record"
1786  )
1787  ));
1788  }
1789 
1790  BlessTupleDesc(tupdesc);
1791  funcctx->tuple_desc = tupdesc;
1792 
1793  MemoryContextSwitchTo(oldcontext);
1794  }
1795 
1796  /* stuff done on every call of the function */
1797  funcctx = SRF_PERCALL_SETUP();
1798 
1799  call_cntr = funcctx->call_cntr;
1800  max_calls = funcctx->max_calls;
1801  tupdesc = funcctx->tuple_desc;
1802  pixels2 = funcctx->user_fctx;
1803 
1804  /* do when there is more left to send */
1805  if (call_cntr < max_calls) {
1806  Datum values[VALUES_LENGTH];
1807  bool nulls[VALUES_LENGTH];
1808  HeapTuple tuple;
1809  Datum result;
1810 
1811  memset(nulls, FALSE, sizeof(bool) * VALUES_LENGTH);
1812 
1813  /* 0-based to 1-based */
1814  pixels2[call_cntr].x += 1;
1815  pixels2[call_cntr].y += 1;
1816 
1817  values[0] = Float8GetDatum(pixels2[call_cntr].value);
1818  values[1] = Int32GetDatum(pixels2[call_cntr].x);
1819  values[2] = Int32GetDatum(pixels2[call_cntr].y);
1820 
1821  /* build a tuple */
1822  tuple = heap_form_tuple(tupdesc, values, nulls);
1823 
1824  /* make the tuple into a datum */
1825  result = HeapTupleGetDatum(tuple);
1826 
1827  SRF_RETURN_NEXT(funcctx, result);
1828  }
1829  else {
1830  pfree(pixels2);
1831  SRF_RETURN_DONE(funcctx);
1832  }
1833 }
int rt_raster_get_num_bands(rt_raster raster)
Definition: rt_raster.c:372
raster
Be careful!! Zeros function&#39;s input parameter can be a (height x width) array, not (width x height): ...
Definition: rtrowdump.py:121
band
Definition: ovdump.py:57
int rt_band_get_pixel_of_value(rt_band band, int exclude_nodata_value, double *searchset, int searchcount, rt_pixel *pixels)
Search band for pixel(s) with search values.
Definition: rt_band.c:1519
#define VALUES_LENGTH
Definition: rtpg_pixel.c:1606
void rt_band_destroy(rt_band band)
Destroy a raster band.
Definition: rt_band.c:242
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition: rtpostgis.h:65
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
int count
Definition: genraster.py:56
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 value
Definition: genraster.py:61
#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: