PostGIS  2.5.0dev-r@@SVN_REVISION@@

◆ RASTER_union_finalfn()

Datum RASTER_union_finalfn ( PG_FUNCTION_ARGS  )

Definition at line 2742 of file rtpg_mapalgebra.c.

References rtpg_union_arg_t::bandarg, ES_NONE, ET_UNION, rt_iterator_t::nband, rtpg_union_arg_t::numband, rtpg_union_band_arg_t::numraster, POSTGIS_RT_DEBUG, POSTGIS_RT_DEBUGF, PT_END, rtpg_union_band_arg_t::raster, rt_iterator_t::raster, rt_band_get_hasnodata_flag(), rt_band_get_nodata(), rt_band_get_pixtype(), rt_pixtype_name(), rt_raster_copy_band(), rt_raster_destroy(), rt_raster_from_band(), rt_raster_get_band(), rt_raster_iterator(), rt_raster_serialize(), rtpg_union_arg_destroy(), rtpg_union_mean_callback(), rtpg_union_range_callback(), rt_raster_serialized_t::size, rtpg_union_band_arg_t::uniontype, UT_MEAN, and UT_RANGE.

Referenced by RASTER_union_transfn().

2743 {
2744  rtpg_union_arg iwr;
2745  rt_raster _rtn = NULL;
2746  rt_raster _raster = NULL;
2747  rt_pgraster *pgraster = NULL;
2748 
2749  int i = 0;
2750  int j = 0;
2751  rt_iterator itrset = NULL;
2752  rt_band _band = NULL;
2753  int noerr = 1;
2754  int status = 0;
2755  rt_pixtype pixtype = PT_END;
2756  int hasnodata = 0;
2757  double nodataval = 0;
2758 
2759  POSTGIS_RT_DEBUG(3, "Starting...");
2760 
2761  /* cannot be called directly as this is exclusive aggregate function */
2762  if (!AggCheckCallContext(fcinfo, NULL)) {
2763  elog(ERROR, "RASTER_union_finalfn: Cannot be called in a non-aggregate context");
2764  PG_RETURN_NULL();
2765  }
2766 
2767  /* NULL, return null */
2768  if (PG_ARGISNULL(0))
2769  PG_RETURN_NULL();
2770 
2771  iwr = (rtpg_union_arg) PG_GETARG_POINTER(0);
2772 
2773  /* init itrset */
2774  itrset = palloc(sizeof(struct rt_iterator_t) * 2);
2775  if (itrset == NULL) {
2777  elog(ERROR, "RASTER_union_finalfn: Could not allocate memory for iterator arguments");
2778  PG_RETURN_NULL();
2779  }
2780 
2781  for (i = 0; i < iwr->numband; i++) {
2782  if (
2783  iwr->bandarg[i].uniontype == UT_MEAN ||
2784  iwr->bandarg[i].uniontype == UT_RANGE
2785  ) {
2786  /* raster containing the SUM or MAX is at index 1 */
2787  _band = rt_raster_get_band(iwr->bandarg[i].raster[1], 0);
2788 
2789  pixtype = rt_band_get_pixtype(_band);
2790  hasnodata = rt_band_get_hasnodata_flag(_band);
2791  if (hasnodata)
2792  rt_band_get_nodata(_band, &nodataval);
2793  POSTGIS_RT_DEBUGF(4, "(pixtype, hasnodata, nodataval) = (%s, %d, %f)", rt_pixtype_name(pixtype), hasnodata, nodataval);
2794 
2795  itrset[0].raster = iwr->bandarg[i].raster[0];
2796  itrset[0].nband = 0;
2797  itrset[1].raster = iwr->bandarg[i].raster[1];
2798  itrset[1].nband = 0;
2799 
2800  /* pass everything to iterator */
2801  if (iwr->bandarg[i].uniontype == UT_MEAN) {
2802  noerr = rt_raster_iterator(
2803  itrset, 2,
2804  ET_UNION, NULL,
2805  pixtype,
2806  hasnodata, nodataval,
2807  0, 0,
2808  NULL,
2809  NULL,
2811  &_raster
2812  );
2813  }
2814  else if (iwr->bandarg[i].uniontype == UT_RANGE) {
2815  noerr = rt_raster_iterator(
2816  itrset, 2,
2817  ET_UNION, NULL,
2818  pixtype,
2819  hasnodata, nodataval,
2820  0, 0,
2821  NULL,
2822  NULL,
2824  &_raster
2825  );
2826  }
2827 
2828  if (noerr != ES_NONE) {
2829  pfree(itrset);
2831  if (_rtn != NULL)
2832  rt_raster_destroy(_rtn);
2833  elog(ERROR, "RASTER_union_finalfn: Could not run raster iterator function");
2834  PG_RETURN_NULL();
2835  }
2836  }
2837  else
2838  _raster = iwr->bandarg[i].raster[0];
2839 
2840  /* first band, _rtn doesn't exist */
2841  if (i < 1) {
2842  uint32_t bandNums[1] = {0};
2843  _rtn = rt_raster_from_band(_raster, bandNums, 1);
2844  status = (_rtn == NULL) ? -1 : 0;
2845  }
2846  else
2847  status = rt_raster_copy_band(_rtn, _raster, 0, i);
2848 
2849  POSTGIS_RT_DEBUG(4, "destroying source rasters");
2850 
2851  /* destroy source rasters */
2852  if (
2853  iwr->bandarg[i].uniontype == UT_MEAN ||
2854  iwr->bandarg[i].uniontype == UT_RANGE
2855  ) {
2856  rt_raster_destroy(_raster);
2857  }
2858 
2859  for (j = 0; j < iwr->bandarg[i].numraster; j++) {
2860  if (iwr->bandarg[i].raster[j] == NULL)
2861  continue;
2862  rt_raster_destroy(iwr->bandarg[i].raster[j]);
2863  iwr->bandarg[i].raster[j] = NULL;
2864  }
2865 
2866  if (status < 0) {
2868  rt_raster_destroy(_rtn);
2869  elog(ERROR, "RASTER_union_finalfn: Could not add band to final raster");
2870  PG_RETURN_NULL();
2871  }
2872  }
2873 
2874  /* cleanup */
2875  pfree(itrset);
2877 
2878  if (!_rtn) PG_RETURN_NULL();
2879 
2880  pgraster = rt_raster_serialize(_rtn);
2881  rt_raster_destroy(_rtn);
2882 
2883  POSTGIS_RT_DEBUG(3, "Finished");
2884 
2885  if (!pgraster)
2886  PG_RETURN_NULL();
2887 
2888  SET_VARSIZE(pgraster, pgraster->size);
2889  PG_RETURN_POINTER(pgraster);
2890 }
static int rtpg_union_range_callback(rt_iterator_arg arg, void *userarg, double *value, int *nodata)
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
Definition: rt_serialize.c:521
rt_raster rt_raster_from_band(rt_raster raster, uint32_t *bandNums, int count)
Construct a new rt_raster from an existing rt_raster and an array of band numbers.
Definition: rt_raster.c:1439
rtpg_union_band_arg bandarg
rt_pixtype
Definition: librtcore.h:185
#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:1682
unsigned int uint32_t
Definition: uthash.h:78
static int rtpg_union_mean_callback(rt_iterator_arg arg, void *userarg, double *value, int *nodata)
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_raster_copy_band(rt_raster torast, rt_raster fromrast, int fromindex, int toindex)
Copy one band from one raster to another.
Definition: rt_raster.c:1374
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
Definition: rt_band.c:626
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition: rt_raster.c:82
rt_raster raster
Definition: librtcore.h:2423
uint16_t nband
Definition: librtcore.h:2424
static void rtpg_union_arg_destroy(rtpg_union_arg arg)
Struct definitions.
Definition: librtcore.h:2230
const char * rt_pixtype_name(rt_pixtype pixtype)
Definition: rt_pixel.c:110
rt_errorstate rt_raster_iterator(rt_iterator itrset, uint16_t itrcount, rt_extenttype extenttype, rt_raster customextent, rt_pixtype pixtype, uint8_t hasnodata, double nodataval, uint16_t distancex, uint16_t distancey, rt_mask mask, void *userarg, int(*callback)(rt_iterator_arg arg, void *userarg, double *value, int *nodata), rt_raster *rtnraster)
n-raster iterator.
rt_pixtype rt_band_get_pixtype(rt_band band)
Return pixeltype of this band.
Definition: rt_band.c:583
struct rtpg_union_arg_t * rtpg_union_arg
rtpg_union_type uniontype
#define POSTGIS_RT_DEBUG(level, msg)
Definition: rtpostgis.h:61
Here is the call graph for this function:
Here is the caller graph for this function: