PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ rtpg_nmapalgebra_callback()

static int rtpg_nmapalgebra_callback ( rt_iterator_arg  arg,
void *  userarg,
double *  value,
int *  nodata 
)
static

Definition at line 369 of file rtpg_mapalgebra.c.

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

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, genraster::value, rt_iterator_arg_t::values, pixval::x, and pixval::y.

Referenced by RASTER_nMapAlgebra().

Here is the call graph for this function:
Here is the caller graph for this function: