PostGIS  3.7.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 370 of file rtpg_mapalgebra.c.

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

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: