]> granicus.if.org Git - postgis/commitdiff
Some bugs related with ticket #870 solved. Added more test for polygonize function.
authorJorge Arévalo <jorge.arevalo at deimos-space.com>
Mon, 28 Mar 2011 22:49:00 +0000 (22:49 +0000)
committerJorge Arévalo <jorge.arevalo at deimos-space.com>
Mon, 28 Mar 2011 22:49:00 +0000 (22:49 +0000)
git-svn-id: http://svn.osgeo.org/postgis/trunk@6980 b70326c6-7e19-0410-871a-916f4a2858ee

raster/rt_core/rt_api.c
raster/test/core/testapi.c

index 57d7d99cbddc8795245124ec8a1657eca0640ea6..c901a7ce7005bce253a2aaf582c798b6ee568642 100644 (file)
@@ -895,12 +895,12 @@ rt_band_set_pixel(rt_context ctx, rt_band band, uint16_t x, uint16_t y,
     pixtype = band->pixtype;
 
     if (x >= band->width || y >= band->height) {
-        ctx->warn("Coordinates out of range while setting pixel value");
+        ctx->err("rt_band_set_pixel: Coordinates out of range");
         return -1;
     }
 
     if (band->offline) {
-        ctx->warn("rt_band_set_pixel not implemented yet for OFFDB bands");
+        ctx->err("rt_band_set_pixel not implemented yet for OFFDB bands");
         return -1;
     }
 
@@ -1031,7 +1031,7 @@ rt_band_get_pixel(rt_context ctx, rt_band band, uint16_t x, uint16_t y, double *
     }
 
     if (band->offline) {
-        ctx->warn("rt_band_get_pixel not implemented yet for OFFDB bands");
+        ctx->err("rt_band_get_pixel not implemented yet for OFFDB bands");
         return -1;
     }
 
@@ -1472,8 +1472,8 @@ rt_raster_add_band(rt_context ctx, rt_raster raster, rt_band band, int index) {
 
     RASTER_DEBUGF(3, "Adding band %p to raster %p", band, raster);
 
-    if (band->width != raster->width || band->height != raster->height) {
-        ctx->warn("Can't add a %dx%d band to a %dx%d raster",
+    if (band->width != raster->width) {
+        ctx->err("rt_raster_add_band: Can't add a %dx%d band to a %dx%d raster",
                 band->width, band->height, raster->width, raster->height);
         return -1;
     }
@@ -1749,12 +1749,9 @@ rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
     int nFeatureCount = 0;
     rt_band band = NULL;
     int iPixVal = -1;
-    int nValidPols = 0;
     double dValue = 0.0;
     int iBandHasNodataValue = FALSE;
     double dBandNoData = 0.0;
-    double dEpsilon = 0.0;
-    int nCont = 0;
 
     /* Checkings */
     assert(NULL != ctx);
@@ -1788,7 +1785,7 @@ rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
     memdatasource = OGR_Dr_CreateDataSource(ogr_drv, "", NULL);
 
     if (NULL == memdatasource) {
-        ctx->err("rt_raster_dump_as_wktpolygons: Couldn't create a OGR Datasource to store polygons\n");
+        ctx->err("rt_raster_dump_as_wktpolygons: Couldn't create a OGR Datasource to store pols\n");
         return 0;
     }
 
@@ -2011,8 +2008,12 @@ rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
         **/
        if (iBandHasNodataValue) {
                pszQuery = (char *) ctx->alloc(50 * sizeof (char));
-               sprintf(pszQuery, "PixelValue != %f", &dBandNoData );           
-               OGR_L_SetAttributeFilter(hLayer, pszQuery);
+               sprintf(pszQuery, "PixelValue != %f", dBandNoData );            
+               OGRErr e = OGR_L_SetAttributeFilter(hLayer, pszQuery);
+               if (e != OGRERR_NONE) {
+                       ctx->warn("Error filtering NODATA values for band. All values will be treated as data values\n");
+               }
+                       
        }
 
        else {
@@ -2031,7 +2032,6 @@ rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
      *********************************************************************/
     nFeatureCount = OGR_L_GetFeatureCount(hLayer, TRUE);
 
-
     /* Allocate memory for pols */
     pols = (rt_geomval) ctx->alloc(nFeatureCount * sizeof (struct rt_geomval_t));
 
@@ -2070,9 +2070,9 @@ rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
                     * sizeof (char));
        strcpy(pols[j].geom, pszSrcText);
 
-       RASTER_DEBUGF(4, "Feature %d, Polygon: %s", j, pols[nCont].geom);
-       RASTER_DEBUGF(4, "Feature %d, value: %f", j, pols[nCont].val);
-       RASTER_DEBUGF(4, "Feature %d, srid: %d", j, pols[nCont].srid);
+       RASTER_DEBUGF(4, "Feature %d, Polygon: %s", j, pols[j].geom);
+       RASTER_DEBUGF(4, "Feature %d, value: %f", j, pols[j].val);
+       RASTER_DEBUGF(4, "Feature %d, srid: %d", j, pols[j].srid);
        
                /**
          * We can't use deallocator from rt_context, because it comes from
@@ -2088,7 +2088,7 @@ rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
     }
 
     if (pnElements)
-        *pnElements = nCont;
+        *pnElements = nFeatureCount;
 
     RASTER_DEBUG(3, "destroying GDAL MEM raster");
 
@@ -3571,28 +3571,28 @@ int32_t rt_raster_copy_band(rt_context ctx, rt_raster torast,
     /* Check bands limits */
     if (fromrast->numBands < 1)
     {
-        ctx->warn("Second raster has no band to copy");
+        ctx->warn("rt_raster_copy_band: Second raster has no band");
         return -1;
     }
     else if (fromindex < 0)
     {
-        ctx->warn("Band index for second raster to copy is smaller than 0. Defaulted to 1");
+        ctx->warn("rt_raster_copy_band: Band index for second raster < 0. Defaulted to 1");
         fromindex = 0;
     }
     else if (fromindex >= fromrast->numBands)
     {
-        ctx->warn("Band index for second raster to copy is greater than number of raster bands. Truncated from %u to %u", fromindex - 1, fromrast->numBands);
+        ctx->warn("rt_raster_copy_band: Band index for second raster > number of bands, truncated from %u to %u", fromindex - 1, fromrast->numBands);
         fromindex = fromrast->numBands - 1;
     }
 
     if (toindex < 0)
     {
-        ctx->warn("Band index for first raster to copy band to is smaller than 0. Defaulted to 1");
+        ctx->warn("rt_raster_copy_band: Band index for first raster < 0. Defaulted to 1");
         toindex = 0;
     }
     else if (toindex > torast->numBands)
     {
-        ctx->warn("Band index for first raster to copy to is greater than number of bands. Truncated from %u to %u", toindex - 1, torast->numBands);
+        ctx->warn("rt_raster_copy_band: Band index for first raster > number of bands, truncated from %u to %u", toindex - 1, torast->numBands);
         toindex = torast->numBands;
     }
 
index 3cac827239078a560ebe752284c4a91dd592698e..b356a946dfc7baf7065e0f06e9bae0a1184360b5 100644 (file)
@@ -10,7 +10,7 @@
 static rt_band addBand(rt_context ctx, rt_raster raster, rt_pixtype pixtype, int hasnodata, double nodataval);
 static void deepRelease(rt_context ctx, rt_raster raster);
 static void testBand1BB(rt_context ctx, rt_band band);
-static rt_raster fillRasterToPolygonize(rt_context ctx);
+static rt_raster fillRasterToPolygonize(rt_context ctx, int hasnodata, double nodatavalue);
 
 static rt_band
 addBand(rt_context ctx, rt_raster raster, rt_pixtype pixtype, int hasnodata, double nodataval)
@@ -48,7 +48,7 @@ deepRelease(rt_context ctx, rt_raster raster)
 
 
 static rt_raster
-fillRasterToPolygonize(rt_context ctx)
+fillRasterToPolygonize(rt_context ctx, int hasnodata, double nodatavalue)
 {
     /* Create raster */
 
@@ -82,7 +82,7 @@ fillRasterToPolygonize(rt_context ctx)
     
 
     /* Fill raster. Option 2: 9x9, 1 band */    
-    rt_band band = addBand(ctx, raster, PT_32BUI, 1, 0);
+    rt_band band = addBand(ctx, raster, PT_32BUI, hasnodata, nodatavalue);
     
     {
         int x, y;
@@ -1113,8 +1113,12 @@ main()
 
     {   /* Check ST_AsPolygon */
         printf("Testing polygonize function\n");
-        rt_raster rt = fillRasterToPolygonize(ctx);
+
+               /* First test: NODATA value = -1 */
+        rt_raster rt = fillRasterToPolygonize(ctx, 1, -1.0);
                                
+               printf("*** POLYGONIZE WITH NODATA = -1.0\n");
+
                /* We can check rt_raster_has_no_band here too */
                CHECK(!rt_raster_has_no_band(ctx, rt, 1));
 
@@ -1132,14 +1136,105 @@ main()
         
         rt_geomval gv = (rt_geomval) rt_raster_dump_as_wktpolygons(ctx, rt, 1, &nPols);
 
-
         CHECK_EQUALS_DOUBLE(gv[0].val, 1.0);
-        CHECK(!strcmp(gv[0].geom,"POLYGON ((3 1,3 2,2 2,2 3,1 3,1 6,2 6,2 7,3 7,3 8,5 8,5 6,3 6,3 3,4 3,5 3,5 1,3 1))"));
+        CHECK(!strcmp(gv[0].geom, "POLYGON ((3 1,3 2,2 2,2 3,1 3,1 6,2 6,2 7,3 7,3 8,5 8,5 6,3 6,3 3,4 3,5 3,5 1,3 1))"));
+
+               CHECK_EQUALS_DOUBLE(gv[1].val, 0.0);
+               CHECK(!strcmp(gv[1].geom, "POLYGON ((3 3,3 6,6 6,6 3,3 3))"));
+
+        CHECK_EQUALS_DOUBLE(gv[2].val, 2.0);
+        CHECK(!strcmp(gv[2].geom, "POLYGON ((5 1,5 3,6 3,6 6,5 6,5 8,6 8,6 7,7 7,7 6,8 6,8 3,7 3,7 2,6 2,6 1,5 1))"));   
+
+               CHECK_EQUALS_DOUBLE(gv[3].val, 0.0);
+               CHECK(!strcmp(gv[3].geom, "POLYGON ((0 0,0 9,9 9,9 0,0 0),(6 7,6 8,3 8,3 7,2 7,2 6,1 6,1 3,2 3,2 2,3 2,3 1,6 1,6 2,7 2,7 3,8 3,8 6,7 6,7 7,6 7))"));
+               
+        
+        rt_raster_destroy(ctx, rt);
+
+       
+               /* Second test: NODATA value = 1 */
+        rt = fillRasterToPolygonize(ctx, 1, 1.0);
+                               
+               /* We can check rt_raster_has_no_band here too */
+               CHECK(!rt_raster_has_no_band(ctx, rt, 1));
+
+        nPols = 0;
+        
+        gv = (rt_geomval) rt_raster_dump_as_wktpolygons(ctx, rt, 1, &nPols);
+
+
+               CHECK_EQUALS_DOUBLE(gv[0].val, 0.0);
+               CHECK(!strcmp(gv[0].geom, "POLYGON ((3 3,3 6,6 6,6 3,3 3))"));
 
         CHECK_EQUALS_DOUBLE(gv[1].val, 2.0);
-        CHECK(!strcmp(gv[1].geom,"POLYGON ((5 1,5 3,6 3,6 6,5 6,5 8,6 8,6 7,7 7,7 6,8 6,8 3,7 3,7 2,6 2,6 1,5 1))"));   
+        CHECK(!strcmp(gv[1].geom, "POLYGON ((5 1,5 3,6 3,6 6,5 6,5 8,6 8,6 7,7 7,7 6,8 6,8 3,7 3,7 2,6 2,6 1,5 1))"));   
+
+               CHECK_EQUALS_DOUBLE(gv[2].val, 0.0);
+               CHECK(!strcmp(gv[2].geom, "POLYGON ((0 0,0 9,9 9,9 0,0 0),(6 7,6 8,3 8,3 7,2 7,2 6,1 6,1 3,2 3,2 2,3 2,3 1,6 1,6 2,7 2,7 3,8 3,8 6,7 6,7 7,6 7))"));
+        rt_raster_destroy(ctx, rt);
+               /* Third test: NODATA value = 2 */
+        rt = fillRasterToPolygonize(ctx, 1, 2.0);
+                               
+               /* We can check rt_raster_has_no_band here too */
+               CHECK(!rt_raster_has_no_band(ctx, rt, 1));
+
+        nPols = 0;
         
+        gv = (rt_geomval) rt_raster_dump_as_wktpolygons(ctx, rt, 1, &nPols);
+
+        CHECK_EQUALS_DOUBLE(gv[0].val, 1.0);
+        CHECK(!strcmp(gv[0].geom, "POLYGON ((3 1,3 2,2 2,2 3,1 3,1 6,2 6,2 7,3 7,3 8,5 8,5 6,3 6,3 3,4 3,5 3,5 1,3 1))"));
+
+               CHECK_EQUALS_DOUBLE(gv[1].val, 0.0);
+               CHECK(!strcmp(gv[1].geom, "POLYGON ((3 3,3 6,6 6,6 3,3 3))"));
+
+               CHECK_EQUALS_DOUBLE(gv[2].val, 0.0);
+               CHECK(!strcmp(gv[2].geom, "POLYGON ((0 0,0 9,9 9,9 0,0 0),(6 7,6 8,3 8,3 7,2 7,2 6,1 6,1 3,2 3,2 2,3 2,3 1,6 1,6 2,7 2,7 3,8 3,8 6,7 6,7 7,6 7))"));
         rt_raster_destroy(ctx, rt);
+
+               /* Fourth test: NODATA value = 0 */
+        rt = fillRasterToPolygonize(ctx, 1, 0.0);
+                               
+               /* We can check rt_raster_has_no_band here too */
+               CHECK(!rt_raster_has_no_band(ctx, rt, 1));
+
+        nPols = 0;
+        
+        gv = (rt_geomval) rt_raster_dump_as_wktpolygons(ctx, rt, 1, &nPols);
+               
+        CHECK_EQUALS_DOUBLE(gv[0].val, 1.0);
+        CHECK(!strcmp(gv[0].geom, "POLYGON ((3 1,3 2,2 2,2 3,1 3,1 6,2 6,2 7,3 7,3 8,5 8,5 6,3 6,3 3,4 3,5 3,5 1,3 1))"));
+        
+               CHECK_EQUALS_DOUBLE(gv[1].val, 2.0);
+        CHECK(!strcmp(gv[1].geom, "POLYGON ((5 1,5 3,6 3,6 6,5 6,5 8,6 8,6 7,7 7,7 6,8 6,8 3,7 3,7 2,6 2,6 1,5 1))"));   
+
+               rt_raster_destroy(ctx, rt);
+               /* Last test: There is no NODATA value (all values are valid) */
+        rt = fillRasterToPolygonize(ctx, 0, 1.0);
+               
+               /* We can check rt_raster_has_no_band here too */
+               CHECK(!rt_raster_has_no_band(ctx, rt, 1));
+
+        nPols = 0;
+        
+        gv = (rt_geomval) rt_raster_dump_as_wktpolygons(ctx, rt, 1, &nPols);
+
+        CHECK_EQUALS_DOUBLE(gv[0].val, 1.0);
+        CHECK(!strcmp(gv[0].geom, "POLYGON ((3 1,3 2,2 2,2 3,1 3,1 6,2 6,2 7,3 7,3 8,5 8,5 6,3 6,3 3,4 3,5 3,5 1,3 1))"));
+
+               CHECK_EQUALS_DOUBLE(gv[1].val, 0.0);
+               CHECK(!strcmp(gv[1].geom, "POLYGON ((3 3,3 6,6 6,6 3,3 3))"));
+
+        CHECK_EQUALS_DOUBLE(gv[2].val, 2.0);
+        CHECK(!strcmp(gv[2].geom, "POLYGON ((5 1,5 3,6 3,6 6,5 6,5 8,6 8,6 7,7 7,7 6,8 6,8 3,7 3,7 2,6 2,6 1,5 1))"));   
+
+               CHECK_EQUALS_DOUBLE(gv[3].val, 0.0);
+               CHECK(!strcmp(gv[3].geom, "POLYGON ((0 0,0 9,9 9,9 0,0 0),(6 7,6 8,3 8,3 7,2 7,2 6,1 6,1 3,2 3,2 2,3 2,3 1,6 1,6 2,7 2,7 3,8 3,8 6,7 6,7 7,6 7))"));
+               rt_raster_destroy(ctx, rt);
+
     }
 
     printf("Testing 1BB band\n");