]> granicus.if.org Git - postgis/commitdiff
Additional regression tests for ST_Neighborhood and tweaked to support a
authorBborie Park <bkpark at ucdavis.edu>
Wed, 26 Sep 2012 15:56:17 +0000 (15:56 +0000)
committerBborie Park <bkpark at ucdavis.edu>
Wed, 26 Sep 2012 15:56:17 +0000 (15:56 +0000)
distance values of zero for one axis.

git-svn-id: http://svn.osgeo.org/postgis/trunk@10333 b70326c6-7e19-0410-871a-916f4a2858ee

raster/rt_core/rt_api.c
raster/rt_pg/rt_pg.c
raster/test/regress/rt_neighborhood.sql
raster/test/regress/rt_neighborhood_expected

index ea436992f10c2ca4a867c2b22c7a2087ecdeed73..2f2aecfc8272e3a24de3d057a4177385bea6eff4 100644 (file)
@@ -2417,14 +2417,9 @@ int rt_band_get_nearest_pixel(
        distance[0] = distancex;
        distance[1] = distancey;
 
-       /* no distance */
+       /* no distance, means get nearest pixels and return */
        if (!distance[0] && !distance[1])
                d0 = 1;
-       /* distance for X or Y is missing */
-       else if (!distance[0] || !distance[1]) {
-               rterror("rt_band_get_nearest_pixel: distancex and distancey must both be zero if one is zero");
-               return 0;
-       }
 
        RASTER_DEBUGF(4, "Selected pixel: %d x %d", x, y);
        RASTER_DEBUGF(4, "Distances: %d x %d", distance[0], distance[1]);
index 77c4ed9dd315225c77daefa3bfa510dc1ab3ef54..738c3af793d610372556aac3c1642bf76019f99b 100644 (file)
@@ -3815,8 +3815,8 @@ Datum RASTER_neighborhood(PG_FUNCTION_ARGS)
 
        /* distance X axis */
        distance[0] = PG_GETARG_INT32(4);
-       if (distance[0] < 1) {
-               elog(NOTICE, "Invalid value for distancex (must be greater than zero). Returning NULL");
+       if (distance[0] < 0) {
+               elog(NOTICE, "Invalid value for distancex (must be >= zero). Returning NULL");
                rt_raster_destroy(raster);
                PG_FREE_IF_COPY(pgraster, 0);
                PG_RETURN_NULL();
@@ -3825,8 +3825,8 @@ Datum RASTER_neighborhood(PG_FUNCTION_ARGS)
 
        /* distance Y axis */
        distance[1] = PG_GETARG_INT32(5);
-       if (distance[1] < 1) {
-               elog(NOTICE, "Invalid value for distancey (must be greater than zero). Returning NULL");
+       if (distance[1] < 0) {
+               elog(NOTICE, "Invalid value for distancey (must be >= zero). Returning NULL");
                rt_raster_destroy(raster);
                PG_FREE_IF_COPY(pgraster, 0);
                PG_RETURN_NULL();
@@ -3847,22 +3847,26 @@ Datum RASTER_neighborhood(PG_FUNCTION_ARGS)
        }
 
        /* get neighborhood */
-       count = rt_band_get_nearest_pixel(
-               band,
-               _x, _y,
-               distance[0], distance[1],
-               exclude_nodata_value,
-               &npixels
-       );
-       /* error */
-       if (count < 0) {
-               elog(NOTICE, "Unable to get the pixel's neighborhood for band at index %d", bandindex);
+       count = 0;
+       npixels = NULL;
+       if (distance[0] > 0 || distance[1] > 0) {
+               count = rt_band_get_nearest_pixel(
+                       band,
+                       _x, _y,
+                       distance[0], distance[1],
+                       exclude_nodata_value,
+                       &npixels
+               );
+               /* error */
+               if (count < 0) {
+                       elog(NOTICE, "Unable to get the pixel's neighborhood for band at index %d", bandindex);
                        
-               rt_band_destroy(band);
-               rt_raster_destroy(raster);
-               PG_FREE_IF_COPY(pgraster, 0);
+                       rt_band_destroy(band);
+                       rt_raster_destroy(raster);
+                       PG_FREE_IF_COPY(pgraster, 0);
 
-               PG_RETURN_NULL();
+                       PG_RETURN_NULL();
+               }
        }
 
        /* get pixel's value */
@@ -3934,13 +3938,14 @@ Datum RASTER_neighborhood(PG_FUNCTION_ARGS)
        PG_FREE_IF_COPY(pgraster, 0);
 
        /* convert set of rt_pixel to 2D array */
+       /* dim is passed with element 0 being Y-axis and element 1 being X-axis */
        count = rt_pixel_set_to_array(
                npixels, count,
                _x, _y,
                distance[0], distance[1],
                &value2D,
                &nodata2D,
-               &(dim[0]), &(dim[1])
+               &(dim[1]), &(dim[0])
        );
        pfree(npixels);
        if (!count) {
@@ -3968,9 +3973,9 @@ Datum RASTER_neighborhood(PG_FUNCTION_ARGS)
        /* copy values from 2D arrays to 1D arrays */
        k = 0;
        /* Y-axis */
-       for (i = 0; i < dim[1]; i++) {
+       for (i = 0; i < dim[0]; i++) {
                /* X-axis */
-               for (j = 0; j < dim[0]; j++) {
+               for (j = 0; j < dim[1]; j++) {
                        nodata1D[k] = (bool) nodata2D[i][j];
                        if (!nodata1D[k])
                                value1D[k] = Float8GetDatum(value2D[i][j]);
@@ -3982,7 +3987,7 @@ Datum RASTER_neighborhood(PG_FUNCTION_ARGS)
        }
 
        /* no more need for 2D arrays */
-       for (i = 0; i < dim[1]; i++) {
+       for (i = 0; i < dim[0]; i++) {
                pfree(value2D[i]);
                pfree(nodata2D[i]);
        }
index f5da8cf9ae5ddf75c91ff7b19d03806372fbca93..25bc74bb9964e8604f38ac975c93e9f5b9a69bca 100644 (file)
@@ -77,8 +77,14 @@ FROM raster_neighborhood;
 SELECT 
        ST_Neighborhood(rast, 1, 4, 4, 1, 1)
 FROM raster_neighborhood;
+SELECT 
+       ST_Neighborhood(rast, 1, 4, 4, 2, 2)
+FROM raster_neighborhood;
 SELECT 
        ST_Neighborhood(rast, 1, 4, 4, 1, 2)
 FROM raster_neighborhood;
+SELECT 
+       ST_Neighborhood(rast, 1, 4, 4, 1, 0)
+FROM raster_neighborhood;
 
 DROP TABLE IF EXISTS raster_neighborhood;
index 3844e3c13ee2162f5f667db363cb7f3dd012ed88..32ef039619c835c579f0124337bc06a865110d8e 100644 (file)
@@ -12,4 +12,6 @@ NOTICE:  table "raster_neighborhood" does not exist, skipping
 {{NULL,NULL,NULL,NULL,NULL,NULL,NULL},{NULL,NULL,NULL,NULL,NULL,NULL,NULL},{NULL,NULL,NULL,NULL,NULL,NULL,NULL},{NULL,NULL,NULL,NULL,NULL,NULL,NULL},{NULL,NULL,NULL,NULL,NULL,NULL,NULL},{NULL,NULL,NULL,NULL,NULL,NULL,NULL},{NULL,NULL,NULL,NULL,NULL,NULL,NULL}}
 {{0,0,0,0,0,0,0},{0,0,0,0,0,0,0},{0,0,0,0,0,0,0},{0,0,0,0,0,0,0},{0,0,0,0,0,0,0},{0,0,0,0,0,0,0},{0,0,0,0,0,0,0}}
 {{1,1,NULL},{1,1,1},{NULL,1,1}}
-{{1,1,1,1,1},{NULL,1,1,1,NULL},{1,1,1,1,1}}
+{{1,1,1,1,1},{NULL,1,1,NULL,1},{1,1,1,1,1},{1,NULL,1,1,NULL},{1,1,1,1,1}}
+{{1,1,1},{1,1,NULL},{1,1,1},{NULL,1,1},{1,1,1}}
+{{1,1,1}}