From 7a2facadff9385f4211dd13e8132df3720ed3cf9 Mon Sep 17 00:00:00 2001 From: Bborie Park Date: Wed, 26 Sep 2012 15:56:17 +0000 Subject: [PATCH] Additional regression tests for ST_Neighborhood and tweaked to support a 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 | 7 +-- raster/rt_pg/rt_pg.c | 49 +++++++++++--------- raster/test/regress/rt_neighborhood.sql | 6 +++ raster/test/regress/rt_neighborhood_expected | 4 +- 4 files changed, 37 insertions(+), 29 deletions(-) diff --git a/raster/rt_core/rt_api.c b/raster/rt_core/rt_api.c index ea436992f..2f2aecfc8 100644 --- a/raster/rt_core/rt_api.c +++ b/raster/rt_core/rt_api.c @@ -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]); diff --git a/raster/rt_pg/rt_pg.c b/raster/rt_pg/rt_pg.c index 77c4ed9dd..738c3af79 100644 --- a/raster/rt_pg/rt_pg.c +++ b/raster/rt_pg/rt_pg.c @@ -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]); } diff --git a/raster/test/regress/rt_neighborhood.sql b/raster/test/regress/rt_neighborhood.sql index f5da8cf9a..25bc74bb9 100644 --- a/raster/test/regress/rt_neighborhood.sql +++ b/raster/test/regress/rt_neighborhood.sql @@ -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; diff --git a/raster/test/regress/rt_neighborhood_expected b/raster/test/regress/rt_neighborhood_expected index 3844e3c13..32ef03961 100644 --- a/raster/test/regress/rt_neighborhood_expected +++ b/raster/test/regress/rt_neighborhood_expected @@ -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}} -- 2.40.0