PostGIS  2.2.7dev-r@@SVN_REVISION@@
Datum RASTER_pixelOfValue ( PG_FUNCTION_ARGS  )

Definition at line 1608 of file rtpg_pixel.c.

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

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

Here is the call graph for this function: