2615 FuncCallContext *funcctx;
2619 uint64_t covcount = 0;
2620 uint64_t covtotal = 0;
2629 if (SRF_IS_FIRSTCALL()) {
2630 MemoryContext oldcontext;
2632 text *tablenametext = NULL;
2633 char *tablename = NULL;
2634 text *colnametext = NULL;
2635 char *colname = NULL;
2636 int32_t bandindex = 1;
2637 bool exclude_nodata_value =
TRUE;
2638 double *search_values = NULL;
2639 uint32_t search_values_count = 0;
2648 bool isNull =
FALSE;
2670 funcctx = SRF_FIRSTCALL_INIT();
2673 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
2676 if (PG_ARGISNULL(0)) {
2677 elog(NOTICE,
"Table name must be provided");
2678 MemoryContextSwitchTo(oldcontext);
2679 SRF_RETURN_DONE(funcctx);
2681 tablenametext = PG_GETARG_TEXT_P(0);
2683 if (!strlen(tablename)) {
2684 elog(NOTICE,
"Table name must be provided");
2685 MemoryContextSwitchTo(oldcontext);
2686 SRF_RETURN_DONE(funcctx);
2691 if (PG_ARGISNULL(1)) {
2692 elog(NOTICE,
"Column name must be provided");
2693 MemoryContextSwitchTo(oldcontext);
2694 SRF_RETURN_DONE(funcctx);
2696 colnametext = PG_GETARG_TEXT_P(1);
2698 if (!strlen(colname)) {
2699 elog(NOTICE,
"Column name must be provided");
2700 MemoryContextSwitchTo(oldcontext);
2701 SRF_RETURN_DONE(funcctx);
2706 if (!PG_ARGISNULL(2))
2707 bandindex = PG_GETARG_INT32(2);
2710 if (!PG_ARGISNULL(3))
2711 exclude_nodata_value = PG_GETARG_BOOL(3);
2714 if (!PG_ARGISNULL(4)) {
2715 array = PG_GETARG_ARRAYTYPE_P(4);
2716 etype = ARR_ELEMTYPE(array);
2717 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
2724 MemoryContextSwitchTo(oldcontext);
2725 elog(ERROR,
"RASTER_valueCountCoverage: Invalid data type for values");
2726 SRF_RETURN_DONE(funcctx);
2730 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
2733 search_values = palloc(
sizeof(
double) * n);
2734 for (i = 0, j = 0; i < (uint32_t) n; i++) {
2735 if (nulls[i])
continue;
2739 search_values[j] = (double) DatumGetFloat4(e[i]);
2742 search_values[j] = (double) DatumGetFloat8(e[i]);
2749 search_values_count = j;
2752 pfree(search_values);
2753 search_values = NULL;
2758 if (!PG_ARGISNULL(5)) {
2759 roundto = PG_GETARG_FLOAT8(5);
2760 if (roundto < 0.) roundto = 0;
2765 spi_result = SPI_connect();
2766 if (spi_result != SPI_OK_CONNECT) {
2768 if (search_values_count) pfree(search_values);
2770 MemoryContextSwitchTo(oldcontext);
2771 elog(ERROR,
"RASTER_valueCountCoverage: Cannot connect to database using SPI");
2772 SRF_RETURN_DONE(funcctx);
2776 len =
sizeof(char) * (strlen(
"SELECT \"\" FROM \"\" WHERE \"\" IS NOT NULL") + (strlen(colname) * 2) + strlen(tablename) + 1);
2777 sql = (
char *) palloc(len);
2780 if (SPI_tuptable) SPI_freetuptable(SPI_tuptable);
2783 if (search_values_count) pfree(search_values);
2785 MemoryContextSwitchTo(oldcontext);
2786 elog(ERROR,
"RASTER_valueCountCoverage: Cannot allocate memory for sql");
2787 SRF_RETURN_DONE(funcctx);
2791 snprintf(
sql, len,
"SELECT \"%s\" FROM \"%s\" WHERE \"%s\" IS NOT NULL", colname, tablename, colname);
2793 portal = SPI_cursor_open_with_args(
2803 SPI_cursor_fetch(portal,
TRUE, 1);
2804 while (SPI_processed == 1 && SPI_tuptable != NULL) {
2805 tupdesc = SPI_tuptable->tupdesc;
2806 tuple = SPI_tuptable->vals[0];
2808 datum = SPI_getbinval(tuple, tupdesc, 1, &isNull);
2809 if (SPI_result == SPI_ERROR_NOATTRIBUTE) {
2811 SPI_freetuptable(SPI_tuptable);
2812 SPI_cursor_close(portal);
2815 if (NULL != covvcnts) pfree(covvcnts);
2816 if (search_values_count) pfree(search_values);
2818 MemoryContextSwitchTo(oldcontext);
2819 elog(ERROR,
"RASTER_valueCountCoverage: Cannot get raster of coverage");
2820 SRF_RETURN_DONE(funcctx);
2823 SPI_cursor_fetch(portal,
TRUE, 1);
2827 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(datum);
2832 SPI_freetuptable(SPI_tuptable);
2833 SPI_cursor_close(portal);
2836 if (NULL != covvcnts) pfree(covvcnts);
2837 if (search_values_count) pfree(search_values);
2839 MemoryContextSwitchTo(oldcontext);
2840 elog(ERROR,
"RASTER_valueCountCoverage: Cannot deserialize raster");
2841 SRF_RETURN_DONE(funcctx);
2846 if (bandindex < 1 || bandindex > num_bands) {
2847 elog(NOTICE,
"Invalid band index (must use 1-based). Returning NULL");
2851 SPI_freetuptable(SPI_tuptable);
2852 SPI_cursor_close(portal);
2855 if (NULL != covvcnts) pfree(covvcnts);
2856 if (search_values_count) pfree(search_values);
2858 MemoryContextSwitchTo(oldcontext);
2859 SRF_RETURN_DONE(funcctx);
2865 elog(NOTICE,
"Cannot find band at index %d. Returning NULL", bandindex);
2869 SPI_freetuptable(SPI_tuptable);
2870 SPI_cursor_close(portal);
2873 if (NULL != covvcnts) pfree(covvcnts);
2874 if (search_values_count) pfree(search_values);
2876 MemoryContextSwitchTo(oldcontext);
2877 SRF_RETURN_DONE(funcctx);
2884 if (NULL == vcnts || !
count) {
2885 elog(NOTICE,
"Cannot count the values for band at index %d", bandindex);
2887 SPI_freetuptable(SPI_tuptable);
2888 SPI_cursor_close(portal);
2891 if (NULL != covvcnts)
free(covvcnts);
2892 if (search_values_count) pfree(search_values);
2894 MemoryContextSwitchTo(oldcontext);
2895 SRF_RETURN_DONE(funcctx);
2900 if (NULL == covvcnts) {
2902 if (NULL == covvcnts) {
2904 SPI_freetuptable(SPI_tuptable);
2905 SPI_cursor_close(portal);
2908 if (search_values_count) pfree(search_values);
2910 MemoryContextSwitchTo(oldcontext);
2911 elog(ERROR,
"RASTER_valueCountCoverage: Cannot allocate memory for value counts of coverage");
2912 SRF_RETURN_DONE(funcctx);
2915 for (i = 0; i <
count; i++) {
2924 for (i = 0; i <
count; i++) {
2927 for (j = 0; j < covcount; j++) {
2939 covvcnts = SPI_repalloc(covvcnts,
sizeof(
struct rt_valuecount_t) * covcount);
2941 SPI_freetuptable(SPI_tuptable);
2942 SPI_cursor_close(portal);
2945 if (search_values_count) pfree(search_values);
2947 MemoryContextSwitchTo(oldcontext);
2948 elog(ERROR,
"RASTER_valueCountCoverage: Cannot change allocated memory for value counts of coverage");
2949 SRF_RETURN_DONE(funcctx);
2952 covvcnts[covcount - 1].
value = vcnts[i].
value;
2953 covvcnts[covcount - 1].
count = vcnts[i].
count;
2954 covvcnts[covcount - 1].
percent = -1;
2964 SPI_cursor_fetch(portal,
TRUE, 1);
2967 if (SPI_tuptable) SPI_freetuptable(SPI_tuptable);
2968 SPI_cursor_close(portal);
2971 if (search_values_count) pfree(search_values);
2974 for (i = 0; i < covcount; i++) {
2975 covvcnts[i].
percent = (double) covvcnts[i].
count / covtotal;
2979 funcctx->user_fctx = covvcnts;
2982 funcctx->max_calls = covcount;
2985 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
2987 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2989 "function returning record called in context "
2990 "that cannot accept type record"
2995 BlessTupleDesc(tupdesc);
2996 funcctx->tuple_desc = tupdesc;
2998 MemoryContextSwitchTo(oldcontext);
3002 funcctx = SRF_PERCALL_SETUP();
3004 call_cntr = funcctx->call_cntr;
3005 max_calls = funcctx->max_calls;
3006 tupdesc = funcctx->tuple_desc;
3007 covvcnts2 = funcctx->user_fctx;
3010 if (call_cntr < max_calls) {
3020 values[0] = Float8GetDatum(covvcnts2[call_cntr].
value);
3021 values[1] = UInt32GetDatum(covvcnts2[call_cntr].
count);
3022 values[2] = Float8GetDatum(covvcnts2[call_cntr].percent);
3025 tuple = heap_form_tuple(tupdesc, values, nulls);
3028 result = HeapTupleGetDatum(tuple);
3030 SRF_RETURN_NEXT(funcctx, result);
3035 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)
struct rt_valuecount_t * rt_valuecount
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): ...
char * text_to_cstring(const text *textptr)
#define POSTGIS_RT_DEBUG(level, msg)
#define POSTGIS_RT_DEBUGF(level, msg,...)