From: Bborie Park Date: Tue, 3 Jul 2012 22:29:51 +0000 (+0000) Subject: Added check for empty raster to RASTER_getPixelPolygons X-Git-Tag: 2.1.0beta2~830 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=94c7d39610327e0f4331fa62fcfdb4bdbae0f116;p=postgis Added check for empty raster to RASTER_getPixelPolygons git-svn-id: http://svn.osgeo.org/postgis/trunk@10021 b70326c6-7e19-0410-871a-916f4a2858ee --- diff --git a/raster/rt_pg/rt_pg.c b/raster/rt_pg/rt_pg.c index 65b8a67a0..f42c2b720 100644 --- a/raster/rt_pg/rt_pg.c +++ b/raster/rt_pg/rt_pg.c @@ -2583,31 +2583,39 @@ Datum RASTER_getPixelPolygons(PG_FUNCTION_ARGS) SRF_RETURN_DONE(funcctx); } + /* raster empty, return */ + if (rt_raster_is_empty(raster)) { + elog(NOTICE, "Raster is empty. Returning NULL"); + PG_FREE_IF_COPY(pgraster, 0); + MemoryContextSwitchTo(oldcontext); + SRF_RETURN_DONE(funcctx); + } + /* band specified, load band and info */ if (!noband) { - POSTGIS_RT_DEBUGF(3, "band %d", nband); - numbands = rt_raster_get_num_bands(raster); + do { + numbands = rt_raster_get_num_bands(raster); + POSTGIS_RT_DEBUGF(3, "band %d", nband); + POSTGIS_RT_DEBUGF(3, "# of bands %d", numbands); + + if (nband < 1 || nband > numbands) { + elog(NOTICE, "Invalid band index (must use 1-based). Returning pixel values will be NULL"); + noband = TRUE; + break; + } - if (nband < 1 || nband > numbands) { - elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL"); - rt_raster_destroy(raster); - PG_FREE_IF_COPY(pgraster, 0); - MemoryContextSwitchTo(oldcontext); - SRF_RETURN_DONE(funcctx); - } + band = rt_raster_get_band(raster, nband - 1); + if (!band) { + elog(NOTICE, "Could not find band at index %d. Returning pixel values will be NULL", nband); + noband = TRUE; + break; + } - band = rt_raster_get_band(raster, nband - 1); - if (!band) { - elog(NOTICE, "Could not find band at index %d. Returning NULL", nband); - rt_raster_destroy(raster); - PG_FREE_IF_COPY(pgraster, 0); - MemoryContextSwitchTo(oldcontext); - SRF_RETURN_DONE(funcctx); + hasnodata = rt_band_get_hasnodata_flag(band); + if (hasnodata) + nodataval = rt_band_get_nodata(band); } - - hasnodata = rt_band_get_hasnodata_flag(band); - if (hasnodata) - nodataval = rt_band_get_nodata(band); + while (0); } /* set bounds if columnx, rowy not set */ @@ -2619,6 +2627,8 @@ Datum RASTER_getPixelPolygons(PG_FUNCTION_ARGS) bounds[2] = 1; bounds[3] = rt_raster_get_height(raster); } + POSTGIS_RT_DEBUGF(3, "bounds (min x, max x, min y, max y) = (%d, %d, %d, %d)", + bounds[0], bounds[1], bounds[2], bounds[3]); /* rowy */ pixcount = 0; @@ -2740,8 +2750,8 @@ Datum RASTER_getPixelPolygons(PG_FUNCTION_ARGS) int values_length = 4; Datum values[values_length]; bool nulls[values_length]; - HeapTuple tuple; - Datum result; + HeapTuple tuple; + Datum result; GSERIALIZED *gser = NULL; size_t gser_size = 0; @@ -2897,7 +2907,7 @@ Datum RASTER_pixelOfValue(PG_FUNCTION_ARGS) } search[nsearch] = val; - POSTGIS_RT_DEBUGF(3, "search[%d] = %d", nsearch, search[nsearch]); + POSTGIS_RT_DEBUGF(3, "search[%d] = %f", nsearch, search[nsearch]); nsearch++; }