PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ RASTER_bandmetadata()

Datum RASTER_bandmetadata ( PG_FUNCTION_ARGS  )

Definition at line 349 of file rtpg_band_properties.c.

References ovdump::band, FALSE, PG_FUNCTION_INFO_V1(), POSTGIS_RT_DEBUG, POSTGIS_RT_DEBUGF, rtrowdump::raster, RASTER_setBandNoDataValue(), rt_band_destroy(), rt_band_get_ext_path(), rt_band_get_hasnodata_flag(), rt_band_get_nodata(), rt_band_get_pixtype(), rt_pixtype_name(), rt_raster_deserialize(), rt_raster_destroy(), rt_raster_get_band(), rt_raster_get_num_bands(), TRUE, and VALUES_LENGTH.

350 {
351  FuncCallContext *funcctx;
352  TupleDesc tupdesc;
353  int call_cntr;
354  int max_calls;
355 
356  struct bandmetadata {
357  uint32_t bandnum;
358  char *pixeltype;
359  bool hasnodata;
360  double nodataval;
361  bool isoutdb;
362  char *bandpath;
363  };
364  struct bandmetadata *bmd = NULL;
365  struct bandmetadata *bmd2 = NULL;
366 
367  HeapTuple tuple;
368  Datum result;
369 
370  if (SRF_IS_FIRSTCALL()) {
371  MemoryContext oldcontext;
372 
373  rt_pgraster *pgraster = NULL;
374  rt_raster raster = NULL;
375  rt_band band = NULL;
376 
377  ArrayType *array;
378  Oid etype;
379  Datum *e;
380  bool *nulls;
381  int16 typlen;
382  bool typbyval;
383  char typalign;
384  int i = 0;
385  int j = 0;
386  int n = 0;
387 
388  uint32_t numBands;
389  uint32_t idx = 1;
390  uint32_t *bandNums = NULL;
391  const char *tmp = NULL;
392 
393  POSTGIS_RT_DEBUG(3, "RASTER_bandmetadata: Starting");
394 
395  /* create a function context for cross-call persistence */
396  funcctx = SRF_FIRSTCALL_INIT();
397 
398  /* switch to memory context appropriate for multiple function calls */
399  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
400 
401  /* pgraster is null, return null */
402  if (PG_ARGISNULL(0)) {
403  MemoryContextSwitchTo(oldcontext);
404  SRF_RETURN_DONE(funcctx);
405  }
406  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
407 
408  /* raster */
409  raster = rt_raster_deserialize(pgraster, FALSE);
410  if (!raster) {
411  PG_FREE_IF_COPY(pgraster, 0);
412  MemoryContextSwitchTo(oldcontext);
413  elog(ERROR, "RASTER_bandmetadata: Could not deserialize raster");
414  SRF_RETURN_DONE(funcctx);
415  }
416 
417  /* numbands */
418  numBands = rt_raster_get_num_bands(raster);
419  if (numBands < 1) {
420  elog(NOTICE, "Raster provided has no bands");
421  rt_raster_destroy(raster);
422  PG_FREE_IF_COPY(pgraster, 0);
423  MemoryContextSwitchTo(oldcontext);
424  SRF_RETURN_DONE(funcctx);
425  }
426 
427  /* band index */
428  array = PG_GETARG_ARRAYTYPE_P(1);
429  etype = ARR_ELEMTYPE(array);
430  get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
431 
432  switch (etype) {
433  case INT2OID:
434  case INT4OID:
435  break;
436  default:
437  rt_raster_destroy(raster);
438  PG_FREE_IF_COPY(pgraster, 0);
439  MemoryContextSwitchTo(oldcontext);
440  elog(ERROR, "RASTER_bandmetadata: Invalid data type for band number(s)");
441  SRF_RETURN_DONE(funcctx);
442  break;
443  }
444 
445  deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
446  &nulls, &n);
447 
448  bandNums = palloc(sizeof(uint32_t) * n);
449  for (i = 0, j = 0; i < n; i++) {
450  if (nulls[i]) continue;
451 
452  switch (etype) {
453  case INT2OID:
454  idx = (uint32_t) DatumGetInt16(e[i]);
455  break;
456  case INT4OID:
457  idx = (uint32_t) DatumGetInt32(e[i]);
458  break;
459  }
460 
461  POSTGIS_RT_DEBUGF(3, "band idx (before): %d", idx);
462  if (idx > numBands || idx < 1) {
463  elog(NOTICE, "Invalid band index: %d. Indices must be 1-based. Returning NULL", idx);
464  pfree(bandNums);
465  rt_raster_destroy(raster);
466  PG_FREE_IF_COPY(pgraster, 0);
467  MemoryContextSwitchTo(oldcontext);
468  SRF_RETURN_DONE(funcctx);
469  }
470 
471  bandNums[j] = idx;
472  POSTGIS_RT_DEBUGF(3, "bandNums[%d] = %d", j, bandNums[j]);
473  j++;
474  }
475 
476  if (j < 1) {
477  j = numBands;
478  bandNums = repalloc(bandNums, sizeof(uint32_t) * j);
479  for (i = 0; i < j; i++)
480  bandNums[i] = i + 1;
481  }
482  else if (j < n)
483  bandNums = repalloc(bandNums, sizeof(uint32_t) * j);
484 
485  bmd = (struct bandmetadata *) palloc(sizeof(struct bandmetadata) * j);
486 
487  for (i = 0; i < j; i++) {
488  band = rt_raster_get_band(raster, bandNums[i] - 1);
489  if (NULL == band) {
490  elog(NOTICE, "Could not get raster band at index %d", bandNums[i]);
491  rt_raster_destroy(raster);
492  PG_FREE_IF_COPY(pgraster, 0);
493  MemoryContextSwitchTo(oldcontext);
494  SRF_RETURN_DONE(funcctx);
495  }
496 
497  /* bandnum */
498  bmd[i].bandnum = bandNums[i];
499 
500  /* pixeltype */
502  bmd[i].pixeltype = palloc(sizeof(char) * (strlen(tmp) + 1));
503  strncpy(bmd[i].pixeltype, tmp, strlen(tmp) + 1);
504 
505  /* hasnodatavalue */
506  if (rt_band_get_hasnodata_flag(band))
507  bmd[i].hasnodata = TRUE;
508  else
509  bmd[i].hasnodata = FALSE;
510 
511  /* nodatavalue */
512  if (bmd[i].hasnodata)
513  rt_band_get_nodata(band, &(bmd[i].nodataval));
514  else
515  bmd[i].nodataval = 0;
516 
517  /* path */
518  tmp = rt_band_get_ext_path(band);
519  if (tmp) {
520  bmd[i].bandpath = palloc(sizeof(char) * (strlen(tmp) + 1));
521  strncpy(bmd[i].bandpath, tmp, strlen(tmp) + 1);
522  }
523  else
524  bmd[i].bandpath = NULL;
525 
526  /* isoutdb */
527  bmd[i].isoutdb = bmd[i].bandpath ? TRUE : FALSE;
528 
529  rt_band_destroy(band);
530  }
531 
532  rt_raster_destroy(raster);
533  PG_FREE_IF_COPY(pgraster, 0);
534 
535  /* Store needed information */
536  funcctx->user_fctx = bmd;
537 
538  /* total number of tuples to be returned */
539  funcctx->max_calls = j;
540 
541  /* Build a tuple descriptor for our result type */
542  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
543  MemoryContextSwitchTo(oldcontext);
544  ereport(ERROR, (
545  errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
546  errmsg(
547  "function returning record called in context "
548  "that cannot accept type record"
549  )
550  ));
551  }
552 
553  BlessTupleDesc(tupdesc);
554  funcctx->tuple_desc = tupdesc;
555 
556  MemoryContextSwitchTo(oldcontext);
557  }
558 
559  /* stuff done on every call of the function */
560  funcctx = SRF_PERCALL_SETUP();
561 
562  call_cntr = funcctx->call_cntr;
563  max_calls = funcctx->max_calls;
564  tupdesc = funcctx->tuple_desc;
565  bmd2 = funcctx->user_fctx;
566 
567  /* do when there is more left to send */
568  if (call_cntr < max_calls) {
569  Datum values[VALUES_LENGTH];
570  bool nulls[VALUES_LENGTH];
571 
572  memset(nulls, FALSE, sizeof(bool) * VALUES_LENGTH);
573 
574  values[0] = UInt32GetDatum(bmd2[call_cntr].bandnum);
575  values[1] = CStringGetTextDatum(bmd2[call_cntr].pixeltype);
576 
577  if (bmd2[call_cntr].hasnodata)
578  values[2] = Float8GetDatum(bmd2[call_cntr].nodataval);
579  else
580  nulls[2] = TRUE;
581 
582  values[3] = BoolGetDatum(bmd2[call_cntr].isoutdb);
583  if (bmd2[call_cntr].bandpath && strlen(bmd2[call_cntr].bandpath))
584  values[4] = CStringGetTextDatum(bmd2[call_cntr].bandpath);
585  else
586  nulls[4] = TRUE;
587 
588  /* build a tuple */
589  tuple = heap_form_tuple(tupdesc, values, nulls);
590 
591  /* make the tuple into a datum */
592  result = HeapTupleGetDatum(tuple);
593 
594  /* clean up */
595  pfree(bmd2[call_cntr].pixeltype);
596  if (bmd2[call_cntr].bandpath) pfree(bmd2[call_cntr].bandpath);
597 
598  SRF_RETURN_NEXT(funcctx, result);
599  }
600  /* do when there is no more left */
601  else {
602  pfree(bmd2);
603  SRF_RETURN_DONE(funcctx);
604  }
605 }
int rt_raster_get_num_bands(rt_raster raster)
Definition: rt_raster.c:372
raster
Be careful!! Zeros function&#39;s input parameter can be a (height x width) array, not (width x height): ...
Definition: rtrowdump.py:121
band
Definition: ovdump.py:57
void rt_band_destroy(rt_band band)
Destroy a raster band.
Definition: rt_band.c:242
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition: rtpostgis.h:65
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
Definition: rt_band.c:1597
const char * rt_band_get_ext_path(rt_band band)
Return band&#39;s external path (only valid when rt_band_is_offline returns non-zero).
Definition: rt_band.c:265
unsigned int uint32_t
Definition: uthash.h:78
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
Definition: rt_raster.c:381
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
Definition: rt_band.c:541
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition: rt_raster.c:82
#define VALUES_LENGTH
#define FALSE
Definition: dbfopen.c:168
Struct definitions.
Definition: librtcore.h:2201
const char * rt_pixtype_name(rt_pixtype pixtype)
Definition: rt_pixel.c:110
rt_pixtype rt_band_get_pixtype(rt_band band)
Return pixeltype of this band.
Definition: rt_band.c:498
#define POSTGIS_RT_DEBUG(level, msg)
Definition: rtpostgis.h:61
#define TRUE
Definition: dbfopen.c:169
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
Definition: rt_serialize.c:717
Here is the call graph for this function: