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

Definition at line 353 of file rtpg_mapalgebra.c.

References rt_iterator_arg_t::columns, rt_iterator_arg_t::dst_pixel, if(), rt_iterator_arg_t::nodata, POSTGIS_RT_DEBUGF, rt_iterator_arg_t::rasters, rt_iterator_arg_t::rows, rt_iterator_arg_t::src_pixel, rtpg_nmapalgebra_callback_arg::ufc_info, rtpg_nmapalgebra_callback_arg::ufc_rettype, rt_iterator_arg_t::values, pixval::x, and pixval::y.

Referenced by RASTER_nMapAlgebra().

356  {
358 
359  int16 typlen;
360  bool typbyval;
361  char typalign;
362 
363  ArrayType *mdValues = NULL;
364  Datum *_values = NULL;
365  bool *_nodata = NULL;
366 
367  ArrayType *mdPos = NULL;
368  Datum *_pos = NULL;
369  bool *_null = NULL;
370 
371  int i = 0;
372  uint32_t x = 0;
373  uint32_t y = 0;
374  int z = 0;
375  int dim[3] = {0};
376  int lbound[3] = {1, 1, 1};
377  Datum datum = (Datum) NULL;
378 
379  if (arg == NULL)
380  return 0;
381 
382  *value = 0;
383  *nodata = 0;
384 
385  dim[0] = arg->rasters;
386  dim[1] = arg->rows;
387  dim[2] = arg->columns;
388 
389  _values = palloc(sizeof(Datum) * arg->rasters * arg->rows * arg->columns);
390  _nodata = palloc(sizeof(bool) * arg->rasters * arg->rows * arg->columns);
391  if (_values == NULL || _nodata == NULL) {
392  elog(ERROR, "rtpg_nmapalgebra_callback: Could not allocate memory for values array");
393  return 0;
394  }
395 
396  /* build mdValues */
397  i = 0;
398  /* raster */
399  for (z = 0; z < arg->rasters; z++) {
400  /* Y axis */
401  for (y = 0; y < arg->rows; y++) {
402  /* X axis */
403  for (x = 0; x < arg->columns; x++) {
404  POSTGIS_RT_DEBUGF(4, "(z, y ,x) = (%d, %d, %d)", z, y, x);
405  POSTGIS_RT_DEBUGF(4, "(value, nodata) = (%f, %d)", arg->values[z][y][x], arg->nodata[z][y][x]);
406 
407  _nodata[i] = (bool) arg->nodata[z][y][x];
408  if (!_nodata[i])
409  _values[i] = Float8GetDatum(arg->values[z][y][x]);
410  else
411  _values[i] = (Datum) NULL;
412 
413  i++;
414  }
415  }
416  }
417 
418  /* info about the type of item in the multi-dimensional array (float8). */
419  get_typlenbyvalalign(FLOAT8OID, &typlen, &typbyval, &typalign);
420 
421  /* construct mdValues */
422  mdValues = construct_md_array(
423  _values, _nodata,
424  3, dim, lbound,
425  FLOAT8OID,
426  typlen, typbyval, typalign
427  );
428  pfree(_nodata);
429  pfree(_values);
430 
431  _pos = palloc(sizeof(Datum) * (arg->rasters + 1) * 2);
432  _null = palloc(sizeof(bool) * (arg->rasters + 1) * 2);
433  if (_pos == NULL || _null == NULL) {
434  pfree(mdValues);
435  elog(ERROR, "rtpg_nmapalgebra_callback: Could not allocate memory for position array");
436  return 0;
437  }
438  memset(_null, 0, sizeof(bool) * (arg->rasters + 1) * 2);
439 
440  /* build mdPos */
441  i = 0;
442  _pos[i] = arg->dst_pixel[0] + 1;
443  i++;
444  _pos[i] = arg->dst_pixel[1] + 1;
445  i++;
446 
447  for (z = 0; z < arg->rasters; z++) {
448  _pos[i] = arg->src_pixel[z][0] + 1;
449  i++;
450 
451  _pos[i] = arg->src_pixel[z][1] + 1;
452  i++;
453  }
454 
455  /* info about the type of item in the multi-dimensional array (int4). */
456  get_typlenbyvalalign(INT4OID, &typlen, &typbyval, &typalign);
457 
458  /* reuse dim and lbound, just tweak to what we need */
459  dim[0] = arg->rasters + 1;
460  dim[1] = 2;
461  lbound[0] = 0;
462 
463  /* construct mdPos */
464  mdPos = construct_md_array(
465  _pos, _null,
466  2, dim, lbound,
467  INT4OID,
468  typlen, typbyval, typalign
469  );
470  pfree(_pos);
471  pfree(_null);
472 
473  callback->ufc_info.arg[0] = PointerGetDatum(mdValues);
474  callback->ufc_info.arg[1] = PointerGetDatum(mdPos);
475 
476  /* call user callback function */
477  datum = FunctionCallInvoke(&(callback->ufc_info));
478  pfree(mdValues);
479  pfree(mdPos);
480 
481  /* result is not null*/
482  if (!callback->ufc_info.isnull) {
483  switch (callback->ufc_rettype) {
484  case FLOAT8OID:
485  *value = DatumGetFloat8(datum);
486  break;
487  case FLOAT4OID:
488  *value = (double) DatumGetFloat4(datum);
489  break;
490  case INT4OID:
491  *value = (double) DatumGetInt32(datum);
492  break;
493  case INT2OID:
494  *value = (double) DatumGetInt16(datum);
495  break;
496  }
497  }
498  else
499  *nodata = 1;
500 
501  return 1;
502 }
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition: rtpostgis.h:65
unsigned int uint32_t
Definition: uthash.h:78
uint32_t columns
Definition: librtcore.h:2406
uint16_t rasters
Definition: librtcore.h:2402
tuple x
Definition: pixval.py:53
double *** values
Definition: librtcore.h:2410
tuple y
Definition: pixval.py:54
if(!(yy_init))
Definition: lwin_wkt_lex.c:888
FunctionCallInfoData ufc_info

Here is the call graph for this function:

Here is the caller graph for this function: