1446 {
1447 FuncCallContext *funcctx;
1448 TupleDesc tupdesc;
1449
1450 int i;
1453 int call_cntr;
1454 int max_calls;
1455
1456
1457 if (SRF_IS_FIRSTCALL()) {
1458 MemoryContext oldcontext;
1459
1463 int32_t bandindex = 0;
1464 int num_bands = 0;
1465 bool exclude_nodata_value =
TRUE;
1466 double *search_values = NULL;
1467 uint32_t search_values_count = 0;
1468 double roundto = 0;
1470
1471 int j;
1472 int n;
1473
1474 ArrayType *array;
1475 Oid etype;
1476 Datum *e;
1477 bool *nulls;
1478 int16 typlen;
1479 bool typbyval;
1480 char typalign;
1481
1482
1483 funcctx = SRF_FIRSTCALL_INIT();
1484
1485
1486 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1487
1488
1489 if (PG_ARGISNULL(0)) {
1490 MemoryContextSwitchTo(oldcontext);
1491 SRF_RETURN_DONE(funcctx);
1492 }
1493 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
1494
1496 if (!raster) {
1497 PG_FREE_IF_COPY(pgraster, 0);
1498 MemoryContextSwitchTo(oldcontext);
1499 elog(ERROR, "RASTER_valueCount: Cannot deserialize raster");
1500 SRF_RETURN_DONE(funcctx);
1501 }
1502
1503
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);
1512 }
1513
1514
1515 if (!PG_ARGISNULL(2))
1516 exclude_nodata_value = PG_GETARG_BOOL(2);
1517
1518
1519 if (!PG_ARGISNULL(3)) {
1520 array = PG_GETARG_ARRAYTYPE_P(3);
1521 etype = ARR_ELEMTYPE(array);
1522 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
1523
1524 switch (etype) {
1525 case FLOAT4OID:
1526 case FLOAT8OID:
1527 break;
1528 default:
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);
1534 break;
1535 }
1536
1537 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
1538 &nulls, &n);
1539
1540 search_values = palloc(sizeof(double) * n);
1541 for (i = 0, j = 0; i < n; i++) {
1542 if (nulls[i]) continue;
1543
1544 switch (etype) {
1545 case FLOAT4OID:
1546 search_values[j] = (double) DatumGetFloat4(e[i]);
1547 break;
1548 case FLOAT8OID:
1549 search_values[j] = (double) DatumGetFloat8(e[i]);
1550 break;
1551 }
1552
1554 j++;
1555 }
1556 search_values_count = j;
1557
1558 if (j < 1) {
1559 pfree(search_values);
1560 search_values = NULL;
1561 }
1562 }
1563
1564
1565 if (!PG_ARGISNULL(4)) {
1566 roundto = PG_GETARG_FLOAT8(4);
1567 if (roundto < 0.) roundto = 0;
1568 }
1569
1570
1572 if (!band) {
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);
1578 }
1579
1580
1581 vcnts =
rt_band_get_value_count(band, (
int) exclude_nodata_value, search_values, search_values_count, roundto, NULL, &count);
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);
1589 }
1590
1592
1593
1594 funcctx->user_fctx = vcnts;
1595
1596
1597 funcctx->max_calls =
count;
1598
1599
1600 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
1601 ereport(ERROR, (
1602 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1603 errmsg(
1604 "function returning record called in context "
1605 "that cannot accept type record"
1606 )
1607 ));
1608 }
1609
1610 BlessTupleDesc(tupdesc);
1611 funcctx->tuple_desc = tupdesc;
1612
1613 MemoryContextSwitchTo(oldcontext);
1614 }
1615
1616
1617 funcctx = SRF_PERCALL_SETUP();
1618
1619 call_cntr = funcctx->call_cntr;
1620 max_calls = funcctx->max_calls;
1621 tupdesc = funcctx->tuple_desc;
1622 vcnts2 = funcctx->user_fctx;
1623
1624
1625 if (call_cntr < max_calls) {
1628 HeapTuple tuple;
1630
1632
1634
1635 values[0] = Float8GetDatum(vcnts2[call_cntr].value);
1636 values[1] = UInt32GetDatum(vcnts2[call_cntr].count);
1637 values[2] = Float8GetDatum(vcnts2[call_cntr].percent);
1638
1639
1640 tuple = heap_form_tuple(tupdesc, values, nulls);
1641
1642
1643 result = HeapTupleGetDatum(tuple);
1644
1645 SRF_RETURN_NEXT(funcctx,
result);
1646 }
1647
1648 else {
1649 pfree(vcnts2);
1650 SRF_RETURN_DONE(funcctx);
1651 }
1652}
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,...)