PostGIS  3.1.6dev-r@@SVN_REVISION@@

◆ RASTER_pixelOfValue()

Datum RASTER_pixelOfValue ( PG_FUNCTION_ARGS  )

Definition at line 1618 of file rtpg_pixel.c.

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

References ovdump::band, genraster::count, FALSE, pixval::nband, POSTGIS_RT_DEBUGF, rtrowdump::raster, result, 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, VALUES_LENGTH, rt_pixel_t::x, pixval::x, rt_pixel_t::y, and pixval::y.

Here is the call graph for this function: