74{
78 int32_t bandindex = 1;
79 bool exclude_nodata_value =
TRUE;
80 int num_bands = 0;
81 double sample = 0;
83
84 TupleDesc tupdesc;
87 HeapTuple tuple;
88 Datum result;
89
90
91 if (PG_ARGISNULL(0))
92 PG_RETURN_NULL();
93 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
94
96 if (!raster) {
97 PG_FREE_IF_COPY(pgraster, 0);
98 elog(ERROR, "RASTER_summaryStats: Cannot deserialize raster");
99 PG_RETURN_NULL();
100 }
101
102
103 if (!PG_ARGISNULL(1))
104 bandindex = PG_GETARG_INT32(1);
106 if (bandindex < 1 || bandindex > num_bands) {
107 elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
109 PG_FREE_IF_COPY(pgraster, 0);
110 PG_RETURN_NULL();
111 }
112
113
114 if (!PG_ARGISNULL(2))
115 exclude_nodata_value = PG_GETARG_BOOL(2);
116
117
118 if (!PG_ARGISNULL(3)) {
119 sample = PG_GETARG_FLOAT8(3);
120 if (sample < 0 || sample > 1) {
121 elog(NOTICE, "Invalid sample percentage (must be between 0 and 1). Returning NULL");
123 PG_FREE_IF_COPY(pgraster, 0);
124 PG_RETURN_NULL();
125 }
126 else if (
FLT_EQ(sample, 0.0))
127 sample = 1;
128 }
129 else
130 sample = 1;
131
132
134 if (!band) {
135 elog(NOTICE, "Cannot find band at index %d. Returning NULL", bandindex);
137 PG_FREE_IF_COPY(pgraster, 0);
138 PG_RETURN_NULL();
139 }
140
141
145 PG_FREE_IF_COPY(pgraster, 0);
146 if (NULL == stats) {
147 elog(NOTICE, "Cannot compute summary statistics for band at index %d. Returning NULL", bandindex);
148 PG_RETURN_NULL();
149 }
150
151
152 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
153 ereport(ERROR, (
154 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
155 errmsg(
156 "function returning record called in context "
157 "that cannot accept type record"
158 )
159 ));
160 }
161
162 BlessTupleDesc(tupdesc);
163
165
166 values[0] = Int64GetDatum(stats->
count);
167 if (stats->
count > 0) {
168 values[1] = Float8GetDatum(stats->
sum);
169 values[2] = Float8GetDatum(stats->
mean);
170 values[3] = Float8GetDatum(stats->
stddev);
171 values[4] = Float8GetDatum(stats->
min);
172 values[5] = Float8GetDatum(stats->
max);
173 }
174 else {
180 }
181
182
183 tuple = heap_form_tuple(tupdesc, values, nulls);
184
185
186 result = HeapTupleGetDatum(tuple);
187
188
189 pfree(stats);
190
191 PG_RETURN_DATUM(result);
192}
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
void rt_band_destroy(rt_band band)
Destroy a raster band.
uint16_t rt_raster_get_num_bands(rt_raster raster)
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.
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...