]> granicus.if.org Git - postgis/commitdiff
Implement RAST.X and RAST.Y keyword substitution in ST_MapAlgebraExpr
authorSandro Santilli <strk@keybit.net>
Wed, 1 Feb 2012 10:15:53 +0000 (10:15 +0000)
committerSandro Santilli <strk@keybit.net>
Wed, 1 Feb 2012 10:15:53 +0000 (10:15 +0000)
Includes regression test and documentation update. See #1519.

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

doc/reference_raster.xml
raster/rt_pg/rt_pg.c
raster/test/regress/rt_mapalgebraexpr.sql
raster/test/regress/rt_mapalgebraexpr_expected

index e2908d58d999b93fc6c51d731d38734002b99b8b..5075b27c3abe3d279a62b4963e22a621985f7fbf 100644 (file)
@@ -5576,7 +5576,12 @@ WHERE A.rid =2 ) As foo;
                                <para>Creates a new one band raster formed by applying a valid PostgreSQL algebraic operation defined by the <varname>expression</varname> on the input raster (<varname>rast</varname>). If no <varname>band</varname> 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.</para>
         
                  <para>If <varname>pixeltype</varname> 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 <varname>rast</varname> band.</para>
-                 <para>Use the term <varname>rast</varname> to refer to the pixel value of the original band.</para>
+
+                 <para>In the expression you can use the term
+                 <varname>RAST</varname> to refer to the pixel value of
+                 the original band, <varname>RAST.X</varname> to refer to
+                 the 1-based pixel column index, <varname>RAST.Y</varname>
+                 to refer to the 1-based pixel row index.</para>
                        
                  <para>Availability: 2.0.0 </para>
              </refsection>
index 64acf7e6c73e78566fce21d60d35bfab07bb62a9..7561d2b4fa2de7c536ab2f0370d3190a75949454 100644 (file)
@@ -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",
index a157111e4000338d062456e8f7b9845c40239825..78a87e9451c65bbbe6c658df511b0321565c978b 100644 (file)
@@ -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;
index fe001b25720551d3c70307f617548f74a4c0d6dd..3aa67d4a7ae3cf4074e5f097e9e086e94e52643a 100644 (file)
@@ -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