1447 FuncCallContext *funcctx;
1457 if (SRF_IS_FIRSTCALL()) {
1458 MemoryContext oldcontext;
1463 int32_t bandindex = 0;
1465 bool exclude_nodata_value =
TRUE;
1466 double *search_values = NULL;
1467 uint32_t search_values_count = 0;
1483 funcctx = SRF_FIRSTCALL_INIT();
1486 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1489 if (PG_ARGISNULL(0)) {
1490 MemoryContextSwitchTo(oldcontext);
1491 SRF_RETURN_DONE(funcctx);
1493 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
1497 PG_FREE_IF_COPY(pgraster, 0);
1498 MemoryContextSwitchTo(oldcontext);
1499 elog(ERROR,
"RASTER_valueCount: Cannot deserialize raster");
1500 SRF_RETURN_DONE(funcctx);
1504 bandindex = PG_GETARG_INT32(1);
1506 if (bandindex < 1 || bandindex > num_bands) {
1507 elog(NOTICE,
"Invalid band index (must use 1-based). Returning NULL");
1509 PG_FREE_IF_COPY(pgraster, 0);
1510 MemoryContextSwitchTo(oldcontext);
1511 SRF_RETURN_DONE(funcctx);
1515 if (!PG_ARGISNULL(2))
1516 exclude_nodata_value = PG_GETARG_BOOL(2);
1519 if (!PG_ARGISNULL(3)) {
1520 array = PG_GETARG_ARRAYTYPE_P(3);
1521 etype = ARR_ELEMTYPE(array);
1522 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
1530 PG_FREE_IF_COPY(pgraster, 0);
1531 MemoryContextSwitchTo(oldcontext);
1532 elog(ERROR,
"RASTER_valueCount: Invalid data type for values");
1533 SRF_RETURN_DONE(funcctx);
1537 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
1540 search_values = palloc(
sizeof(
double) * n);
1541 for (i = 0, j = 0; i < n; i++) {
1542 if (nulls[i])
continue;
1546 search_values[j] = (double) DatumGetFloat4(e[i]);
1549 search_values[j] = (double) DatumGetFloat8(e[i]);
1556 search_values_count = j;
1559 pfree(search_values);
1560 search_values = NULL;
1565 if (!PG_ARGISNULL(4)) {
1566 roundto = PG_GETARG_FLOAT8(4);
1567 if (roundto < 0.) roundto = 0;
1573 elog(NOTICE,
"Cannot find band at index %d. Returning NULL", bandindex);
1575 PG_FREE_IF_COPY(pgraster, 0);
1576 MemoryContextSwitchTo(oldcontext);
1577 SRF_RETURN_DONE(funcctx);
1584 PG_FREE_IF_COPY(pgraster, 0);
1585 if (NULL == vcnts || !
count) {
1586 elog(NOTICE,
"Cannot count the values for band at index %d", bandindex);
1587 MemoryContextSwitchTo(oldcontext);
1588 SRF_RETURN_DONE(funcctx);
1594 funcctx->user_fctx = vcnts;
1597 funcctx->max_calls =
count;
1600 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
1602 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1604 "function returning record called in context "
1605 "that cannot accept type record"
1610 BlessTupleDesc(tupdesc);
1611 funcctx->tuple_desc = tupdesc;
1613 MemoryContextSwitchTo(oldcontext);
1617 funcctx = SRF_PERCALL_SETUP();
1619 call_cntr = funcctx->call_cntr;
1620 max_calls = funcctx->max_calls;
1621 tupdesc = funcctx->tuple_desc;
1622 vcnts2 = funcctx->user_fctx;
1625 if (call_cntr < max_calls) {
1635 values[0] = Float8GetDatum(vcnts2[call_cntr].
value);
1636 values[1] = UInt32GetDatum(vcnts2[call_cntr].
count);
1637 values[2] = Float8GetDatum(vcnts2[call_cntr].percent);
1640 tuple = heap_form_tuple(tupdesc, values, nulls);
1643 result = HeapTupleGetDatum(tuple);
1645 SRF_RETURN_NEXT(funcctx,
result);
1650 SRF_RETURN_DONE(funcctx);
char result[OUT_DOUBLE_BUFFER_SIZE]
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_valuecount rt_band_get_value_count(rt_band band, int exclude_nodata_value, double *search_values, uint32_t search_values_count, double roundto, uint32_t *rtn_total, uint32_t *rtn_count)
Count the number of times provided value(s) occur in the 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): ...
#define POSTGIS_RT_DEBUGF(level, msg,...)