From 9a11530a46132f79f63e13e86ae8462bdcb2107a Mon Sep 17 00:00:00 2001 From: Bborie Park Date: Fri, 13 Jul 2012 23:28:39 +0000 Subject: [PATCH] Addition of rt_raster_surface() function and related regression test. Added regression test for rt_raster_pixel_as_polygon. git-svn-id: http://svn.osgeo.org/postgis/trunk@10055 b70326c6-7e19-0410-871a-916f4a2858ee --- raster/rt_core/rt_api.c | 24 ++++++++++----- raster/test/core/testapi.c | 63 ++++++++++++++++++++++++++++++++++---- 2 files changed, 73 insertions(+), 14 deletions(-) diff --git a/raster/rt_core/rt_api.c b/raster/rt_core/rt_api.c index 9a4b001f1..5fbe39702 100644 --- a/raster/rt_core/rt_api.c +++ b/raster/rt_core/rt_api.c @@ -12074,35 +12074,43 @@ LWMPOLY* rt_raster_surface(rt_raster raster, int nband) { if (mpoly != NULL) { /* convert to multi */ if (!lwgeom_is_collection(mpoly)) { + LWGEOM *tmp2 = NULL; tmp = mpoly; #if POSTGIS_DEBUG_LEVEL > 3 { - char *wkt = NULL; - wkt = lwgeom_to_wkt(mpoly, WKT_ISO, DBL_DIG, NULL); + char *wkt = lwgeom_to_wkt(mpoly, WKT_ISO, DBL_DIG, NULL); RASTER_DEBUGF(4, "before multi = %s", wkt); rtdealloc(wkt); } #endif - mpoly = lwgeom_clone_deep(lwgeom_as_multi(tmp)); + RASTER_DEBUGF(4, "mpoly @ %p", mpoly); + + /* + lwgeom_as_multi() only does a shallow clone internally + so input and output geometries may share memory + */ + mpoly = lwgeom_as_multi(tmp); + tmp2 = lwgeom_clone_deep(mpoly); lwgeom_free(tmp); + lwgeom_free(mpoly); + mpoly = tmp2; + + RASTER_DEBUGF(4, "mpoly @ %p", mpoly); #if POSTGIS_DEBUG_LEVEL > 3 { - char *wkt = NULL; - wkt = lwgeom_to_wkt(mpoly, WKT_ISO, DBL_DIG, NULL); + char *wkt = lwgeom_to_wkt(mpoly, WKT_ISO, DBL_DIG, NULL); RASTER_DEBUGF(4, "after multi = %s", wkt); rtdealloc(wkt); } #endif - } #if POSTGIS_DEBUG_LEVEL > 3 { - char *wkt = NULL; - wkt = lwgeom_to_wkt(mpoly, WKT_ISO, DBL_DIG, NULL); + char *wkt = lwgeom_to_wkt(mpoly, WKT_ISO, DBL_DIG, NULL); RASTER_DEBUGF(4, "returning geometry = %s", wkt); rtdealloc(wkt); } diff --git a/raster/test/core/testapi.c b/raster/test/core/testapi.c index 682ede3d6..a2da802bd 100644 --- a/raster/test/core/testapi.c +++ b/raster/test/core/testapi.c @@ -2894,6 +2894,53 @@ static void testPixelOfValue() { deepRelease(rast); } +static void testPixelAsPolygon() { + rt_raster rast; + rt_band band; + uint32_t x, y; + int rtn; + const int maxX = 10; + const int maxY = 10; + LWPOLY *poly = NULL; + + rast = rt_raster_new(maxX, maxY); + assert(rast); + + band = addBand(rast, PT_32BUI, 1, 0); + CHECK(band); + + for (x = 0; x < maxX; x++) { + for (y = 0; y < maxY; y++) { + rtn = rt_band_set_pixel(band, x, y, 1); + CHECK((rtn != -1)); + } + } + + rt_band_set_pixel(band, 0, 0, 0); + rt_band_set_pixel(band, 3, 0, 0); + rt_band_set_pixel(band, 6, 0, 0); + rt_band_set_pixel(band, 9, 0, 0); + rt_band_set_pixel(band, 1, 2, 0); + rt_band_set_pixel(band, 4, 2, 0); + rt_band_set_pixel(band, 7, 2, 0); + rt_band_set_pixel(band, 2, 4, 0); + rt_band_set_pixel(band, 5, 4, 0); + rt_band_set_pixel(band, 8, 4, 0); + rt_band_set_pixel(band, 0, 6, 0); + rt_band_set_pixel(band, 3, 6, 0); + rt_band_set_pixel(band, 6, 6, 0); + rt_band_set_pixel(band, 9, 6, 0); + rt_band_set_pixel(band, 1, 8, 0); + rt_band_set_pixel(band, 4, 8, 0); + rt_band_set_pixel(band, 7, 8, 0); + + poly = rt_raster_pixel_as_polygon(rast, 1, 1); + CHECK((poly != NULL)); + lwpoly_free(poly); + + deepRelease(rast); +} + static void testRasterSurface() { rt_raster rast; rt_band band; @@ -2920,33 +2967,33 @@ static void testRasterSurface() { mpoly = rt_raster_surface(rast, 0); CHECK((mpoly != NULL)); - wkt = lwgeom_to_wkt((const LWGEOM *) lwmpoly_as_lwgeom(mpoly), WKT_ISO, DBL_DIG, NULL); - RASTER_DEBUGF(4, "wkt = %s", wkt); + wkt = lwgeom_to_text(lwmpoly_as_lwgeom(mpoly)); CHECK(!strcmp(wkt, "MULTIPOLYGON(((0 0,1 0,2 0,3 0,4 0,5 0,5 -1,5 -2,5 -3,5 -4,5 -5,4 -5,3 -5,2 -5,1 -5,0 -5,0 -4,0 -3,0 -2,0 -1,0 0)))")); rtdealloc(wkt); lwmpoly_free(mpoly); + mpoly = NULL; /* 0,0 NODATA */ rt_band_set_pixel(band, 0, 0, 0); mpoly = rt_raster_surface(rast, 0); CHECK((mpoly != NULL)); - wkt = lwgeom_to_wkt((const LWGEOM *) lwmpoly_as_lwgeom(mpoly), WKT_ISO, DBL_DIG, NULL); - RASTER_DEBUGF(4, "wkt = %s", wkt); + wkt = lwgeom_to_text(lwmpoly_as_lwgeom(mpoly)); CHECK(!strcmp(wkt, "MULTIPOLYGON(((1 0,2 0,3 0,4 0,5 0,5 -1,5 -2,5 -3,5 -4,5 -5,4 -5,3 -5,2 -5,1 -5,0 -5,0 -4,0 -3,0 -2,0 -1,1 -1,1 0)))")); rtdealloc(wkt); lwmpoly_free(mpoly); + mpoly = NULL; /* plus 1,1 NODATA */ rt_band_set_pixel(band, 1, 1, 0); mpoly = rt_raster_surface(rast, 0); CHECK((mpoly != NULL)); - wkt = lwgeom_to_wkt((const LWGEOM *) lwmpoly_as_lwgeom(mpoly), WKT_ISO, DBL_DIG, NULL); - RASTER_DEBUGF(4, "wkt = %s", wkt); + wkt = lwgeom_to_text(lwmpoly_as_lwgeom(mpoly)); CHECK(!strcmp(wkt, "MULTIPOLYGON(((1 0,2 0,3 0,4 0,5 0,5 -1,5 -2,5 -3,5 -4,5 -5,4 -5,3 -5,2 -5,1 -5,0 -5,0 -4,0 -3,0 -2,0 -1,1 -1,1 0),(1 -1,1 -2,2 -2,2 -1,1 -1)))")); rtdealloc(wkt); lwmpoly_free(mpoly); + mpoly = NULL; deepRelease(rast); } @@ -3225,6 +3272,10 @@ main() testPixelOfValue(); printf("OK\n"); + printf("Testing rt_raster_pixel_as_polygon... "); + testPixelAsPolygon(); + printf("OK\n"); + printf("Testing rt_raster_surface... "); testRasterSurface(); printf("OK\n"); -- 2.40.0