sample = 1;
/* iterate through rasters of coverage */
- /* create sql */
- len = sizeof(char) * (strlen("SELECT \"\" FROM \"\" WHERE \"\" IS NOT NULL") + (strlen(colname) * 2) + strlen(tablename) + 1);
- sql = (char *) palloc(len);
- if (NULL == sql) {
- elog(ERROR, "RASTER_summaryStatsCoverage: Unable to allocate memory for sql\n");
- PG_RETURN_NULL();
- }
-
/* connect to database */
spi_result = SPI_connect();
if (spi_result != SPI_OK_CONNECT) {
PG_RETURN_NULL();
}
+ /* create sql */
+ len = sizeof(char) * (strlen("SELECT \"\" FROM \"\" WHERE \"\" IS NOT NULL") + (strlen(colname) * 2) + strlen(tablename) + 1);
+ sql = (char *) palloc(len);
+ if (NULL == sql) {
+ elog(ERROR, "RASTER_summaryStatsCoverage: Unable to allocate memory for sql\n");
+ if (SPI_tuptable) SPI_freetuptable(tuptable);
+ SPI_finish();
+ PG_RETURN_NULL();
+ }
+
/* get cursor */
snprintf(sql, len, "SELECT \"%s\" FROM \"%s\" WHERE \"%s\" IS NOT NULL", colname, tablename, colname);
portal = SPI_cursor_open_with_args(
NULL, NULL,
TRUE, 0
);
- SPI_pfree(sql);
+ pfree(sql);
/* process resultset */
SPI_cursor_fetch(portal, TRUE, 1);
SPI_finish();
if (NULL != rtn) pfree(rtn);
-
PG_RETURN_NULL();
}
else if (isNull) {
SPI_finish();
if (NULL != rtn) pfree(rtn);
-
PG_RETURN_NULL();
}
SPI_cursor_close(portal);
SPI_finish();
- rt_raster_destroy(raster);
if (NULL != rtn) pfree(rtn);
-
PG_RETURN_NULL();
}
SPI_cursor_close(portal);
SPI_finish();
- rt_raster_destroy(raster);
if (NULL != rtn) pfree(rtn);
-
PG_RETURN_NULL();
}
SPI_finish();
if (NULL != rtn) pfree(rtn);
-
PG_RETURN_NULL();
}
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
/* pgraster is null, return nothing */
- if (PG_ARGISNULL(0)) SRF_RETURN_DONE(funcctx);
+ if (PG_ARGISNULL(0)) {
+ MemoryContextSwitchTo(oldcontext);
+ SRF_RETURN_DONE(funcctx);
+ }
pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
raster = rt_raster_deserialize(pgraster, FALSE);
if (!raster) {
elog(ERROR, "RASTER_histogram: Could not deserialize raster");
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
if (bandindex < 1 || bandindex > num_bands) {
elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
rt_raster_destroy(raster);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
if (sample < 0 || sample > 1) {
elog(NOTICE, "Invalid sample percentage (must be between 0 and 1). Returning NULL");
rt_raster_destroy(raster);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
else if (FLT_EQ(sample, 0.0))
default:
elog(ERROR, "RASTER_histogram: Invalid data type for width");
rt_raster_destroy(raster);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
break;
}
elog(NOTICE, "Invalid value for width (must be greater than 0). Returning NULL");
pfree(bin_width);
rt_raster_destroy(raster);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
if (!band) {
elog(NOTICE, "Could not find band at index %d. Returning NULL", bandindex);
rt_raster_destroy(raster);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
rt_raster_destroy(raster);
if (NULL == stats || NULL == stats->values) {
elog(NOTICE, "Unable to compute summary statistics for band at index %d", bandindex);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
else if (stats->count < 1) {
elog(NOTICE, "Unable to compute histogram for band at index %d as the band has no values", bandindex);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
pfree(stats);
if (NULL == hist || !count) {
elog(NOTICE, "Unable to compute histogram for band at index %d", bandindex);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
/* tablename is null, return null */
if (PG_ARGISNULL(0)) {
elog(NOTICE, "Table name must be provided");
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
tablenametext = PG_GETARG_TEXT_P(0);
tablename = text_to_cstring(tablenametext);
if (!strlen(tablename)) {
elog(NOTICE, "Table name must be provided");
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
POSTGIS_RT_DEBUGF(3, "RASTER_histogramCoverage: tablename = %s", tablename);
/* column name is null, return null */
if (PG_ARGISNULL(1)) {
elog(NOTICE, "Column name must be provided");
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
colnametext = PG_GETARG_TEXT_P(1);
colname = text_to_cstring(colnametext);
if (!strlen(colname)) {
elog(NOTICE, "Column name must be provided");
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
POSTGIS_RT_DEBUGF(3, "RASTER_histogramCoverage: colname = %s", colname);
sample = PG_GETARG_FLOAT8(4);
if (sample < 0 || sample > 1) {
elog(NOTICE, "Invalid sample percentage (must be between 0 and 1). Returning NULL");
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
else if (FLT_EQ(sample, 0.0))
break;
default:
elog(ERROR, "RASTER_histogramCoverage: Invalid data type for width");
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
break;
}
if (width < 0 || FLT_EQ(width, 0.0)) {
elog(NOTICE, "Invalid value for width (must be greater than 0). Returning NULL");
pfree(bin_width);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
if (!PG_ARGISNULL(7))
right = PG_GETARG_BOOL(7);
+ /* connect to database */
+ spi_result = SPI_connect();
+ if (spi_result != SPI_OK_CONNECT) {
+ elog(ERROR, "RASTER_histogramCoverage: Could not connect to database using SPI\n");
+
+ if (bin_width_count) pfree(bin_width);
+
+ MemoryContextSwitchTo(oldcontext);
+ SRF_RETURN_DONE(funcctx);
+ }
+
/* coverage stats */
len = sizeof(char) * (strlen("SELECT min, max FROM _st_summarystats('','',,::boolean,)") + strlen(tablename) + strlen(colname) + (MAX_INT_CHARLEN * 2) + MAX_DBL_CHARLEN + 1);
sql = (char *) palloc(len);
if (NULL == sql) {
elog(ERROR, "RASTER_histogramCoverage: Unable to allocate memory for sql\n");
- if (bin_width_count) pfree(bin_width);
- SRF_RETURN_DONE(funcctx);
- }
- /* connect to database */
- spi_result = SPI_connect();
- if (spi_result != SPI_OK_CONNECT) {
- elog(ERROR, "RASTER_histogramCoverage: Could not connect to database using SPI\n");
+ if (SPI_tuptable) SPI_freetuptable(tuptable);
+ SPI_finish();
- pfree(sql);
if (bin_width_count) pfree(bin_width);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
snprintf(sql, len, "SELECT min, max FROM _st_summarystats('%s','%s',%d,%d::boolean,%f)", tablename, colname, bandindex, (exclude_nodata_value ? 1 : 0), sample);
POSTGIS_RT_DEBUGF(3, "RASTER_histogramCoverage: %s", sql);
spi_result = SPI_execute(sql, TRUE, 0);
- SPI_pfree(sql);
+ pfree(sql);
if (spi_result != SPI_OK_SELECT || SPI_tuptable == NULL || SPI_processed != 1) {
elog(ERROR, "RASTER_histogramCoverage: Could not get summary stats of coverage");
if (bin_width_count) pfree(bin_width);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
if (bin_width_count) pfree(bin_width);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
min = strtod(tmp, NULL);
if (bin_width_count) pfree(bin_width);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
max = strtod(tmp, NULL);
SPI_finish();
if (bin_width_count) pfree(bin_width);
+
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
if (NULL != covhist) pfree(covhist);
if (bin_width_count) pfree(bin_width);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
else if (isNull) {
if (NULL != covhist) pfree(covhist);
if (bin_width_count) pfree(bin_width);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
SPI_cursor_close(portal);
SPI_finish();
- rt_raster_destroy(raster);
if (NULL != covhist) pfree(covhist);
if (bin_width_count) pfree(bin_width);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
SPI_cursor_close(portal);
SPI_finish();
- rt_raster_destroy(raster);
if (NULL != covhist) pfree(covhist);
if (bin_width_count) pfree(bin_width);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
if (NULL != covhist) pfree(covhist);
if (bin_width_count) pfree(bin_width);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
if (NULL != covhist) pfree(covhist);
if (bin_width_count) pfree(bin_width);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
if (bin_width_count) pfree(bin_width);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
/* pgraster is null, return nothing */
- if (PG_ARGISNULL(0)) SRF_RETURN_DONE(funcctx);
+ if (PG_ARGISNULL(0)) {
+ MemoryContextSwitchTo(oldcontext);
+ SRF_RETURN_DONE(funcctx);
+ }
pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
raster = rt_raster_deserialize(pgraster, FALSE);
if (!raster) {
elog(ERROR, "RASTER_quantile: Could not deserialize raster");
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
if (bandindex < 1 || bandindex > num_bands) {
elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
rt_raster_destroy(raster);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
assert(0 <= (bandindex - 1));
if (sample < 0 || sample > 1) {
elog(NOTICE, "Invalid sample percentage (must be between 0 and 1). Returning NULL");
rt_raster_destroy(raster);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
else if (FLT_EQ(sample, 0.0))
default:
elog(ERROR, "RASTER_quantile: Invalid data type for quantiles");
rt_raster_destroy(raster);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
break;
}
elog(NOTICE, "Invalid value for quantile (must be between 0 and 1). Returning NULL");
pfree(quantiles);
rt_raster_destroy(raster);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
if (!band) {
elog(NOTICE, "Could not find band at index %d. Returning NULL", bandindex);
rt_raster_destroy(raster);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
rt_raster_destroy(raster);
if (NULL == stats || NULL == stats->values) {
elog(NOTICE, "Could not retrieve summary statistics for band at index %d", bandindex);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
else if (stats->count < 1) {
elog(NOTICE, "Unable to compute quantiles for band at index %d as the band has no values", bandindex);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
pfree(stats);
if (NULL == quant || !count) {
elog(NOTICE, "Unable to compute quantiles for band at index %d", bandindex);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
/* tablename is null, return null */
if (PG_ARGISNULL(0)) {
elog(NOTICE, "Table name must be provided");
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
tablenametext = PG_GETARG_TEXT_P(0);
tablename = text_to_cstring(tablenametext);
if (!strlen(tablename)) {
elog(NOTICE, "Table name must be provided");
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
POSTGIS_RT_DEBUGF(3, "RASTER_quantileCoverage: tablename = %s", tablename);
/* column name is null, return null */
if (PG_ARGISNULL(1)) {
elog(NOTICE, "Column name must be provided");
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
colnametext = PG_GETARG_TEXT_P(1);
colname = text_to_cstring(colnametext);
if (!strlen(colname)) {
elog(NOTICE, "Column name must be provided");
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
POSTGIS_RT_DEBUGF(3, "RASTER_quantileCoverage: colname = %s", colname);
sample = PG_GETARG_FLOAT8(4);
if (sample < 0 || sample > 1) {
elog(NOTICE, "Invalid sample percentage (must be between 0 and 1). Returning NULL");
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
else if (FLT_EQ(sample, 0.0))
break;
default:
elog(ERROR, "RASTER_quantileCoverage: Invalid data type for quantiles");
- rt_raster_destroy(raster);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
break;
}
if (quantile < 0 || quantile > 1) {
elog(NOTICE, "Invalid value for quantile (must be between 0 and 1). Returning NULL");
pfree(quantiles);
- rt_raster_destroy(raster);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
}
/* coverage stats */
+ /* connect to database */
+ spi_result = SPI_connect();
+ if (spi_result != SPI_OK_CONNECT) {
+ elog(ERROR, "RASTER_quantileCoverage: Could not connect to database using SPI\n");
+ MemoryContextSwitchTo(oldcontext);
+ SRF_RETURN_DONE(funcctx);
+ }
+
len = sizeof(char) * (strlen("SELECT count FROM _st_summarystats('','',,::boolean,)") + strlen(tablename) + strlen(colname) + (MAX_INT_CHARLEN * 2) + MAX_DBL_CHARLEN + 1);
sql = (char *) palloc(len);
if (NULL == sql) {
elog(ERROR, "RASTER_quantileCoverage: Unable to allocate memory for sql\n");
- SRF_RETURN_DONE(funcctx);
- }
- /* connect to database */
- spi_result = SPI_connect();
- if (spi_result != SPI_OK_CONNECT) {
- elog(ERROR, "RASTER_quantileCoverage: Could not connect to database using SPI\n");
- pfree(sql);
+ if (SPI_tuptable) SPI_freetuptable(tuptable);
+ SPI_finish();
+
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
snprintf(sql, len, "SELECT count FROM _st_summarystats('%s','%s',%d,%d::boolean,%f)", tablename, colname, bandindex, (exclude_nodata_value ? 1 : 0), sample);
POSTGIS_RT_DEBUGF(3, "stats sql: %s", sql);
spi_result = SPI_execute(sql, TRUE, 0);
- SPI_pfree(sql);
+ pfree(sql);
if (spi_result != SPI_OK_SELECT || SPI_tuptable == NULL || SPI_processed != 1) {
elog(ERROR, "RASTER_quantileCoverage: Could not get summary stats of coverage");
if (SPI_tuptable) SPI_freetuptable(tuptable);
SPI_finish();
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
if (SPI_tuptable) SPI_freetuptable(tuptable);
SPI_finish();
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
cov_count = strtol(tmp, NULL, 10);
if (SPI_tuptable) SPI_freetuptable(tuptable);
SPI_finish();
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
SPI_cursor_close(portal);
SPI_finish();
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
else if (isNull) {
SPI_cursor_close(portal);
SPI_finish();
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
SPI_cursor_close(portal);
SPI_finish();
- rt_raster_destroy(raster);
-
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
SPI_cursor_close(portal);
SPI_finish();
- rt_raster_destroy(raster);
-
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
SPI_cursor_close(portal);
SPI_finish();
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
/* pgraster is null, return nothing */
- if (PG_ARGISNULL(0)) SRF_RETURN_DONE(funcctx);
+ if (PG_ARGISNULL(0)) {
+ MemoryContextSwitchTo(oldcontext);
+ SRF_RETURN_DONE(funcctx);
+ }
pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
raster = rt_raster_deserialize(pgraster, FALSE);
if (!raster) {
elog(ERROR, "RASTER_valueCount: Could not deserialize raster");
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
if (bandindex < 1 || bandindex > num_bands) {
elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
rt_raster_destroy(raster);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
assert(0 <= (bandindex - 1));
default:
elog(ERROR, "RASTER_valueCount: Invalid data type for values");
rt_raster_destroy(raster);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
break;
}
if (!band) {
elog(NOTICE, "Could not find band at index %d. Returning NULL", bandindex);
rt_raster_destroy(raster);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
rt_raster_destroy(raster);
if (NULL == vcnts || !count) {
elog(NOTICE, "Unable to count the values for band at index %d", bandindex);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
/* tablename is null, return null */
if (PG_ARGISNULL(0)) {
elog(NOTICE, "Table name must be provided");
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
tablenametext = PG_GETARG_TEXT_P(0);
tablename = text_to_cstring(tablenametext);
if (!strlen(tablename)) {
elog(NOTICE, "Table name must be provided");
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
POSTGIS_RT_DEBUGF(3, "tablename = %s", tablename);
/* column name is null, return null */
if (PG_ARGISNULL(1)) {
elog(NOTICE, "Column name must be provided");
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
colnametext = PG_GETARG_TEXT_P(1);
colname = text_to_cstring(colnametext);
if (!strlen(colname)) {
elog(NOTICE, "Column name must be provided");
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
POSTGIS_RT_DEBUGF(3, "colname = %s", colname);
break;
default:
elog(ERROR, "RASTER_valueCountCoverage: Invalid data type for values");
- rt_raster_destroy(raster);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
break;
}
}
/* iterate through rasters of coverage */
+ /* connect to database */
+ spi_result = SPI_connect();
+ if (spi_result != SPI_OK_CONNECT) {
+ elog(ERROR, "RASTER_valueCountCoverage: Could not connect to database using SPI\n");
+
+ if (search_values_count) pfree(search_values);
+
+ MemoryContextSwitchTo(oldcontext);
+ SRF_RETURN_DONE(funcctx);
+ }
+
/* create sql */
len = sizeof(char) * (strlen("SELECT \"\" FROM \"\" WHERE \"\" IS NOT NULL") + (strlen(colname) * 2) + strlen(tablename) + 1);
sql = (char *) palloc(len);
if (NULL == sql) {
elog(ERROR, "RASTER_valueCountCoverage: Unable to allocate memory for sql\n");
- if (search_values_count) pfree(search_values);
- SRF_RETURN_DONE(funcctx);
- }
-
- /* connect to database */
- spi_result = SPI_connect();
- if (spi_result != SPI_OK_CONNECT) {
- elog(ERROR, "RASTER_valueCountCoverage: Could not connect to database using SPI\n");
+ if (SPI_tuptable) SPI_freetuptable(tuptable);
+ SPI_finish();
- pfree(sql);
if (search_values_count) pfree(search_values);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
NULL, NULL,
TRUE, 0
);
- SPI_pfree(sql);
+ pfree(sql);
/* process resultset */
SPI_cursor_fetch(portal, TRUE, 1);
if (NULL != covvcnts) pfree(covvcnts);
if (search_values_count) pfree(search_values);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
else if (isNull) {
if (NULL != covvcnts) pfree(covvcnts);
if (search_values_count) pfree(search_values);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
SPI_cursor_close(portal);
SPI_finish();
- rt_raster_destroy(raster);
if (NULL != covvcnts) pfree(covvcnts);
if (search_values_count) pfree(search_values);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
SPI_cursor_close(portal);
SPI_finish();
- rt_raster_destroy(raster);
if (NULL != covvcnts) pfree(covvcnts);
if (search_values_count) pfree(search_values);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
if (NULL == vcnts || !count) {
elog(NOTICE, "Unable to count the values for band at index %d", bandindex);
- pfree(vcnts);
if (SPI_tuptable) SPI_freetuptable(tuptable);
SPI_cursor_close(portal);
SPI_finish();
if (NULL != covvcnts) free(covvcnts);
if (search_values_count) pfree(search_values);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
if (NULL == covvcnts) {
elog(ERROR, "RASTER_valueCountCoverage: Unable to allocate memory for value counts of coverage");
- pfree(vcnts);
if (SPI_tuptable) SPI_freetuptable(tuptable);
SPI_cursor_close(portal);
SPI_finish();
if (search_values_count) pfree(search_values);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
if (NULL == covvcnts) {
elog(ERROR, "RASTER_valueCountCoverage: Unable to change allocated memory for value counts of coverage");
- pfree(vcnts);
if (SPI_tuptable) SPI_freetuptable(tuptable);
SPI_cursor_close(portal);
SPI_finish();
if (search_values_count) pfree(search_values);
if (NULL != covvcnts) free(covvcnts);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
if (!n) {
elog(NOTICE, "Invalid argument for reclassargset. Returning original raster");
- rt_raster_destroy(raster);
+ pgraster = rt_raster_serialize(raster);
+ rt_raster_destroy(raster);
+ if (!pgraster) PG_RETURN_NULL();
SET_VARSIZE(pgraster, pgraster->size);
+
PG_RETURN_POINTER(pgraster);
}
tup = (HeapTupleHeader) DatumGetPointer(e[i]);
if (NULL == tup) {
elog(NOTICE, "Invalid argument for reclassargset. Returning original raster");
- rt_raster_destroy(raster);
+ pgraster = rt_raster_serialize(raster);
+ rt_raster_destroy(raster);
+ if (!pgraster) PG_RETURN_NULL();
SET_VARSIZE(pgraster, pgraster->size);
+
PG_RETURN_POINTER(pgraster);
}
tupv = GetAttributeByName(tup, "nband", &isnull);
if (isnull) {
elog(NOTICE, "Invalid argument for reclassargset. Missing value of nband for reclassarg of index %d . Returning original raster", i);
- rt_raster_destroy(raster);
+ pgraster = rt_raster_serialize(raster);
+ rt_raster_destroy(raster);
+ if (!pgraster) PG_RETURN_NULL();
SET_VARSIZE(pgraster, pgraster->size);
+
PG_RETURN_POINTER(pgraster);
}
nband = DatumGetInt32(tupv);
/* valid band index? */
if (nband < 1 || nband > numBands) {
elog(NOTICE, "Invalid argument for reclassargset. Invalid band index (must use 1-based) for reclassarg of index %d . Returning original raster", i);
- rt_raster_destroy(raster);
+ pgraster = rt_raster_serialize(raster);
+ rt_raster_destroy(raster);
+ if (!pgraster) PG_RETURN_NULL();
SET_VARSIZE(pgraster, pgraster->size);
+
PG_RETURN_POINTER(pgraster);
}
tupv = GetAttributeByName(tup, "reclassexpr", &isnull);
if (isnull) {
elog(NOTICE, "Invalid argument for reclassargset. Missing value of reclassexpr for reclassarg of index %d . Returning original raster", i);
- rt_raster_destroy(raster);
+ pgraster = rt_raster_serialize(raster);
+ rt_raster_destroy(raster);
+ if (!pgraster) PG_RETURN_NULL();
SET_VARSIZE(pgraster, pgraster->size);
+
PG_RETURN_POINTER(pgraster);
}
exprtext = (text *) DatumGetPointer(tupv);
if (NULL == exprtext) {
elog(NOTICE, "Invalid argument for reclassargset. Missing value of reclassexpr for reclassarg of index %d . Returning original raster", i);
- rt_raster_destroy(raster);
+ pgraster = rt_raster_serialize(raster);
+ rt_raster_destroy(raster);
+ if (!pgraster) PG_RETURN_NULL();
SET_VARSIZE(pgraster, pgraster->size);
+
PG_RETURN_POINTER(pgraster);
}
expr = text_to_cstring(exprtext);
comma_set = rtpg_strsplit(expr, ",", &comma_n);
if (comma_n < 1) {
elog(NOTICE, "Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
- rt_raster_destroy(raster);
+ pgraster = rt_raster_serialize(raster);
+ rt_raster_destroy(raster);
+ if (!pgraster) PG_RETURN_NULL();
SET_VARSIZE(pgraster, pgraster->size);
+
PG_RETURN_POINTER(pgraster);
}
elog(NOTICE, "Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
for (k = 0; k < j; k++) pfree(exprset[k]);
pfree(exprset);
- rt_raster_destroy(raster);
+ pgraster = rt_raster_serialize(raster);
+ rt_raster_destroy(raster);
+ if (!pgraster) PG_RETURN_NULL();
SET_VARSIZE(pgraster, pgraster->size);
+
PG_RETURN_POINTER(pgraster);
}
elog(NOTICE, "Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
for (k = 0; k < j; k++) pfree(exprset[k]);
pfree(exprset);
- rt_raster_destroy(raster);
+ pgraster = rt_raster_serialize(raster);
+ rt_raster_destroy(raster);
+ if (!pgraster) PG_RETURN_NULL();
SET_VARSIZE(pgraster, pgraster->size);
+
PG_RETURN_POINTER(pgraster);
}
elog(NOTICE, "Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
for (k = 0; k < j; k++) pfree(exprset[k]);
pfree(exprset);
- rt_raster_destroy(raster);
+ pgraster = rt_raster_serialize(raster);
+ rt_raster_destroy(raster);
+ if (!pgraster) PG_RETURN_NULL();
SET_VARSIZE(pgraster, pgraster->size);
+
PG_RETURN_POINTER(pgraster);
}
elog(NOTICE, "Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
for (k = 0; k < j; k++) pfree(exprset[k]);
pfree(exprset);
- rt_raster_destroy(raster);
+ pgraster = rt_raster_serialize(raster);
+ rt_raster_destroy(raster);
+ if (!pgraster) PG_RETURN_NULL();
SET_VARSIZE(pgraster, pgraster->size);
+
PG_RETURN_POINTER(pgraster);
}
POSTGIS_RT_DEBUGF(5, "RASTER_reclass: min/max (double) %f", val);
tupv = GetAttributeByName(tup, "pixeltype", &isnull);
if (isnull) {
elog(NOTICE, "Invalid argument for reclassargset. Missing value of pixeltype for reclassarg of index %d . Returning original raster", i);
- rt_raster_destroy(raster);
+ pgraster = rt_raster_serialize(raster);
+ rt_raster_destroy(raster);
+ if (!pgraster) PG_RETURN_NULL();
SET_VARSIZE(pgraster, pgraster->size);
+
PG_RETURN_POINTER(pgraster);
}
pixeltypetext = (text *) DatumGetPointer(tupv);
if (NULL == pixeltypetext) {
elog(NOTICE, "Invalid argument for reclassargset. Missing value of pixeltype for reclassarg of index %d . Returning original raster", i);
- rt_raster_destroy(raster);
+ pgraster = rt_raster_serialize(raster);
+ rt_raster_destroy(raster);
+ if (!pgraster) PG_RETURN_NULL();
SET_VARSIZE(pgraster, pgraster->size);
+
PG_RETURN_POINTER(pgraster);
}
pixeltype = text_to_cstring(pixeltypetext);
elog(NOTICE, "Could not find raster band of index %d. Returning original raster", nband);
for (k = 0; k < j; k++) pfree(exprset[k]);
pfree(exprset);
- rt_raster_destroy(raster);
+ pgraster = rt_raster_serialize(raster);
+ rt_raster_destroy(raster);
+ if (!pgraster) PG_RETURN_NULL();
SET_VARSIZE(pgraster, pgraster->size);
+
PG_RETURN_POINTER(pgraster);
}
newband = rt_band_reclass(band, pixtype, hasnodata, nodataval, exprset, j);
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
/* pgraster is null, return null */
- if (PG_ARGISNULL(0)) SRF_RETURN_DONE(funcctx);
+ if (PG_ARGISNULL(0)) {
+ MemoryContextSwitchTo(oldcontext);
+ SRF_RETURN_DONE(funcctx);
+ }
pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
/* raster */
raster = rt_raster_deserialize(pgraster, FALSE);
if (!raster) {
- MemoryContextSwitchTo(oldcontext);
elog(ERROR, "RASTER_bandmetadata: Could not deserialize raster");
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
/* numbands */
numBands = rt_raster_get_num_bands(raster);
if (numBands < 1) {
+ elog(NOTICE, "Raster provided has no bands");
rt_raster_destroy(raster);
MemoryContextSwitchTo(oldcontext);
- elog(NOTICE, "Raster provided has no bands");
SRF_RETURN_DONE(funcctx);
}
default:
elog(ERROR, "RASTER_bandmetadata: Invalid data type for band number(s)");
rt_raster_destroy(raster);
+ MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
break;
}