1197 FuncCallContext *funcctx;
1209 if (SRF_IS_FIRSTCALL()) {
1210 MemoryContext oldcontext;
1212 text *tablenametext = NULL;
1213 char *tablename = NULL;
1214 text *colnametext = NULL;
1215 char *colname = NULL;
1216 int32_t bandindex = 1;
1217 bool exclude_nodata_value =
TRUE;
1219 uint32_t bin_count = 0;
1220 double *bin_width = NULL;
1221 uint32_t bin_width_count = 0;
1235 bool isNull =
FALSE;
1259 funcctx = SRF_FIRSTCALL_INIT();
1262 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1265 if (PG_ARGISNULL(0)) {
1266 elog(NOTICE,
"Table name must be provided");
1267 MemoryContextSwitchTo(oldcontext);
1268 SRF_RETURN_DONE(funcctx);
1270 tablenametext = PG_GETARG_TEXT_P(0);
1272 if (!strlen(tablename)) {
1273 elog(NOTICE,
"Table name must be provided");
1274 MemoryContextSwitchTo(oldcontext);
1275 SRF_RETURN_DONE(funcctx);
1280 if (PG_ARGISNULL(1)) {
1281 elog(NOTICE,
"Column name must be provided");
1282 MemoryContextSwitchTo(oldcontext);
1283 SRF_RETURN_DONE(funcctx);
1285 colnametext = PG_GETARG_TEXT_P(1);
1287 if (!strlen(colname)) {
1288 elog(NOTICE,
"Column name must be provided");
1289 MemoryContextSwitchTo(oldcontext);
1290 SRF_RETURN_DONE(funcctx);
1295 if (!PG_ARGISNULL(2))
1296 bandindex = PG_GETARG_INT32(2);
1299 if (!PG_ARGISNULL(3))
1300 exclude_nodata_value = PG_GETARG_BOOL(3);
1303 if (!PG_ARGISNULL(4)) {
1304 sample = PG_GETARG_FLOAT8(4);
1305 if (sample < 0 || sample > 1) {
1306 elog(NOTICE,
"Invalid sample percentage (must be between 0 and 1). Returning NULL");
1307 MemoryContextSwitchTo(oldcontext);
1308 SRF_RETURN_DONE(funcctx);
1310 else if (
FLT_EQ(sample, 0.0))
1317 if (!PG_ARGISNULL(5)) {
1318 bin_count = PG_GETARG_INT32(5);
1319 if (bin_count < 1) bin_count = 0;
1323 if (!PG_ARGISNULL(6)) {
1324 array = PG_GETARG_ARRAYTYPE_P(6);
1325 etype = ARR_ELEMTYPE(array);
1326 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
1333 MemoryContextSwitchTo(oldcontext);
1334 elog(ERROR,
"RASTER_histogramCoverage: Invalid data type for width");
1335 SRF_RETURN_DONE(funcctx);
1339 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
1342 bin_width = palloc(
sizeof(
double) * n);
1343 for (i = 0, j = 0; i < (uint32_t) n; i++) {
1344 if (nulls[i])
continue;
1348 width = (double) DatumGetFloat4(e[i]);
1351 width = (double) DatumGetFloat8(e[i]);
1355 if (width < 0 ||
FLT_EQ(width, 0.0)) {
1356 elog(NOTICE,
"Invalid value for width (must be greater than 0). Returning NULL");
1358 MemoryContextSwitchTo(oldcontext);
1359 SRF_RETURN_DONE(funcctx);
1362 bin_width[j] = width;
1366 bin_width_count = j;
1375 if (!PG_ARGISNULL(7))
1376 right = PG_GETARG_BOOL(7);
1379 spi_result = SPI_connect();
1380 if (spi_result != SPI_OK_CONNECT) {
1382 if (bin_width_count) pfree(bin_width);
1384 MemoryContextSwitchTo(oldcontext);
1385 elog(ERROR,
"RASTER_histogramCoverage: Cannot connect to database using SPI");
1386 SRF_RETURN_DONE(funcctx);
1390 len =
sizeof(char) * (strlen(
"SELECT min, max FROM _st_summarystats('','',,::boolean,)") + strlen(tablename) + strlen(colname) + (
MAX_INT_CHARLEN * 2) +
MAX_DBL_CHARLEN + 1);
1391 sql = (
char *) palloc(len);
1394 if (SPI_tuptable) SPI_freetuptable(SPI_tuptable);
1397 if (bin_width_count) pfree(bin_width);
1399 MemoryContextSwitchTo(oldcontext);
1400 elog(ERROR,
"RASTER_histogramCoverage: Cannot allocate memory for sql");
1401 SRF_RETURN_DONE(funcctx);
1405 snprintf(
sql, len,
"SELECT min, max FROM _st_summarystats('%s','%s',%d,%d::boolean,%f)", tablename, colname, bandindex, (exclude_nodata_value ? 1 : 0), sample);
1407 spi_result = SPI_execute(
sql,
TRUE, 0);
1409 if (spi_result != SPI_OK_SELECT || SPI_tuptable == NULL || SPI_processed != 1) {
1411 if (SPI_tuptable) SPI_freetuptable(SPI_tuptable);
1414 if (bin_width_count) pfree(bin_width);
1416 MemoryContextSwitchTo(oldcontext);
1417 elog(ERROR,
"RASTER_histogramCoverage: Cannot get summary stats of coverage");
1418 SRF_RETURN_DONE(funcctx);
1421 tupdesc = SPI_tuptable->tupdesc;
1422 tuple = SPI_tuptable->vals[0];
1424 tmp = SPI_getvalue(tuple, tupdesc, 1);
1425 if (NULL == tmp || !strlen(tmp)) {
1427 SPI_freetuptable(SPI_tuptable);
1430 if (bin_width_count) pfree(bin_width);
1432 MemoryContextSwitchTo(oldcontext);
1433 elog(ERROR,
"RASTER_histogramCoverage: Cannot get summary stats of coverage");
1434 SRF_RETURN_DONE(funcctx);
1436 min = strtod(tmp, NULL);
1440 tmp = SPI_getvalue(tuple, tupdesc, 2);
1441 if (NULL == tmp || !strlen(tmp)) {
1443 if (SPI_tuptable) SPI_freetuptable(SPI_tuptable);
1446 if (bin_width_count) pfree(bin_width);
1448 MemoryContextSwitchTo(oldcontext);
1449 elog(ERROR,
"RASTER_histogramCoverage: Cannot get summary stats of coverage");
1450 SRF_RETURN_DONE(funcctx);
1452 max = strtod(tmp, NULL);
1458 len =
sizeof(char) * (strlen(
"SELECT \"\" FROM \"\" WHERE \"\" IS NOT NULL") + (strlen(colname) * 2) + strlen(tablename) + 1);
1459 sql = (
char *) palloc(len);
1462 if (SPI_tuptable) SPI_freetuptable(SPI_tuptable);
1465 if (bin_width_count) pfree(bin_width);
1467 MemoryContextSwitchTo(oldcontext);
1468 elog(ERROR,
"RASTER_histogramCoverage: Cannot allocate memory for sql");
1469 SRF_RETURN_DONE(funcctx);
1473 snprintf(
sql, len,
"SELECT \"%s\" FROM \"%s\" WHERE \"%s\" IS NOT NULL", colname, tablename, colname);
1475 portal = SPI_cursor_open_with_args(
1485 SPI_cursor_fetch(portal,
TRUE, 1);
1486 while (SPI_processed == 1 && SPI_tuptable != NULL) {
1487 tupdesc = SPI_tuptable->tupdesc;
1488 tuple = SPI_tuptable->vals[0];
1490 datum = SPI_getbinval(tuple, tupdesc, 1, &isNull);
1491 if (SPI_result == SPI_ERROR_NOATTRIBUTE) {
1492 SPI_freetuptable(SPI_tuptable);
1493 SPI_cursor_close(portal);
1496 if (NULL != covhist) pfree(covhist);
1497 if (bin_width_count) pfree(bin_width);
1499 MemoryContextSwitchTo(oldcontext);
1500 elog(ERROR,
"RASTER_histogramCoverage: Cannot get raster of coverage");
1501 SRF_RETURN_DONE(funcctx);
1504 SPI_cursor_fetch(portal,
TRUE, 1);
1508 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(datum);
1513 if (SPI_tuptable) SPI_freetuptable(SPI_tuptable);
1514 SPI_cursor_close(portal);
1517 if (NULL != covhist) pfree(covhist);
1518 if (bin_width_count) pfree(bin_width);
1520 MemoryContextSwitchTo(oldcontext);
1521 elog(ERROR,
"RASTER_histogramCoverage: Cannot deserialize raster");
1522 SRF_RETURN_DONE(funcctx);
1527 if (bandindex < 1 || bandindex > num_bands) {
1528 elog(NOTICE,
"Invalid band index (must use 1-based). Returning NULL");
1532 if (SPI_tuptable) SPI_freetuptable(SPI_tuptable);
1533 SPI_cursor_close(portal);
1536 if (NULL != covhist) pfree(covhist);
1537 if (bin_width_count) pfree(bin_width);
1539 MemoryContextSwitchTo(oldcontext);
1540 SRF_RETURN_DONE(funcctx);
1546 elog(NOTICE,
"Cannot find band at index %d. Returning NULL", bandindex);
1550 if (SPI_tuptable) SPI_freetuptable(SPI_tuptable);
1551 SPI_cursor_close(portal);
1554 if (NULL != covhist) pfree(covhist);
1555 if (bin_width_count) pfree(bin_width);
1557 MemoryContextSwitchTo(oldcontext);
1558 SRF_RETURN_DONE(funcctx);
1567 if (NULL == stats) {
1568 elog(NOTICE,
"Cannot compute summary statistics for band at index %d. Returning NULL", bandindex);
1570 if (SPI_tuptable) SPI_freetuptable(SPI_tuptable);
1571 SPI_cursor_close(portal);
1574 if (NULL != covhist) pfree(covhist);
1575 if (bin_width_count) pfree(bin_width);
1577 MemoryContextSwitchTo(oldcontext);
1578 SRF_RETURN_DONE(funcctx);
1582 if (stats->
count > 0) {
1585 if (NULL == hist || !
count) {
1586 elog(NOTICE,
"Cannot compute histogram for band at index %d", bandindex);
1588 if (SPI_tuptable) SPI_freetuptable(SPI_tuptable);
1589 SPI_cursor_close(portal);
1592 if (NULL != covhist) pfree(covhist);
1593 if (bin_width_count) pfree(bin_width);
1595 MemoryContextSwitchTo(oldcontext);
1596 SRF_RETURN_DONE(funcctx);
1602 if (NULL == covhist) {
1604 if (NULL == covhist) {
1607 if (SPI_tuptable) SPI_freetuptable(SPI_tuptable);
1608 SPI_cursor_close(portal);
1611 if (bin_width_count) pfree(bin_width);
1613 MemoryContextSwitchTo(oldcontext);
1614 elog(ERROR,
"RASTER_histogramCoverage: Cannot allocate memory for histogram of coverage");
1615 SRF_RETURN_DONE(funcctx);
1618 for (i = 0; i <
count; i++) {
1619 sum += hist[i].
count;
1622 covhist[i].
min = hist[i].
min;
1623 covhist[i].
max = hist[i].
max;
1627 for (i = 0; i <
count; i++) {
1628 sum += hist[i].
count;
1636 if (bin_count <= 0) bin_count =
count;
1640 SPI_cursor_fetch(portal,
TRUE, 1);
1643 if (SPI_tuptable) SPI_freetuptable(SPI_tuptable);
1644 SPI_cursor_close(portal);
1647 if (bin_width_count) pfree(bin_width);
1651 for (i = 0; i <
count; i++)
1652 covhist[i].percent = covhist[i].
count / (
double) sum;
1656 funcctx->user_fctx = covhist;
1659 funcctx->max_calls =
count;
1662 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
1664 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1666 "function returning record called in context "
1667 "that cannot accept type record"
1672 BlessTupleDesc(tupdesc);
1673 funcctx->tuple_desc = tupdesc;
1675 MemoryContextSwitchTo(oldcontext);
1679 funcctx = SRF_PERCALL_SETUP();
1681 call_cntr = funcctx->call_cntr;
1682 max_calls = funcctx->max_calls;
1683 tupdesc = funcctx->tuple_desc;
1684 covhist2 = funcctx->user_fctx;
1687 if (call_cntr < max_calls) {
1697 values[0] = Float8GetDatum(covhist2[call_cntr].min);
1698 values[1] = Float8GetDatum(covhist2[call_cntr].max);
1699 values[2] = Int64GetDatum(covhist2[call_cntr].
count);
1700 values[3] = Float8GetDatum(covhist2[call_cntr].percent);
1703 tuple = heap_form_tuple(tupdesc, values, nulls);
1706 result = HeapTupleGetDatum(tuple);
1708 SRF_RETURN_NEXT(funcctx, result);
1713 SRF_RETURN_DONE(funcctx);
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_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.
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.
struct rt_histogram_t * rt_histogram
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): ...
char * text_to_cstring(const text *textptr)
#define POSTGIS_RT_DEBUG(level, msg)
#define POSTGIS_RT_DEBUGF(level, msg,...)