PostGIS  2.5.0beta2dev-r@@SVN_REVISION@@

◆ RASTER_histogram()

Datum RASTER_histogram ( PG_FUNCTION_ARGS  )

Definition at line 915 of file rtpg_statistics.c.

References ovdump::band, genraster::count, rt_bandstats_t::count, rtpg_summarystats_arg_t::exclude_nodata_value, FALSE, FLT_EQ, PG_FUNCTION_INFO_V1(), POSTGIS_RT_DEBUG, POSTGIS_RT_DEBUGF, rtrowdump::raster, RASTER_histogramCoverage(), rt_band_destroy(), rt_band_get_histogram(), rt_band_get_summary_stats(), rt_raster_deserialize(), rt_raster_destroy(), rt_raster_get_band(), rt_raster_get_num_bands(), rtpg_summarystats_arg_t::sample, rtpg_summarystats_arg_t::stats, TRUE, and rt_bandstats_t::values.

Referenced by RASTER_summaryStats_finalfn().

916 {
917  FuncCallContext *funcctx;
918  TupleDesc tupdesc;
919 
920  int i;
921  rt_histogram hist;
922  rt_histogram hist2;
923  int call_cntr;
924  int max_calls;
925 
926  /* first call of function */
927  if (SRF_IS_FIRSTCALL()) {
928  MemoryContext oldcontext;
929 
930  rt_pgraster *pgraster = NULL;
931  rt_raster raster = NULL;
932  rt_band band = NULL;
933  int32_t bandindex = 1;
934  int num_bands = 0;
935  bool exclude_nodata_value = TRUE;
936  double sample = 0;
937  uint32_t bin_count = 0;
938  double *bin_width = NULL;
939  uint32_t bin_width_count = 0;
940  double width = 0;
941  bool right = FALSE;
942  double min = 0;
943  double max = 0;
944  rt_bandstats stats = NULL;
945  uint32_t count;
946 
947  int j;
948  int n;
949 
950  ArrayType *array;
951  Oid etype;
952  Datum *e;
953  bool *nulls;
954  int16 typlen;
955  bool typbyval;
956  char typalign;
957 
958  POSTGIS_RT_DEBUG(3, "RASTER_histogram: Starting");
959 
960  /* create a function context for cross-call persistence */
961  funcctx = SRF_FIRSTCALL_INIT();
962 
963  /* switch to memory context appropriate for multiple function calls */
964  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
965 
966  /* pgraster is null, return nothing */
967  if (PG_ARGISNULL(0)) {
968  MemoryContextSwitchTo(oldcontext);
969  SRF_RETURN_DONE(funcctx);
970  }
971  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
972 
973  raster = rt_raster_deserialize(pgraster, FALSE);
974  if (!raster) {
975  PG_FREE_IF_COPY(pgraster, 0);
976  MemoryContextSwitchTo(oldcontext);
977  elog(ERROR, "RASTER_histogram: Cannot deserialize raster");
978  SRF_RETURN_DONE(funcctx);
979  }
980 
981  /* band index is 1-based */
982  if (!PG_ARGISNULL(1))
983  bandindex = PG_GETARG_INT32(1);
984  num_bands = rt_raster_get_num_bands(raster);
985  if (bandindex < 1 || bandindex > num_bands) {
986  elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
987  rt_raster_destroy(raster);
988  PG_FREE_IF_COPY(pgraster, 0);
989  MemoryContextSwitchTo(oldcontext);
990  SRF_RETURN_DONE(funcctx);
991  }
992 
993  /* exclude_nodata_value flag */
994  if (!PG_ARGISNULL(2))
995  exclude_nodata_value = PG_GETARG_BOOL(2);
996 
997  /* sample % */
998  if (!PG_ARGISNULL(3)) {
999  sample = PG_GETARG_FLOAT8(3);
1000  if (sample < 0 || sample > 1) {
1001  elog(NOTICE, "Invalid sample percentage (must be between 0 and 1). Returning NULL");
1002  rt_raster_destroy(raster);
1003  PG_FREE_IF_COPY(pgraster, 0);
1004  MemoryContextSwitchTo(oldcontext);
1005  SRF_RETURN_DONE(funcctx);
1006  }
1007  else if (FLT_EQ(sample, 0.0))
1008  sample = 1;
1009  }
1010  else
1011  sample = 1;
1012 
1013  /* bin_count */
1014  if (!PG_ARGISNULL(4)) {
1015  bin_count = PG_GETARG_INT32(4);
1016  if (bin_count < 1) bin_count = 0;
1017  }
1018 
1019  /* bin_width */
1020  if (!PG_ARGISNULL(5)) {
1021  array = PG_GETARG_ARRAYTYPE_P(5);
1022  etype = ARR_ELEMTYPE(array);
1023  get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
1024 
1025  switch (etype) {
1026  case FLOAT4OID:
1027  case FLOAT8OID:
1028  break;
1029  default:
1030  rt_raster_destroy(raster);
1031  PG_FREE_IF_COPY(pgraster, 0);
1032  MemoryContextSwitchTo(oldcontext);
1033  elog(ERROR, "RASTER_histogram: Invalid data type for width");
1034  SRF_RETURN_DONE(funcctx);
1035  break;
1036  }
1037 
1038  deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
1039  &nulls, &n);
1040 
1041  bin_width = palloc(sizeof(double) * n);
1042  for (i = 0, j = 0; i < n; i++) {
1043  if (nulls[i]) continue;
1044 
1045  switch (etype) {
1046  case FLOAT4OID:
1047  width = (double) DatumGetFloat4(e[i]);
1048  break;
1049  case FLOAT8OID:
1050  width = (double) DatumGetFloat8(e[i]);
1051  break;
1052  }
1053 
1054  if (width < 0 || FLT_EQ(width, 0.0)) {
1055  elog(NOTICE, "Invalid value for width (must be greater than 0). Returning NULL");
1056  pfree(bin_width);
1057  rt_raster_destroy(raster);
1058  PG_FREE_IF_COPY(pgraster, 0);
1059  MemoryContextSwitchTo(oldcontext);
1060  SRF_RETURN_DONE(funcctx);
1061  }
1062 
1063  bin_width[j] = width;
1064  POSTGIS_RT_DEBUGF(5, "bin_width[%d] = %f", j, bin_width[j]);
1065  j++;
1066  }
1067  bin_width_count = j;
1068 
1069  if (j < 1) {
1070  pfree(bin_width);
1071  bin_width = NULL;
1072  }
1073  }
1074 
1075  /* right */
1076  if (!PG_ARGISNULL(6))
1077  right = PG_GETARG_BOOL(6);
1078 
1079  /* min */
1080  if (!PG_ARGISNULL(7)) min = PG_GETARG_FLOAT8(7);
1081 
1082  /* max */
1083  if (!PG_ARGISNULL(8)) max = PG_GETARG_FLOAT8(8);
1084 
1085  /* get band */
1086  band = rt_raster_get_band(raster, bandindex - 1);
1087  if (!band) {
1088  elog(NOTICE, "Cannot find band at index %d. Returning NULL", bandindex);
1089  rt_raster_destroy(raster);
1090  PG_FREE_IF_COPY(pgraster, 0);
1091  MemoryContextSwitchTo(oldcontext);
1092  SRF_RETURN_DONE(funcctx);
1093  }
1094 
1095  /* get stats */
1096  stats = rt_band_get_summary_stats(band, (int) exclude_nodata_value, sample, 1, NULL, NULL, NULL);
1097  rt_band_destroy(band);
1098  rt_raster_destroy(raster);
1099  PG_FREE_IF_COPY(pgraster, 0);
1100  if (NULL == stats || NULL == stats->values) {
1101  elog(NOTICE, "Cannot compute summary statistics for band at index %d", bandindex);
1102  MemoryContextSwitchTo(oldcontext);
1103  SRF_RETURN_DONE(funcctx);
1104  }
1105  else if (stats->count < 1) {
1106  elog(NOTICE, "Cannot compute histogram for band at index %d as the band has no values", bandindex);
1107  MemoryContextSwitchTo(oldcontext);
1108  SRF_RETURN_DONE(funcctx);
1109  }
1110 
1111  /* get histogram */
1112  hist = rt_band_get_histogram(stats, (uint32_t)bin_count, bin_width, bin_width_count, right, min, max, &count);
1113  if (bin_width_count) pfree(bin_width);
1114  pfree(stats);
1115  if (NULL == hist || !count) {
1116  elog(NOTICE, "Cannot compute histogram for band at index %d", bandindex);
1117  MemoryContextSwitchTo(oldcontext);
1118  SRF_RETURN_DONE(funcctx);
1119  }
1120 
1121  POSTGIS_RT_DEBUGF(3, "%d bins returned", count);
1122 
1123  /* Store needed information */
1124  funcctx->user_fctx = hist;
1125 
1126  /* total number of tuples to be returned */
1127  funcctx->max_calls = count;
1128 
1129  /* Build a tuple descriptor for our result type */
1130  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
1131  ereport(ERROR, (
1132  errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1133  errmsg(
1134  "function returning record called in context "
1135  "that cannot accept type record"
1136  )
1137  ));
1138  }
1139 
1140  BlessTupleDesc(tupdesc);
1141  funcctx->tuple_desc = tupdesc;
1142 
1143  MemoryContextSwitchTo(oldcontext);
1144  }
1145 
1146  /* stuff done on every call of the function */
1147  funcctx = SRF_PERCALL_SETUP();
1148 
1149  call_cntr = funcctx->call_cntr;
1150  max_calls = funcctx->max_calls;
1151  tupdesc = funcctx->tuple_desc;
1152  hist2 = funcctx->user_fctx;
1153 
1154  /* do when there is more left to send */
1155  if (call_cntr < max_calls) {
1156  int values_length = 4;
1157  Datum values[values_length];
1158  bool nulls[values_length];
1159  HeapTuple tuple;
1160  Datum result;
1161 
1162  POSTGIS_RT_DEBUGF(3, "Result %d", call_cntr);
1163 
1164  memset(nulls, FALSE, sizeof(bool) * values_length);
1165 
1166  values[0] = Float8GetDatum(hist2[call_cntr].min);
1167  values[1] = Float8GetDatum(hist2[call_cntr].max);
1168  values[2] = Int64GetDatum(hist2[call_cntr].count);
1169  values[3] = Float8GetDatum(hist2[call_cntr].percent);
1170 
1171  /* build a tuple */
1172  tuple = heap_form_tuple(tupdesc, values, nulls);
1173 
1174  /* make the tuple into a datum */
1175  result = HeapTupleGetDatum(tuple);
1176 
1177  SRF_RETURN_NEXT(funcctx, result);
1178  }
1179  /* do when there is no more left */
1180  else {
1181  pfree(hist2);
1182  SRF_RETURN_DONE(funcctx);
1183  }
1184 }
uint16_t rt_raster_get_num_bands(rt_raster raster)
Definition: rt_raster.c:372
uint32_t count
Definition: librtcore.h:2360
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
#define FLT_EQ(x, y)
Definition: librtcore.h:2234
void rt_band_destroy(rt_band band)
Destroy a raster band.
Definition: rt_band.c:340
rt_histogram rt_band_get_histogram(rt_bandstats stats, uint32_t bin_count, double *bin_widths, uint32_t bin_widths_count, int right, double min, double max, uint32_t *rtn_count)
Count the distribution of data.
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition: rtpostgis.h:65
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 count
Definition: genraster.py:56
double * values
Definition: librtcore.h:2368
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition: rt_raster.c:82
rt_bandstats rt_band_get_summary_stats(rt_band band, int exclude_nodata_value, double sample, int inc_vals, uint64_t *cK, double *cM, double *cQ)
Compute summary statistics for a band.
#define FALSE
Definition: dbfopen.c:168
Struct definitions.
Definition: librtcore.h:2250
#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:
Here is the caller graph for this function: