PostGIS  2.1.10dev-r@@SVN_REVISION@@
static int rtpg_nmapalgebraexpr_callback ( rt_iterator_arg  arg,
void *  userarg,
double *  value,
int *  nodata 
)
static

Definition at line 17108 of file rt_pg.c.

References rtpg_nmapalgebraexpr_callback_arg::count, rtpg_nmapalgebraexpr_callback_arg::expr, FALSE, rtpg_nmapalgebraexpr_callback_arg::hasval, rtpg_nmapalgebraexpr_callback_arg::kw, rt_iterator_arg_t::nodata, rtpg_nmapalgebraexpr_callback_arg::nodatanodata, POSTGIS_RT_DEBUG, POSTGIS_RT_DEBUGF, rt_iterator_arg_t::rasters, rtpg_nmapalgebraexpr_callback_arg::spi_argcount, rtpg_nmapalgebraexpr_callback_arg::spi_argpos, rtpg_nmapalgebraexpr_callback_arg::spi_plan, rt_iterator_arg_t::src_pixel, TRUE, rtpg_nmapalgebraexpr_callback_arg::val, and rt_iterator_arg_t::values.

Referenced by RASTER_nMapAlgebraExpr().

17111  {
17113  SPIPlanPtr plan = NULL;
17114  int i = 0;
17115  int id = -1;
17116 
17117  if (arg == NULL)
17118  return 0;
17119 
17120  *value = 0;
17121  *nodata = 0;
17122 
17123  /* 2 raster */
17124  if (arg->rasters > 1) {
17125  /* nodata1 = 1 AND nodata2 = 1, nodatanodataval */
17126  if (arg->nodata[0][0][0] && arg->nodata[1][0][0]) {
17127  if (callback->nodatanodata.hasval)
17128  *value = callback->nodatanodata.val;
17129  else
17130  *nodata = 1;
17131  }
17132  /* nodata1 = 1 AND nodata2 != 1, nodata1expr */
17133  else if (arg->nodata[0][0][0] && !arg->nodata[1][0][0]) {
17134  id = 1;
17135  if (callback->expr[id].hasval)
17136  *value = callback->expr[id].val;
17137  else if (callback->expr[id].spi_plan)
17138  plan = callback->expr[id].spi_plan;
17139  else
17140  *nodata = 1;
17141  }
17142  /* nodata1 != 1 AND nodata2 = 1, nodata2expr */
17143  else if (!arg->nodata[0][0][0] && arg->nodata[1][0][0]) {
17144  id = 2;
17145  if (callback->expr[id].hasval)
17146  *value = callback->expr[id].val;
17147  else if (callback->expr[id].spi_plan)
17148  plan = callback->expr[id].spi_plan;
17149  else
17150  *nodata = 1;
17151  }
17152  /* expression */
17153  else {
17154  id = 0;
17155  if (callback->expr[id].hasval)
17156  *value = callback->expr[id].val;
17157  else if (callback->expr[id].spi_plan)
17158  plan = callback->expr[id].spi_plan;
17159  else {
17160  if (callback->nodatanodata.hasval)
17161  *value = callback->nodatanodata.val;
17162  else
17163  *nodata = 1;
17164  }
17165  }
17166  }
17167  /* 1 raster */
17168  else {
17169  /* nodata = 1, nodata1expr */
17170  if (arg->nodata[0][0][0]) {
17171  id = 1;
17172  if (callback->expr[id].hasval)
17173  *value = callback->expr[id].val;
17174  else if (callback->expr[id].spi_plan)
17175  plan = callback->expr[id].spi_plan;
17176  else
17177  *nodata = 1;
17178  }
17179  /* expression */
17180  else {
17181  id = 0;
17182  if (callback->expr[id].hasval)
17183  *value = callback->expr[id].val;
17184  else if (callback->expr[id].spi_plan)
17185  plan = callback->expr[id].spi_plan;
17186  else {
17187  /* see if nodata1expr is available */
17188  id = 1;
17189  if (callback->expr[id].hasval)
17190  *value = callback->expr[id].val;
17191  else if (callback->expr[id].spi_plan)
17192  plan = callback->expr[id].spi_plan;
17193  else
17194  *nodata = 1;
17195  }
17196  }
17197  }
17198 
17199  /* run prepared plan */
17200  if (plan != NULL) {
17201  Datum values[12];
17202  bool nulls[12];
17203  int err = 0;
17204 
17205  TupleDesc tupdesc;
17206  SPITupleTable *tuptable = NULL;
17207  HeapTuple tuple;
17208  Datum datum;
17209  bool isnull = FALSE;
17210 
17211  POSTGIS_RT_DEBUGF(4, "Running plan %d", id);
17212 
17213  /* init values and nulls */
17214  memset(values, (Datum) NULL, sizeof(Datum) * callback->kw.count);
17215  memset(nulls, FALSE, sizeof(bool) * callback->kw.count);
17216 
17217  if (callback->expr[id].spi_argcount) {
17218  int idx = 0;
17219 
17220  for (i = 0; i < callback->kw.count; i++) {
17221  idx = callback->expr[id].spi_argpos[i];
17222  if (idx < 1) continue;
17223  idx--; /* 1-based now 0-based */
17224 
17225  switch (i) {
17226  /* [rast.x] */
17227  case 0:
17228  values[idx] = Int32GetDatum(arg->src_pixel[0][0] + 1);
17229  break;
17230  /* [rast.y] */
17231  case 1:
17232  values[idx] = Int32GetDatum(arg->src_pixel[0][1] + 1);
17233  break;
17234  /* [rast.val] */
17235  case 2:
17236  /* [rast] */
17237  case 3:
17238  if (!arg->nodata[0][0][0])
17239  values[idx] = Float8GetDatum(arg->values[0][0][0]);
17240  else
17241  nulls[idx] = TRUE;
17242  break;
17243 
17244  /* [rast1.x] */
17245  case 4:
17246  values[idx] = Int32GetDatum(arg->src_pixel[0][0] + 1);
17247  break;
17248  /* [rast1.y] */
17249  case 5:
17250  values[idx] = Int32GetDatum(arg->src_pixel[0][1] + 1);
17251  break;
17252  /* [rast1.val] */
17253  case 6:
17254  /* [rast1] */
17255  case 7:
17256  if (!arg->nodata[0][0][0])
17257  values[idx] = Float8GetDatum(arg->values[0][0][0]);
17258  else
17259  nulls[idx] = TRUE;
17260  break;
17261 
17262  /* [rast2.x] */
17263  case 8:
17264  values[idx] = Int32GetDatum(arg->src_pixel[1][0] + 1);
17265  break;
17266  /* [rast2.y] */
17267  case 9:
17268  values[idx] = Int32GetDatum(arg->src_pixel[1][1] + 1);
17269  break;
17270  /* [rast2.val] */
17271  case 10:
17272  /* [rast2] */
17273  case 11:
17274  if (!arg->nodata[1][0][0])
17275  values[idx] = Float8GetDatum(arg->values[1][0][0]);
17276  else
17277  nulls[idx] = TRUE;
17278  break;
17279  }
17280 
17281  }
17282  }
17283 
17284  /* run prepared plan */
17285  err = SPI_execute_plan(plan, values, nulls, TRUE, 1);
17286  if (err != SPI_OK_SELECT || SPI_tuptable == NULL || SPI_processed != 1) {
17287  elog(ERROR, "rtpg_nmapalgebraexpr_callback: Unexpected error when running prepared statement %d", id);
17288  return 0;
17289  }
17290 
17291  /* get output of prepared plan */
17292  tupdesc = SPI_tuptable->tupdesc;
17293  tuptable = SPI_tuptable;
17294  tuple = tuptable->vals[0];
17295 
17296  datum = SPI_getbinval(tuple, tupdesc, 1, &isnull);
17297  if (SPI_result == SPI_ERROR_NOATTRIBUTE) {
17298  if (SPI_tuptable) SPI_freetuptable(tuptable);
17299  elog(ERROR, "rtpg_nmapalgebraexpr_callback: Could not get result of prepared statement %d", id);
17300  return 0;
17301  }
17302 
17303  if (!isnull) {
17304  *value = DatumGetFloat8(datum);
17305  POSTGIS_RT_DEBUG(4, "Getting value from Datum");
17306  }
17307  else {
17308  /* 2 raster, check nodatanodataval */
17309  if (arg->rasters > 1) {
17310  if (callback->nodatanodata.hasval)
17311  *value = callback->nodatanodata.val;
17312  else
17313  *nodata = 1;
17314  }
17315  /* 1 raster, check nodataval */
17316  else {
17317  if (callback->expr[1].hasval)
17318  *value = callback->expr[1].val;
17319  else
17320  *nodata = 1;
17321  }
17322  }
17323 
17324  if (SPI_tuptable) SPI_freetuptable(tuptable);
17325  }
17326 
17327  POSTGIS_RT_DEBUGF(4, "(value, nodata) = (%f, %d)", *value, *nodata);
17328  return 1;
17329 }
struct rtpg_nmapalgebraexpr_callback_arg::@17 expr[3]
struct rtpg_nmapalgebraexpr_callback_arg::@18 nodatanodata
int ** src_pixel
Definition: rt_api.h:2381
uint16_t rasters
Definition: rt_api.h:2368
#define POSTGIS_RT_DEBUG(level, msg)
Definition: rt_pg.h:58
double *** values
Definition: rt_api.h:2376
struct rtpg_nmapalgebraexpr_callback_arg::@19 kw
#define FALSE
Definition: dbfopen.c:169
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition: rt_pg.h:62
int *** nodata
Definition: rt_api.h:2378
#define TRUE
Definition: dbfopen.c:170

Here is the caller graph for this function: