From 83f25d261e790f1409fa217dae7d750e55b0b7a1 Mon Sep 17 00:00:00 2001 From: Bborie Park Date: Mon, 24 Sep 2012 15:07:58 +0000 Subject: [PATCH] Changed output array of ST_Neighborhood to have dimensions of Y,X instead of X,Y. This matches that found for GDAL blocks. git-svn-id: http://svn.osgeo.org/postgis/trunk@10323 b70326c6-7e19-0410-871a-916f4a2858ee --- raster/rt_core/rt_api.c | 30 +++++++++++--------- raster/rt_core/rt_api.h | 4 ++- raster/rt_pg/rt_pg.c | 8 ++++-- raster/test/regress/rt_neighborhood_expected | 10 +++---- 4 files changed, 29 insertions(+), 23 deletions(-) diff --git a/raster/rt_core/rt_api.c b/raster/rt_core/rt_api.c index d07e3c7db..b3e694eaf 100644 --- a/raster/rt_core/rt_api.c +++ b/raster/rt_core/rt_api.c @@ -1151,7 +1151,9 @@ int rt_pixtype_compare_clamped_values(rt_pixtype pixtype, double val, double ref /*- rt_pixel ----------------------------------------------------------*/ /* - * Convert an array of rt_pixel objects to two 2D arrays of value and NODATA + * Convert an array of rt_pixel objects to two 2D arrays of value and NODATA. + * The dimensions of the returned 2D array are [Y][X], going by row Y and + * then column X. * * @param npixel: array of rt_pixel objects * @param count: number of elements in npixel @@ -1193,19 +1195,19 @@ int rt_pixel_set_to_array( dim[1] = distancey * 2 + 1; RASTER_DEBUGF(4, "dimensions = %d x %d", dim[0], dim[1]); - /* establish 2D arrays (X axis) */ - values = rtalloc(sizeof(double *) * dim[0]); - nodatas = rtalloc(sizeof(int *) * dim[0]); + /* establish 2D arrays (Y axis) */ + values = rtalloc(sizeof(double *) * dim[1]); + nodatas = rtalloc(sizeof(int *) * dim[1]); if (values == NULL || nodatas == NULL) { rterror("rt_pixel_set_to_array: Unable to allocate memory for 2D array"); return 0; } - /* initialize Y axis */ - for (i = 0; i < dim[0]; i++) { - values[i] = rtalloc(sizeof(double) * dim[1]); - nodatas[i] = rtalloc(sizeof(int) * dim[1]); + /* initialize X axis */ + for (i = 0; i < dim[1]; i++) { + values[i] = rtalloc(sizeof(double) * dim[0]); + nodatas[i] = rtalloc(sizeof(int) * dim[0]); if (values[i] == NULL || nodatas[i] == NULL) { rterror("rt_pixel_set_to_array: Unable to allocate memory for dimension of 2D array"); @@ -1231,14 +1233,14 @@ int rt_pixel_set_to_array( } /* set values to 0 */ - memset(values[i], 0, sizeof(double) * dim[1]); + memset(values[i], 0, sizeof(double) * dim[0]); /* set nodatas to 1 */ - for (j = 0; j < dim[1]; j++) + for (j = 0; j < dim[0]; j++) nodatas[i][j] = 1; } - /* get zero, zero of grid */ + /* get 0,0 of grid */ zero[0] = x - distancex; zero[1] = y - distancey; @@ -1253,10 +1255,10 @@ int rt_pixel_set_to_array( RASTER_DEBUGF(4, "absolute x,y: %d x %d", npixel[i].x, npixel[i].y); RASTER_DEBUGF(4, "relative x,y: %d x %d", _x, _y); - values[_x][_y] = npixel[i].value; - nodatas[_x][_y] = 0; + values[_y][_x] = npixel[i].value; + nodatas[_y][_x] = 0; - RASTER_DEBUGF(4, "(x, y, nodata, value) = (%d, %d, %d, %f)", _x, _y, nodatas[_x][_y], values[_x][_y]); + RASTER_DEBUGF(4, "(x, y, nodata, value) = (%d, %d, %d, %f)", _x, _y, nodatas[_y][_x], values[_y][_x]); } *value = &(*values); diff --git a/raster/rt_core/rt_api.h b/raster/rt_core/rt_api.h index 9911278b2..1f0cfd5e5 100644 --- a/raster/rt_core/rt_api.h +++ b/raster/rt_core/rt_api.h @@ -339,7 +339,9 @@ int rt_pixtype_compare_clamped_values(rt_pixtype pixtype, double val, double ref /*- rt_pixel ----------------------------------------------------------*/ /* - * Convert an array of rt_pixel objects to two 2D arrays of value and NODATA + * Convert an array of rt_pixel objects to two 2D arrays of value and NODATA. + * The dimensions of the returned 2D array are [Y][X], going by row Y and + * then column X. * * @param npixel: array of rt_pixel objects * @param count: number of elements in npixel diff --git a/raster/rt_pg/rt_pg.c b/raster/rt_pg/rt_pg.c index 1e92c9d80..558f06126 100644 --- a/raster/rt_pg/rt_pg.c +++ b/raster/rt_pg/rt_pg.c @@ -3966,8 +3966,10 @@ Datum RASTER_neighborhood(PG_FUNCTION_ARGS) /* copy values from 2D arrays to 1D arrays */ k = 0; - for (i = 0; i < dim[0]; i++) { - for (j = 0; j < dim[1]; j++) { + /* Y-axis */ + for (i = 0; i < dim[1]; i++) { + /* X-axis */ + for (j = 0; j < dim[0]; j++) { nodata1D[k] = (bool) nodata2D[i][j]; if (!nodata1D[k]) value1D[k] = Float8GetDatum(value2D[i][j]); @@ -3979,7 +3981,7 @@ Datum RASTER_neighborhood(PG_FUNCTION_ARGS) } /* no more need for 2D arrays */ - for (i = 0; i < dim[0]; i++) { + for (i = 0; i < dim[1]; i++) { pfree(value2D[i]); pfree(nodata2D[i]); } diff --git a/raster/test/regress/rt_neighborhood_expected b/raster/test/regress/rt_neighborhood_expected index 61dc6fe4a..a45cdbd3b 100644 --- a/raster/test/regress/rt_neighborhood_expected +++ b/raster/test/regress/rt_neighborhood_expected @@ -1,15 +1,15 @@ NOTICE: table "raster_neighborhood" does not exist, skipping {{NULL,NULL,NULL},{NULL,NULL,1},{NULL,1,1}} {{0,0,0},{0,0,1},{0,1,1}} -{{NULL,1,1},{1,1,NULL},{1,1,1}} -{{1,1,1},{1,1,1},{1,NULL,1}} -{{1,1,NULL,1,1},{1,1,1,1,NULL},{NULL,1,1,1,1},{1,1,NULL,1,1},{1,1,1,1,NULL}} +{{NULL,1,1},{1,1,1},{1,NULL,1}} +{{1,1,1},{1,1,NULL},{1,1,1}} +{{1,1,NULL,1,1},{1,1,1,1,1},{NULL,1,1,NULL,1},{1,1,1,1,1},{1,NULL,1,1,NULL}} {{1,NULL,NULL},{NULL,NULL,NULL},{NULL,NULL,NULL}} NOTICE: Pixel has no neighbors for band at distance 1 x 1 NOTICE: Pixel has no neighbors for band at distance 1 x 1 -{{NULL,NULL,NULL},{NULL,NULL,NULL},{NULL,1,1}} +{{NULL,NULL,NULL},{NULL,NULL,1},{NULL,NULL,1}} NOTICE: Pixel has no neighbors for band at distance 1 x 1 NOTICE: Pixel has no neighbors for band at distance 3 x 3 {{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,NULL,1},{1,1,1,1,1},{1,NULL,1,1,1}} +{{1,1,1,1,1},{NULL,1,1,1,NULL},{1,1,1,1,1}} -- 2.40.0