From: Sandro Santilli Date: Wed, 1 Feb 2012 10:15:53 +0000 (+0000) Subject: Implement RAST.X and RAST.Y keyword substitution in ST_MapAlgebraExpr X-Git-Tag: 2.0.0alpha3~9 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b06105cb0da164bb63971f5dfdc09bd3bd47848e;p=postgis Implement RAST.X and RAST.Y keyword substitution in ST_MapAlgebraExpr Includes regression test and documentation update. See #1519. git-svn-id: http://svn.osgeo.org/postgis/trunk@8994 b70326c6-7e19-0410-871a-916f4a2858ee --- diff --git a/doc/reference_raster.xml b/doc/reference_raster.xml index e2908d58d..5075b27c3 100644 --- a/doc/reference_raster.xml +++ b/doc/reference_raster.xml @@ -5576,7 +5576,12 @@ WHERE A.rid =2 ) As foo; Creates a new one band raster formed by applying a valid PostgreSQL algebraic operation defined by the expression on the input raster (rast). If no band is specified band 1 is assumed. The new raster will have the same georeference, width, and height as the original raster but will only have one band. If pixeltype is passed in, then the new raster will have a band of that pixeltype. If pixeltype is passed NULL, then the new raster band will have the same pixeltype as the input rast band. - Use the term rast to refer to the pixel value of the original band. + + In the expression you can use the term + RAST to refer to the pixel value of + the original band, RAST.X to refer to + the 1-based pixel column index, RAST.Y + to refer to the 1-based pixel row index. Availability: 2.0.0 diff --git a/raster/rt_pg/rt_pg.c b/raster/rt_pg/rt_pg.c index 64acf7e6c..7561d2b4f 100644 --- a/raster/rt_pg/rt_pg.c +++ b/raster/rt_pg/rt_pg.c @@ -2527,7 +2527,6 @@ Datum RASTER_mapAlgebraExpr(PG_FUNCTION_ARGS) rt_pixtype newpixeltype; int skipcomputation = 0; char strnewval[50]; - int count = 0; int len = 0; int ret = -1; TupleDesc tupdesc; @@ -2941,6 +2940,7 @@ Datum RASTER_mapAlgebraExpr(PG_FUNCTION_ARGS) for (x = 0; x < width; x++) { for(y = 0; y < height; y++) { + char *tmp; ret = rt_band_get_pixel(band, x, y, &r); /** @@ -2949,11 +2949,16 @@ Datum RASTER_mapAlgebraExpr(PG_FUNCTION_ARGS) **/ if (ret != -1 && FLT_NEQ(r, newnodatavalue)) { if (skipcomputation == 0) { - sprintf(strnewval, "%f", r); if (initexpr != NULL) { - count = 0; - newexpr = rtpg_strreplace(initexpr, "RAST", strnewval, &count); + sprintf(strnewval, "%d", x+1); + newexpr = rtpg_strreplace(initexpr, "RAST.X", strnewval, NULL); + sprintf(strnewval, "%d", y+1); + tmp = rtpg_strreplace(newexpr, "RAST.Y", strnewval, NULL); + pfree(newexpr); newexpr=tmp; + sprintf(strnewval, "%f", r); + tmp = rtpg_strreplace(newexpr, "RAST", strnewval, NULL); + pfree(newexpr); newexpr=tmp; POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebraExpr: (%dx%d), " "r = %s, newexpr = %s", diff --git a/raster/test/regress/rt_mapalgebraexpr.sql b/raster/test/regress/rt_mapalgebraexpr.sql index a157111e4..78a87e945 100644 --- a/raster/test/regress/rt_mapalgebraexpr.sql +++ b/raster/test/regress/rt_mapalgebraexpr.sql @@ -2,22 +2,30 @@ SELECT ST_MapAlgebraExpr(NULL, 1, NULL, 'rast + 20', 2) IS NULL FROM ST_TestRaster(0, 0, -1) rast; -- Test empty raster -SELECT ST_IsEmpty(ST_MapAlgebraExpr(ST_MakeEmptyRaster(0, 10, 0, 0, 1, 1, 1, 1, -1), 1, NULL, 'rast + 20', 2)); +SELECT 'T1', ST_IsEmpty(ST_MapAlgebraExpr(ST_MakeEmptyRaster(0, 10, 0, 0, 1, 1, 1, 1, -1), 1, NULL, 'rast + 20', 2)); -- Test hasnoband raster -SELECT ST_HasNoBand(ST_MapAlgebraExpr(ST_MakeEmptyRaster(10, 10, 0, 0, 1, 1, 1, 1, -1), 1, NULL, 'rast + 20', 2)); +SELECT 'T2', ST_HasNoBand(ST_MapAlgebraExpr(ST_MakeEmptyRaster(10, 10, 0, 0, 1, 1, 1, 1, -1), 1, NULL, 'rast + 20', 2)); -- Test hasnodata value -SELECT ST_Value(rast, 1, 1), ST_Value(ST_MapAlgebraExpr(ST_SetBandNoDataValue(rast, NULL), 1, NULL, 'rast + 20', 2), 1, 1) FROM ST_TestRaster(0, 0, -1) rast; +SELECT 'T3', ST_Value(rast, 1, 1), ST_Value(ST_MapAlgebraExpr(ST_SetBandNoDataValue(rast, NULL), 1, NULL, 'rast + 20', 2), 1, 1) FROM ST_TestRaster(0, 0, -1) rast; -- Test nodata value -SELECT ST_Value(rast, 1, 1), ST_Value(ST_MapAlgebraExpr(rast, 1, NULL, 'rast + 20', 2), 1, 1) FROM ST_TestRaster(0, 0, -1) rast; +SELECT 'T4', ST_Value(rast, 1, 1), ST_Value(ST_MapAlgebraExpr(rast, 1, NULL, 'rast + 20', 2), 1, 1) FROM ST_TestRaster(0, 0, -1) rast; -- Test 'rast' expression -SELECT ST_Value(rast, 1, 1), ST_Value(ST_MapAlgebraExpr(rast, 1, NULL, 'rast', 2), 1, 1) FROM ST_TestRaster(0, 0, -1) rast; -SELECT ST_Value(rast, 1, 1), ST_Value(ST_MapAlgebraExpr(ST_SetBandNoDataValue(rast, NULL), 1, NULL, 'rast'), 1, 1) FROM ST_TestRaster(0, 0, -1) rast; +SELECT 'T5', ST_Value(rast, 1, 1), ST_Value(ST_MapAlgebraExpr(rast, 1, NULL, 'rast', 2), 1, 1) FROM ST_TestRaster(0, 0, -1) rast; +SELECT 'T6', ST_Value(rast, 1, 1), ST_Value(ST_MapAlgebraExpr(ST_SetBandNoDataValue(rast, NULL), 1, NULL, 'rast'), 1, 1) FROM ST_TestRaster(0, 0, -1) rast; -- Test pixeltype -SELECT ST_Value(rast, 1, 1), ST_Value(ST_MapAlgebraExpr(rast, 1, '4BUI', 'rast + 20', 2), 1, 1) FROM ST_TestRaster(0, 0, 100) rast; -SELECT ST_Value(rast, 1, 1), ST_Value(ST_MapAlgebraExpr(rast, 1, '4BUId', 'rast + 20', 2), 1, 1) FROM ST_TestRaster(0, 0, 100) rast; -SELECT ST_Value(rast, 1, 1), ST_Value(ST_MapAlgebraExpr(rast, 1, '2BUI', 'rast + 20', 2), 1, 1) FROM ST_TestRaster(0, 0, 101) rast; +SELECT 'T7', ST_Value(rast, 1, 1), ST_Value(ST_MapAlgebraExpr(rast, 1, '4BUI', 'rast + 20', 2), 1, 1) FROM ST_TestRaster(0, 0, 100) rast; +SELECT 'T8', ST_Value(rast, 1, 1), ST_Value(ST_MapAlgebraExpr(rast, 1, '4BUId', 'rast + 20', 2), 1, 1) FROM ST_TestRaster(0, 0, 100) rast; +SELECT 'T9', ST_Value(rast, 1, 1), ST_Value(ST_MapAlgebraExpr(rast, 1, '2BUI', 'rast + 20', 2), 1, 1) FROM ST_TestRaster(0, 0, 101) rast; + +-- Test 'rast.x' and 'rast.y' substitutions expression +SELECT 'T10.'||x||'.'||y, ST_Value(rast, x, y), + ST_Value(ST_MapAlgebraExpr(rast, 1, NULL, 'round(rast*rast.x/rast.y+rast)'), x, y) +FROM ST_TestRaster(0, 0, 10) rast, + generate_series(6, 8) as x, + generate_series(2, 4) as y +ORDER BY x, y; diff --git a/raster/test/regress/rt_mapalgebraexpr_expected b/raster/test/regress/rt_mapalgebraexpr_expected index fe001b257..3aa67d4a7 100644 --- a/raster/test/regress/rt_mapalgebraexpr_expected +++ b/raster/test/regress/rt_mapalgebraexpr_expected @@ -1,15 +1,24 @@ NOTICE: Raster is NULL. Returning NULL t NOTICE: Raster is empty. Returning an empty raster -t +T1|t NOTICE: Raster does not have the required band. Returning a raster without a band -t -|19 -|2 -|2 +T2|t +T3||19 +T4||2 +T5||2 NOTICE: rt_raster_copy_band: Second raster has no band NOTICE: Could not find raster band of index 1 when getting pixel value. Returning NULL -| -100| -100|120 -101| +T6|| +T7|100| +T8|100|120 +T9|101| +T10.6.2|10|40 +T10.6.3|10|30 +T10.6.4|10|25 +T10.7.2|10|45 +T10.7.3|10|33 +T10.7.4|10|28 +T10.8.2|10|50 +T10.8.3|10|37 +T10.8.4|10|30