198 text *tablenametext = NULL;
199 char *tablename = NULL;
200 text *colnametext = NULL;
201 char *colname = NULL;
202 int32_t bandindex = 1;
203 bool exclude_nodata_value =
TRUE;
211 SPITupleTable *tuptable = NULL;
231 if (PG_ARGISNULL(0)) {
232 elog(NOTICE,
"Table name must be provided");
235 tablenametext = PG_GETARG_TEXT_P(0);
236 tablename = text_to_cstring(tablenametext);
237 if (!strlen(tablename)) {
238 elog(NOTICE,
"Table name must be provided");
243 if (PG_ARGISNULL(1)) {
244 elog(NOTICE,
"Column name must be provided");
247 colnametext = PG_GETARG_TEXT_P(1);
248 colname = text_to_cstring(colnametext);
249 if (!strlen(colname)) {
250 elog(NOTICE,
"Column name must be provided");
255 if (!PG_ARGISNULL(2))
256 bandindex = PG_GETARG_INT32(2);
259 if (!PG_ARGISNULL(3))
260 exclude_nodata_value = PG_GETARG_BOOL(3);
263 if (!PG_ARGISNULL(4)) {
264 sample = PG_GETARG_FLOAT8(4);
265 if (sample < 0 || sample > 1) {
266 elog(NOTICE,
"Invalid sample percentage (must be between 0 and 1). Returning NULL");
270 else if (
FLT_EQ(sample, 0.0))
278 spi_result = SPI_connect();
279 if (spi_result != SPI_OK_CONNECT) {
281 elog(ERROR,
"RASTER_summaryStatsCoverage: Cannot connect to database using SPI");
286 len =
sizeof(char) * (strlen(
"SELECT \"\" FROM \"\" WHERE \"\" IS NOT NULL") + (strlen(colname) * 2) + strlen(tablename) + 1);
287 sql = (
char *) palloc(len);
289 if (SPI_tuptable) SPI_freetuptable(tuptable);
291 elog(ERROR,
"RASTER_summaryStatsCoverage: Cannot allocate memory for sql");
296 snprintf(
sql, len,
"SELECT \"%s\" FROM \"%s\" WHERE \"%s\" IS NOT NULL", colname, tablename, colname);
297 portal = SPI_cursor_open_with_args(
307 SPI_cursor_fetch(portal,
TRUE, 1);
308 while (SPI_processed == 1 && SPI_tuptable != NULL) {
309 tupdesc = SPI_tuptable->tupdesc;
310 tuple = SPI_tuptable->vals[0];
312 datum = SPI_getbinval(tuple, tupdesc, 1, &isNull);
313 if (SPI_result == SPI_ERROR_NOATTRIBUTE) {
314 SPI_freetuptable(SPI_tuptable);
315 SPI_cursor_close(portal);
318 if (NULL != rtn) pfree(rtn);
319 elog(ERROR,
"RASTER_summaryStatsCoverage: Cannot get raster of coverage");
323 SPI_cursor_fetch(portal,
TRUE, 1);
327 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(datum);
331 SPI_freetuptable(SPI_tuptable);
332 SPI_cursor_close(portal);
335 if (NULL != rtn) pfree(rtn);
336 elog(ERROR,
"RASTER_summaryStatsCoverage: Cannot deserialize raster");
342 if (bandindex < 1 || bandindex > num_bands) {
343 elog(NOTICE,
"Invalid band index (must use 1-based). Returning NULL");
347 SPI_freetuptable(SPI_tuptable);
348 SPI_cursor_close(portal);
351 if (NULL != rtn) pfree(rtn);
358 elog(NOTICE,
"Cannot find band at index %d. Returning NULL", bandindex);
362 SPI_freetuptable(SPI_tuptable);
363 SPI_cursor_close(portal);
366 if (NULL != rtn) pfree(rtn);
377 elog(NOTICE,
"Cannot compute summary statistics for band at index %d. Returning NULL", bandindex);
379 SPI_freetuptable(SPI_tuptable);
380 SPI_cursor_close(portal);
383 if (NULL != rtn) pfree(rtn);
388 if (stats->
count > 0) {
392 SPI_freetuptable(SPI_tuptable);
393 SPI_cursor_close(portal);
396 elog(ERROR,
"RASTER_summaryStatsCoverage: Cannot allocate memory for summary stats of coverage");
415 if (stats->
min < rtn->
min)
417 if (stats->
max > rtn->
max)
425 SPI_cursor_fetch(portal,
TRUE, 1);
428 if (SPI_tuptable) SPI_freetuptable(SPI_tuptable);
429 SPI_cursor_close(portal);
433 elog(ERROR,
"RASTER_summaryStatsCoverage: Cannot compute coverage summary stats");
447 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
449 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
451 "function returning record called in context "
452 "that cannot accept type record"
457 BlessTupleDesc(tupdesc);
461 values[0] = Int64GetDatum(rtn->
count);
462 if (rtn->
count > 0) {
463 values[1] = Float8GetDatum(rtn->
sum);
464 values[2] = Float8GetDatum(rtn->
mean);
465 values[3] = Float8GetDatum(rtn->
stddev);
466 values[4] = Float8GetDatum(rtn->
min);
467 values[5] = Float8GetDatum(rtn->
max);
478 tuple = heap_form_tuple(tupdesc, values, nulls);
481 result = HeapTupleGetDatum(tuple);
char result[OUT_DOUBLE_BUFFER_SIZE]
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
struct rt_bandstats_t * rt_bandstats
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): ...