PostGIS  2.1.10dev-r@@SVN_REVISION@@
static int rtpg_nmapalgebra_rastbandarg_process ( rtpg_nmapalgebra_arg  arg,
ArrayType *  array,
int *  allnull,
int *  allempty,
int *  noband 
)
static

Definition at line 16360 of file rt_pg.c.

References FALSE, rtpg_nmapalgebra_arg_t::hasband, rtpg_nmapalgebra_arg_t::isempty, pixval::nband, rtpg_nmapalgebra_arg_t::nband, rtpg_nmapalgebra_arg_t::numraster, rtpg_nmapalgebra_arg_t::ownsdata, rtpg_nmapalgebra_arg_t::pgraster, POSTGIS_RT_DEBUG, POSTGIS_RT_DEBUGF, rtpg_nmapalgebra_arg_t::raster, rt_raster_deserialize(), rt_raster_has_band(), and rt_raster_is_empty().

Referenced by RASTER_nMapAlgebra(), and RASTER_nMapAlgebraExpr().

16360  {
16361  Oid etype;
16362  Datum *e;
16363  bool *nulls;
16364  int16 typlen;
16365  bool typbyval;
16366  char typalign;
16367  int n = 0;
16368 
16369  HeapTupleHeader tup;
16370  bool isnull;
16371  Datum tupv;
16372 
16373  int i;
16374  int j;
16375  int nband;
16376 
16377  if (arg == NULL || array == NULL) {
16378  elog(ERROR, "rtpg_nmapalgebra_rastbandarg_process: NULL values not permitted for parameters");
16379  return 0;
16380  }
16381 
16382  etype = ARR_ELEMTYPE(array);
16383  get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
16384 
16385  deconstruct_array(
16386  array,
16387  etype,
16388  typlen, typbyval, typalign,
16389  &e, &nulls, &n
16390  );
16391 
16392  if (!n) {
16393  elog(ERROR, "rtpg_nmapalgebra_rastbandarg_process: Invalid argument for rastbandarg");
16394  return 0;
16395  }
16396 
16397  /* prep arg */
16398  arg->numraster = n;
16399  arg->pgraster = palloc(sizeof(rt_pgraster *) * arg->numraster);
16400  arg->raster = palloc(sizeof(rt_raster) * arg->numraster);
16401  arg->isempty = palloc(sizeof(uint8_t) * arg->numraster);
16402  arg->ownsdata = palloc(sizeof(uint8_t) * arg->numraster);
16403  arg->nband = palloc(sizeof(int) * arg->numraster);
16404  arg->hasband = palloc(sizeof(uint8_t) * arg->numraster);
16405  if (
16406  arg->pgraster == NULL ||
16407  arg->raster == NULL ||
16408  arg->isempty == NULL ||
16409  arg->ownsdata == NULL ||
16410  arg->nband == NULL ||
16411  arg->hasband == NULL
16412  ) {
16413  elog(ERROR, "rtpg_nmapalgebra_rastbandarg_process: Could not allocate memory for processing rastbandarg");
16414  return 0;
16415  }
16416 
16417  *allnull = 0;
16418  *allempty = 0;
16419  *noband = 0;
16420 
16421  /* process each element */
16422  for (i = 0; i < n; i++) {
16423  if (nulls[i]) {
16424  arg->numraster--;
16425  continue;
16426  }
16427 
16428  POSTGIS_RT_DEBUGF(4, "Processing rastbandarg at index %d", i);
16429 
16430  arg->raster[i] = NULL;
16431  arg->isempty[i] = 0;
16432  arg->ownsdata[i] = 1;
16433  arg->nband[i] = 0;
16434  arg->hasband[i] = 0;
16435 
16436  /* each element is a tuple */
16437  tup = (HeapTupleHeader) DatumGetPointer(e[i]);
16438  if (NULL == tup) {
16439  elog(ERROR, "rtpg_nmapalgebra_rastbandarg_process: Invalid argument for rastbandarg at index %d", i);
16440  return 0;
16441  }
16442 
16443  /* first element, raster */
16444  POSTGIS_RT_DEBUG(4, "Processing first element (raster)");
16445  tupv = GetAttributeByName(tup, "rast", &isnull);
16446  if (isnull) {
16447  elog(NOTICE, "First argument (nband) of rastbandarg at index %d is NULL. Assuming NULL raster", i);
16448  arg->isempty[i] = 1;
16449  arg->ownsdata[i] = 0;
16450 
16451  (*allnull)++;
16452  (*allempty)++;
16453  (*noband)++;
16454 
16455  continue;
16456  }
16457 
16458  arg->pgraster[i] = (rt_pgraster *) PG_DETOAST_DATUM(tupv);
16459 
16460  /* see if this is a copy of an existing pgraster */
16461  for (j = 0; j < i; j++) {
16462  if (!arg->isempty[j] && (arg->pgraster[i] == arg->pgraster[j])) {
16463  POSTGIS_RT_DEBUG(4, "raster matching existing same raster found");
16464  arg->raster[i] = arg->raster[j];
16465  arg->ownsdata[i] = 0;
16466  break;
16467  }
16468  }
16469 
16470  if (arg->ownsdata[i]) {
16471  POSTGIS_RT_DEBUG(4, "deserializing raster");
16472  arg->raster[i] = rt_raster_deserialize(arg->pgraster[i], FALSE);
16473  if (arg->raster[i] == NULL) {
16474  elog(ERROR, "rtpg_nmapalgebra_rastbandarg_process: Could not deserialize raster at index %d", i);
16475  return 0;
16476  }
16477  }
16478 
16479  /* is raster empty? */
16480  arg->isempty[i] = rt_raster_is_empty(arg->raster[i]);
16481  if (arg->isempty[i]) {
16482  (*allempty)++;
16483  (*noband)++;
16484 
16485  continue;
16486  }
16487 
16488  /* second element, nband */
16489  POSTGIS_RT_DEBUG(4, "Processing second element (nband)");
16490  tupv = GetAttributeByName(tup, "nband", &isnull);
16491  if (isnull) {
16492  nband = 1;
16493  elog(NOTICE, "First argument (nband) of rastbandarg at index %d is NULL. Assuming nband = %d", i, nband);
16494  }
16495  else
16496  nband = DatumGetInt32(tupv);
16497 
16498  if (nband < 1) {
16499  elog(ERROR, "rtpg_nmapalgebra_rastbandarg_process: Band number provided for rastbandarg at index %d must be greater than zero (1-based)", i);
16500  return 0;
16501  }
16502 
16503  arg->nband[i] = nband - 1;
16504  arg->hasband[i] = rt_raster_has_band(arg->raster[i], arg->nband[i]);
16505  if (!arg->hasband[i]) {
16506  (*noband)++;
16507  POSTGIS_RT_DEBUGF(4, "Band at index %d not found in raster", nband);
16508  }
16509  }
16510 
16511  if (arg->numraster < n) {
16512  arg->pgraster = repalloc(arg->pgraster, sizeof(rt_pgraster *) * arg->numraster);
16513  arg->raster = repalloc(arg->raster, sizeof(rt_raster) * arg->numraster);
16514  arg->isempty = repalloc(arg->isempty, sizeof(uint8_t) * arg->numraster);
16515  arg->ownsdata = repalloc(arg->ownsdata, sizeof(uint8_t) * arg->numraster);
16516  arg->nband = repalloc(arg->nband, sizeof(int) * arg->numraster);
16517  arg->hasband = repalloc(arg->hasband, sizeof(uint8_t) * arg->numraster);
16518  if (
16519  arg->pgraster == NULL ||
16520  arg->raster == NULL ||
16521  arg->isempty == NULL ||
16522  arg->ownsdata == NULL ||
16523  arg->nband == NULL ||
16524  arg->hasband == NULL
16525  ) {
16526  elog(ERROR, "rtpg_nmapalgebra_rastbandarg_process: Could not reallocate memory for processed rastbandarg");
16527  return 0;
16528  }
16529  }
16530 
16531  POSTGIS_RT_DEBUGF(4, "arg->numraster = %d", arg->numraster);
16532 
16533  return 1;
16534 }
rt_raster * raster
Definition: rt_pg.c:16283
int rt_raster_has_band(rt_raster raster, int nband)
Return TRUE if the raster has a band of this number.
Definition: rt_api.c:8563
rt_pgraster ** pgraster
Definition: rt_pg.c:16282
uint8_t * ownsdata
Definition: rt_pg.c:16285
#define POSTGIS_RT_DEBUG(level, msg)
Definition: rt_pg.h:58
tuple nband
Definition: pixval.py:52
#define FALSE
Definition: dbfopen.c:169
Struct definitions.
Definition: rt_api.h:2175
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition: rt_pg.h:62
int rt_raster_is_empty(rt_raster raster)
Return TRUE if the raster is empty.
Definition: rt_api.c:8550
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
Definition: rt_api.c:8350

Here is the call graph for this function:

Here is the caller graph for this function: