PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ RASTER_union_finalfn()

Datum RASTER_union_finalfn ( PG_FUNCTION_ARGS  )

Definition at line 2804 of file rtpg_mapalgebra.c.

2805 {
2806  rtpg_union_arg iwr;
2807  rt_raster _rtn = NULL;
2808  rt_raster _raster = NULL;
2809  rt_pgraster *pgraster = NULL;
2810 
2811  int i = 0;
2812  int j = 0;
2813  rt_iterator itrset = NULL;
2814  rt_band _band = NULL;
2815  int noerr = 1;
2816  int status = 0;
2817  rt_pixtype pixtype = PT_END;
2818  int hasnodata = 0;
2819  double nodataval = 0;
2820 
2821  POSTGIS_RT_DEBUG(3, "Starting...");
2822 
2823  /* cannot be called directly as this is exclusive aggregate function */
2824  if (!AggCheckCallContext(fcinfo, NULL)) {
2825  elog(ERROR, "RASTER_union_finalfn: Cannot be called in a non-aggregate context");
2826  PG_RETURN_NULL();
2827  }
2828 
2829  /* NULL, return null */
2830  if (PG_ARGISNULL(0))
2831  PG_RETURN_NULL();
2832 
2833  iwr = (rtpg_union_arg) PG_GETARG_POINTER(0);
2834 
2835  /* init itrset */
2836  itrset = palloc(sizeof(struct rt_iterator_t) * 2);
2837  if (itrset == NULL) {
2839  elog(ERROR, "RASTER_union_finalfn: Could not allocate memory for iterator arguments");
2840  PG_RETURN_NULL();
2841  }
2842 
2843  for (i = 0; i < iwr->numband; i++) {
2844  if (
2845  iwr->bandarg[i].uniontype == UT_MEAN ||
2846  iwr->bandarg[i].uniontype == UT_RANGE
2847  ) {
2848  /* raster containing the SUM or MAX is at index 1 */
2849  _band = rt_raster_get_band(iwr->bandarg[i].raster[1], 0);
2850 
2851  pixtype = rt_band_get_pixtype(_band);
2852  hasnodata = rt_band_get_hasnodata_flag(_band);
2853  if (hasnodata)
2854  rt_band_get_nodata(_band, &nodataval);
2855  POSTGIS_RT_DEBUGF(4, "(pixtype, hasnodata, nodataval) = (%s, %d, %f)", rt_pixtype_name(pixtype), hasnodata, nodataval);
2856 
2857  itrset[0].raster = iwr->bandarg[i].raster[0];
2858  itrset[0].nband = 0;
2859  itrset[1].raster = iwr->bandarg[i].raster[1];
2860  itrset[1].nband = 0;
2861 
2862  /* pass everything to iterator */
2863  if (iwr->bandarg[i].uniontype == UT_MEAN) {
2864  noerr = rt_raster_iterator(
2865  itrset, 2,
2866  ET_UNION, NULL,
2867  pixtype,
2868  hasnodata, nodataval,
2869  0, 0,
2870  NULL,
2871  NULL,
2873  &_raster
2874  );
2875  }
2876  else if (iwr->bandarg[i].uniontype == UT_RANGE) {
2877  noerr = rt_raster_iterator(
2878  itrset, 2,
2879  ET_UNION, NULL,
2880  pixtype,
2881  hasnodata, nodataval,
2882  0, 0,
2883  NULL,
2884  NULL,
2886  &_raster
2887  );
2888  }
2889 
2890  if (noerr != ES_NONE) {
2891  pfree(itrset);
2893  if (_rtn != NULL)
2894  rt_raster_destroy(_rtn);
2895  elog(ERROR, "RASTER_union_finalfn: Could not run raster iterator function");
2896  PG_RETURN_NULL();
2897  }
2898  }
2899  else {
2900  _raster = iwr->bandarg[i].raster[0];
2901  if (_raster == NULL)
2902  continue;
2903  }
2904 
2905  /* first band, _rtn doesn't exist */
2906  if (i < 1) {
2907  uint32_t bandNums[1] = {0};
2908  _rtn = rt_raster_from_band(_raster, bandNums, 1);
2909  status = (_rtn == NULL) ? -1 : 0;
2910  }
2911  else
2912  status = rt_raster_copy_band(_rtn, _raster, 0, i);
2913 
2914  POSTGIS_RT_DEBUG(4, "destroying source rasters");
2915 
2916  /* destroy source rasters */
2917  if (
2918  iwr->bandarg[i].uniontype == UT_MEAN ||
2919  iwr->bandarg[i].uniontype == UT_RANGE
2920  ) {
2921  rt_raster_destroy(_raster);
2922  }
2923 
2924  for (j = 0; j < iwr->bandarg[i].numraster; j++) {
2925  if (iwr->bandarg[i].raster[j] == NULL)
2926  continue;
2927  rt_raster_destroy(iwr->bandarg[i].raster[j]);
2928  iwr->bandarg[i].raster[j] = NULL;
2929  }
2930 
2931  if (status < 0) {
2933  rt_raster_destroy(_rtn);
2934  elog(ERROR, "RASTER_union_finalfn: Could not add band to final raster");
2935  PG_RETURN_NULL();
2936  }
2937  }
2938 
2939  /* cleanup */
2940  pfree(itrset);
2942 
2943  if (!_rtn) PG_RETURN_NULL();
2944 
2945  pgraster = rt_raster_serialize(_rtn);
2946  rt_raster_destroy(_rtn);
2947 
2948  POSTGIS_RT_DEBUG(3, "Finished");
2949 
2950  if (!pgraster)
2951  PG_RETURN_NULL();
2952 
2953  SET_VARSIZE(pgraster, pgraster->size);
2954  PG_RETURN_POINTER(pgraster);
2955 }
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
Definition: rt_band.c:674
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition: rt_raster.c:82
rt_pixtype
Definition: librtcore.h:185
@ PT_END
Definition: librtcore.h:197
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
Definition: rt_serialize.c:521
@ ES_NONE
Definition: librtcore.h:180
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.
@ ET_UNION
Definition: librtcore.h:202
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:1435
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
Definition: rt_band.c:1730
rt_pixtype rt_band_get_pixtype(rt_band band)
Return pixeltype of this band.
Definition: rt_band.c:631
const char * rt_pixtype_name(rt_pixtype pixtype)
Definition: rt_pixel.c:110
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:1370
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
Definition: rt_raster.c:381
static int rtpg_union_mean_callback(rt_iterator_arg arg, void *userarg, double *value, int *nodata)
static int rtpg_union_range_callback(rt_iterator_arg arg, void *userarg, double *value, int *nodata)
static void rtpg_union_arg_destroy(rtpg_union_arg arg)
struct rtpg_union_arg_t * rtpg_union_arg
@ UT_MEAN
@ UT_RANGE
#define POSTGIS_RT_DEBUG(level, msg)
Definition: rtpostgis.h:61
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition: rtpostgis.h:65
rt_raster raster
Definition: librtcore.h:2443
uint16_t nband
Definition: librtcore.h:2444
Struct definitions.
Definition: librtcore.h:2250
rtpg_union_band_arg bandarg
rtpg_union_type uniontype
unsigned int uint32_t
Definition: uthash.h:78

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, rt_iterator_t::raster, rtpg_union_band_arg_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.

Here is the call graph for this function: