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

Definition at line 4630 of file rt_pg.c.

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, pixval::x, rt_pixel_t::x, pixval::y, and rt_pixel_t::y.

4631 {
4632  FuncCallContext *funcctx;
4633  TupleDesc tupdesc;
4634 
4635  rt_pixel pixels = NULL;
4636  rt_pixel pixels2 = NULL;
4637  int count = 0;
4638  int i = 0;
4639  int n = 0;
4640  int call_cntr;
4641  int max_calls;
4642 
4643  if (SRF_IS_FIRSTCALL()) {
4644  MemoryContext oldcontext;
4645 
4646  rt_pgraster *pgraster = NULL;
4647  rt_raster raster = NULL;
4648  rt_band band = NULL;
4649  int nband = 1;
4650  int num_bands = 0;
4651  double *search = NULL;
4652  int nsearch = 0;
4653  double val;
4654  bool exclude_nodata_value = TRUE;
4655 
4656  ArrayType *array;
4657  Oid etype;
4658  Datum *e;
4659  bool *nulls;
4660  int16 typlen;
4661  bool typbyval;
4662  char typalign;
4663 
4664  /* create a function context for cross-call persistence */
4665  funcctx = SRF_FIRSTCALL_INIT();
4666 
4667  /* switch to memory context appropriate for multiple function calls */
4668  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
4669 
4670  if (PG_ARGISNULL(0)) {
4671  MemoryContextSwitchTo(oldcontext);
4672  SRF_RETURN_DONE(funcctx);
4673  }
4674  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
4675  raster = rt_raster_deserialize(pgraster, FALSE);
4676  if (!raster) {
4677  PG_FREE_IF_COPY(pgraster, 0);
4678  MemoryContextSwitchTo(oldcontext);
4679  elog(ERROR, "RASTER_pixelOfValue: Could not deserialize raster");
4680  SRF_RETURN_DONE(funcctx);
4681  }
4682 
4683  /* num_bands */
4684  num_bands = rt_raster_get_num_bands(raster);
4685  if (num_bands < 1) {
4686  elog(NOTICE, "Raster provided has no bands");
4687  rt_raster_destroy(raster);
4688  PG_FREE_IF_COPY(pgraster, 0);
4689  MemoryContextSwitchTo(oldcontext);
4690  SRF_RETURN_DONE(funcctx);
4691  }
4692 
4693  /* band index is 1-based */
4694  if (!PG_ARGISNULL(1))
4695  nband = PG_GETARG_INT32(1);
4696  if (nband < 1 || nband > num_bands) {
4697  elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
4698  rt_raster_destroy(raster);
4699  PG_FREE_IF_COPY(pgraster, 0);
4700  MemoryContextSwitchTo(oldcontext);
4701  SRF_RETURN_DONE(funcctx);
4702  }
4703 
4704  /* search values */
4705  array = PG_GETARG_ARRAYTYPE_P(2);
4706  etype = ARR_ELEMTYPE(array);
4707  get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
4708 
4709  switch (etype) {
4710  case FLOAT4OID:
4711  case FLOAT8OID:
4712  break;
4713  default:
4714  rt_raster_destroy(raster);
4715  PG_FREE_IF_COPY(pgraster, 0);
4716  MemoryContextSwitchTo(oldcontext);
4717  elog(ERROR, "RASTER_pixelOfValue: Invalid data type for pixel values");
4718  SRF_RETURN_DONE(funcctx);
4719  break;
4720  }
4721 
4722  deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
4723  &nulls, &n);
4724 
4725  search = palloc(sizeof(double) * n);
4726  for (i = 0, nsearch = 0; i < n; i++) {
4727  if (nulls[i]) continue;
4728 
4729  switch (etype) {
4730  case FLOAT4OID:
4731  val = (double) DatumGetFloat4(e[i]);
4732  break;
4733  case FLOAT8OID:
4734  val = (double) DatumGetFloat8(e[i]);
4735  break;
4736  }
4737 
4738  search[nsearch] = val;
4739  POSTGIS_RT_DEBUGF(3, "search[%d] = %f", nsearch, search[nsearch]);
4740  nsearch++;
4741  }
4742 
4743  /* not searching for anything */
4744  if (nsearch < 1) {
4745  elog(NOTICE, "No search values provided. Returning NULL");
4746  pfree(search);
4747  rt_raster_destroy(raster);
4748  PG_FREE_IF_COPY(pgraster, 0);
4749  MemoryContextSwitchTo(oldcontext);
4750  SRF_RETURN_DONE(funcctx);
4751  }
4752  else if (nsearch < n)
4753  search = repalloc(search, sizeof(double) * nsearch);
4754 
4755  /* exclude_nodata_value flag */
4756  if (!PG_ARGISNULL(3))
4757  exclude_nodata_value = PG_GETARG_BOOL(3);
4758 
4759  /* get band */
4760  band = rt_raster_get_band(raster, nband - 1);
4761  if (!band) {
4762  elog(NOTICE, "Could not find band at index %d. Returning NULL", nband);
4763  rt_raster_destroy(raster);
4764  PG_FREE_IF_COPY(pgraster, 0);
4765  MemoryContextSwitchTo(oldcontext);
4766  SRF_RETURN_DONE(funcctx);
4767  }
4768 
4769  /* get pixels of values */
4771  band, exclude_nodata_value,
4772  search, nsearch,
4773  &pixels
4774  );
4775  pfree(search);
4776  rt_band_destroy(band);
4777  rt_raster_destroy(raster);
4778  PG_FREE_IF_COPY(pgraster, 0);
4779  if (count < 1) {
4780  /* error */
4781  if (count < 0)
4782  elog(NOTICE, "Could not get the pixels of search values for band at index %d", nband);
4783  /* no nearest pixel */
4784  else
4785  elog(NOTICE, "No pixels of search values found for band at index %d", nband);
4786 
4787  MemoryContextSwitchTo(oldcontext);
4788  SRF_RETURN_DONE(funcctx);
4789  }
4790 
4791  /* Store needed information */
4792  funcctx->user_fctx = pixels;
4793 
4794  /* total number of tuples to be returned */
4795  funcctx->max_calls = count;
4796 
4797  /* Build a tuple descriptor for our result type */
4798  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
4799  ereport(ERROR, (
4800  errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4801  errmsg(
4802  "function returning record called in context "
4803  "that cannot accept type record"
4804  )
4805  ));
4806  }
4807 
4808  BlessTupleDesc(tupdesc);
4809  funcctx->tuple_desc = tupdesc;
4810 
4811  MemoryContextSwitchTo(oldcontext);
4812  }
4813 
4814  /* stuff done on every call of the function */
4815  funcctx = SRF_PERCALL_SETUP();
4816 
4817  call_cntr = funcctx->call_cntr;
4818  max_calls = funcctx->max_calls;
4819  tupdesc = funcctx->tuple_desc;
4820  pixels2 = funcctx->user_fctx;
4821 
4822  /* do when there is more left to send */
4823  if (call_cntr < max_calls) {
4824  int values_length = 3;
4825  Datum values[values_length];
4826  bool nulls[values_length];
4827  HeapTuple tuple;
4828  Datum result;
4829 
4830  memset(nulls, FALSE, sizeof(bool) * values_length);
4831 
4832  /* 0-based to 1-based */
4833  pixels2[call_cntr].x += 1;
4834  pixels2[call_cntr].y += 1;
4835 
4836  values[0] = Float8GetDatum(pixels2[call_cntr].value);
4837  values[1] = Int32GetDatum(pixels2[call_cntr].x);
4838  values[2] = Int32GetDatum(pixels2[call_cntr].y);
4839 
4840  /* build a tuple */
4841  tuple = heap_form_tuple(tupdesc, values, nulls);
4842 
4843  /* make the tuple into a datum */
4844  result = HeapTupleGetDatum(tuple);
4845 
4846  SRF_RETURN_NEXT(funcctx, result);
4847  }
4848  else {
4849  pfree(pixels2);
4850  SRF_RETURN_DONE(funcctx);
4851  }
4852 }
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
tuple band
Definition: ovdump.py:57
tuple raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
Definition: rtrowdump.py:123
char ** result
Definition: liblwgeom.h:218
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_api.c:2980
tuple nband
Definition: pixval.py:52
int count
Definition: genraster.py:57
tuple x
Definition: pixval.py:53
void rt_band_destroy(rt_band band)
Destroy a raster band.
Definition: rt_api.c:1650
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
#define TRUE
Definition: dbfopen.c:170
tuple y
Definition: pixval.py:54

Here is the call graph for this function: