]> granicus.if.org Git - postgis/commitdiff
Addition of rt_raster_surface() function and related regression test.
authorBborie Park <bkpark at ucdavis.edu>
Fri, 13 Jul 2012 23:28:39 +0000 (23:28 +0000)
committerBborie Park <bkpark at ucdavis.edu>
Fri, 13 Jul 2012 23:28:39 +0000 (23:28 +0000)
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
raster/test/core/testapi.c

index 9a4b001f19206411790bd813b82709badeacd6e7..5fbe39702183b2e26d858446a86bc61b199053e6 100644 (file)
@@ -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);
                }
index 682ede3d6cf5710645b66b4947dba352562bf003..a2da802bd48759f9d5efe7c1cc0d105b8d96de2c 100644 (file)
@@ -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");