PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ RASTER_union_finalfn()

Datum RASTER_union_finalfn ( PG_FUNCTION_ARGS  )

Definition at line 2758 of file rtpg_mapalgebra.c.

2759 {
2760  rtpg_union_arg iwr;
2761  rt_raster _rtn = NULL;
2762  rt_raster _raster = NULL;
2763  rt_pgraster *pgraster = NULL;
2764 
2765  int i = 0;
2766  int j = 0;
2767  rt_iterator itrset = NULL;
2768  rt_band _band = NULL;
2769  int noerr = 1;
2770  int status = 0;
2771  rt_pixtype pixtype = PT_END;
2772  int hasnodata = 0;
2773  double nodataval = 0;
2774 
2775  POSTGIS_RT_DEBUG(3, "Starting...");
2776 
2777  /* cannot be called directly as this is exclusive aggregate function */
2778  if (!AggCheckCallContext(fcinfo, NULL)) {
2779  elog(ERROR, "RASTER_union_finalfn: Cannot be called in a non-aggregate context");
2780  PG_RETURN_NULL();
2781  }
2782 
2783  /* NULL, return null */
2784  if (PG_ARGISNULL(0))
2785  PG_RETURN_NULL();
2786 
2787  iwr = (rtpg_union_arg) PG_GETARG_POINTER(0);
2788 
2789  /* init itrset */
2790  itrset = palloc(sizeof(struct rt_iterator_t) * 2);
2791  if (itrset == NULL) {
2793  elog(ERROR, "RASTER_union_finalfn: Could not allocate memory for iterator arguments");
2794  PG_RETURN_NULL();
2795  }
2796 
2797  for (i = 0; i < iwr->numband; i++) {
2798  if (
2799  iwr->bandarg[i].uniontype == UT_MEAN ||
2800  iwr->bandarg[i].uniontype == UT_RANGE
2801  ) {
2802  /* raster containing the SUM or MAX is at index 1 */
2803  _band = rt_raster_get_band(iwr->bandarg[i].raster[1], 0);
2804 
2805  pixtype = rt_band_get_pixtype(_band);
2806  hasnodata = rt_band_get_hasnodata_flag(_band);
2807  if (hasnodata)
2808  rt_band_get_nodata(_band, &nodataval);
2809  POSTGIS_RT_DEBUGF(4, "(pixtype, hasnodata, nodataval) = (%s, %d, %f)", rt_pixtype_name(pixtype), hasnodata, nodataval);
2810 
2811  itrset[0].raster = iwr->bandarg[i].raster[0];
2812  itrset[0].nband = 0;
2813  itrset[1].raster = iwr->bandarg[i].raster[1];
2814  itrset[1].nband = 0;
2815 
2816  /* pass everything to iterator */
2817  if (iwr->bandarg[i].uniontype == UT_MEAN) {
2818  noerr = rt_raster_iterator(
2819  itrset, 2,
2820  ET_UNION, NULL,
2821  pixtype,
2822  hasnodata, nodataval,
2823  0, 0,
2824  NULL,
2825  NULL,
2827  &_raster
2828  );
2829  }
2830  else if (iwr->bandarg[i].uniontype == UT_RANGE) {
2831  noerr = rt_raster_iterator(
2832  itrset, 2,
2833  ET_UNION, NULL,
2834  pixtype,
2835  hasnodata, nodataval,
2836  0, 0,
2837  NULL,
2838  NULL,
2840  &_raster
2841  );
2842  }
2843 
2844  if (noerr != ES_NONE) {
2845  pfree(itrset);
2847  if (_rtn != NULL)
2848  rt_raster_destroy(_rtn);
2849  elog(ERROR, "RASTER_union_finalfn: Could not run raster iterator function");
2850  PG_RETURN_NULL();
2851  }
2852  }
2853  else {
2854  _raster = iwr->bandarg[i].raster[0];
2855  if (_raster == NULL)
2856  continue;
2857  }
2858 
2859  /* first band, _rtn doesn't exist */
2860  if (i < 1) {
2861  uint32_t bandNums[1] = {0};
2862  _rtn = rt_raster_from_band(_raster, bandNums, 1);
2863  status = (_rtn == NULL) ? -1 : 0;
2864  }
2865  else
2866  status = rt_raster_copy_band(_rtn, _raster, 0, i);
2867 
2868  POSTGIS_RT_DEBUG(4, "destroying source rasters");
2869 
2870  /* destroy source rasters */
2871  if (
2872  iwr->bandarg[i].uniontype == UT_MEAN ||
2873  iwr->bandarg[i].uniontype == UT_RANGE
2874  ) {
2875  rt_raster_destroy(_raster);
2876  }
2877 
2878  for (j = 0; j < iwr->bandarg[i].numraster; j++) {
2879  if (iwr->bandarg[i].raster[j] == NULL)
2880  continue;
2881  rt_raster_destroy(iwr->bandarg[i].raster[j]);
2882  iwr->bandarg[i].raster[j] = NULL;
2883  }
2884 
2885  if (status < 0) {
2887  rt_raster_destroy(_rtn);
2888  elog(ERROR, "RASTER_union_finalfn: Could not add band to final raster");
2889  PG_RETURN_NULL();
2890  }
2891  }
2892 
2893  /* cleanup */
2894  /* For Windowing functions, it is important to leave */
2895  /* the state intact, knowing that the aggcontext will be */
2896  /* freed by PgSQL when the statement is complete. */
2897  /* https://trac.osgeo.org/postgis/ticket/4770 */
2898  // pfree(itrset);
2899  // rtpg_union_arg_destroy(iwr);
2900 
2901  if (!_rtn) PG_RETURN_NULL();
2902 
2903  pgraster = rt_raster_serialize(_rtn);
2904  rt_raster_destroy(_rtn);
2905 
2906  POSTGIS_RT_DEBUG(3, "Finished");
2907 
2908  if (!pgraster)
2909  PG_RETURN_NULL();
2910 
2911  SET_VARSIZE(pgraster, pgraster->size);
2912  PG_RETURN_POINTER(pgraster);
2913 }
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:86
rt_pixtype
Definition: librtcore.h:187
@ PT_END
Definition: librtcore.h:199
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
Definition: rt_serialize.c:521
@ ES_NONE
Definition: librtcore.h:182
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:204
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:1463
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
Definition: rt_band.c:1887
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:1398
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
Definition: rt_raster.c:385
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:65
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition: rtpostgis.h:69
rt_raster raster
Definition: librtcore.h:2596
uint16_t nband
Definition: librtcore.h:2597
Struct definitions.
Definition: librtcore.h:2403
rtpg_union_band_arg bandarg
rtpg_union_type uniontype

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: