]> granicus.if.org Git - postgis/commitdiff
Implemented new keywords for 1-raster ST_MapAlgebraExpr (#1525)
authorSandro Santilli <strk@keybit.net>
Sat, 4 Feb 2012 17:53:39 +0000 (17:53 +0000)
committerSandro Santilli <strk@keybit.net>
Sat, 4 Feb 2012 17:53:39 +0000 (17:53 +0000)
git-svn-id: http://svn.osgeo.org/postgis/trunk@9031 b70326c6-7e19-0410-871a-916f4a2858ee

raster/rt_pg/rt_pg.c
raster/test/regress/rt_mapalgebraexpr.sql

index f5e79d12e97a3b723c7a105341b2e25ebfe6049a..3c71cb93b56455f9bc6302681f68a38dab733809 100644 (file)
@@ -2719,8 +2719,7 @@ Datum RASTER_mapAlgebraExpr(PG_FUNCTION_ARGS)
         initexpr = (char *)palloc(len + 1);
 
         strncpy(initexpr, "SELECT ", strlen("SELECT "));
-        strncpy(initexpr + strlen("SELECT "), rtpg_strtoupper(expression),
-            strlen(expression));
+        strncpy(initexpr + strlen("SELECT "), expression, strlen(expression));
         initexpr[len] = '\0';
 
         POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebraExpr: Expression is %s", initexpr);
@@ -2789,7 +2788,7 @@ Datum RASTER_mapAlgebraExpr(PG_FUNCTION_ARGS)
      * Optimization: If expression resume to 'RAST' and hasnodataval is zero,
                 * we can just return the band from the original raster
      **/
-    if (initexpr != NULL && !strcmp(initexpr, "SELECT RAST") && !hasnodataval) {
+    if (initexpr != NULL && ( !strcmp(initexpr, "SELECT [rast]") || !strcmp(initexpr, "SELECT [rast.val]") ) && !hasnodataval) {
 
         POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebraExpr: Expression resumes to RAST. "
                 "Returning raster with band %d from original raster", nband);
@@ -2822,9 +2821,9 @@ Datum RASTER_mapAlgebraExpr(PG_FUNCTION_ARGS)
 
     /**
      * Optimization: If expression resume to a constant (it does not contain
-     * RAST)
+     * [rast)
      **/
-    if (initexpr != NULL && strstr(initexpr, "RAST") == NULL) {
+    if (initexpr != NULL && strstr(initexpr, "[rast") == NULL) {
 
         SPI_connect();
 
@@ -2944,6 +2943,12 @@ Datum RASTER_mapAlgebraExpr(PG_FUNCTION_ARGS)
     POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebraExpr: Main computing loop (%d x %d)",
             width, height);
 
+    /* Convert [rast.val] to [rast] */
+    if (initexpr != NULL) {
+       newexpr = rtpg_strreplace(initexpr, "[rast.val]", "[rast]", NULL);
+       pfree(initexpr); initexpr=newexpr;
+    }
+
     for (x = 0; x < width; x++) {
         for(y = 0; y < height; y++) {
             char *tmp;
@@ -2957,13 +2962,14 @@ Datum RASTER_mapAlgebraExpr(PG_FUNCTION_ARGS)
                 if (skipcomputation == 0) {
 
                     if (initexpr != NULL) {
+
                         sprintf(strnewval, "%d", x+1);
-                        newexpr = rtpg_strreplace(initexpr, "RAST.X", strnewval, NULL);
+                        newexpr = rtpg_strreplace(initexpr, "[rast.x]", strnewval, NULL);
                         sprintf(strnewval, "%d", y+1);
-                        tmp = rtpg_strreplace(newexpr, "RAST.Y", strnewval, NULL);
+                        tmp = rtpg_strreplace(newexpr, "[rast.y]", strnewval, NULL);
                         pfree(newexpr); newexpr=tmp;
                         sprintf(strnewval, "%f", r);
-                        tmp = rtpg_strreplace(newexpr, "RAST", strnewval, NULL);
+                        tmp = rtpg_strreplace(newexpr, "[rast]", strnewval, NULL);
                         pfree(newexpr); newexpr=tmp;
 
                         POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebraExpr: (%dx%d), "
@@ -3020,6 +3026,8 @@ Datum RASTER_mapAlgebraExpr(PG_FUNCTION_ARGS)
         }
     }
 
+   if (initexpr != NULL) pfree(initexpr);
+
     /* The newrast band has been modified */
 
     POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebraExpr: raster modified, serializing it.");
@@ -3028,8 +3036,6 @@ Datum RASTER_mapAlgebraExpr(PG_FUNCTION_ARGS)
     pgraster = rt_raster_serialize(newrast);
     if (NULL == pgraster) {
         /* Free memory allocated out of the current context */
-        if (initexpr)
-            pfree(initexpr);
         rt_raster_destroy(raster);
         rt_raster_destroy(newrast);
 
@@ -3040,10 +3046,6 @@ Datum RASTER_mapAlgebraExpr(PG_FUNCTION_ARGS)
 
     POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebraExpr: raster serialized");
 
-    /* Free memory */
-    if (initexpr)
-        pfree(initexpr);
-
     rt_raster_destroy(raster);
     rt_raster_destroy(newrast);
 
index dc28f29697cc10d0e8b53472f1a8c8ab427added..07449624c779bc3885d9a0ccdc7c98cfb9bc7375 100644 (file)
@@ -1,30 +1,30 @@
 -- Test NULL raster
-SELECT ST_MapAlgebraExpr(NULL, 1, NULL, 'rast + 20', 2) IS NULL FROM ST_TestRaster(0, 0, -1) rast;
+SELECT ST_MapAlgebraExpr(NULL, 1, NULL, '[rast] + 20', 2) IS NULL FROM ST_TestRaster(0, 0, -1) rast;
 
 -- Test empty raster
-SELECT 'T1', 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 'T2', 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 '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;
+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 '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;
+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 '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;
+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 '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;
+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
+-- Test '[rast.x]', '[rast.y]' and '[rast.val]' 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)
+  ST_Value(ST_MapAlgebraExpr(rast, 1, NULL, 'round([rast]*[rast.x]/[rast.y]+[rast.val])'), x, y)
 FROM ST_TestRaster(0, 0, 10) rast,
      generate_series(6, 8) as x,
      generate_series(2, 4) as y
@@ -36,6 +36,6 @@ WITH op AS ( select rast AS rin,
   AS rout FROM ST_TestRaster(0, 0, 10) rast )
 SELECT 'T11.1', ST_Value(rin, 1, 1), ST_Value(rout, 1, 1) FROM op;
 WITH op AS ( select rast AS rin,
-  ST_MapAlgebraExpr(rast, 1, NULL, 'g from (select RAST,NULL as g) as foo', 2)
+  ST_MapAlgebraExpr(rast, 1, NULL, 'g from (select [rast],NULL as g) as foo', 2)
   AS rout FROM ST_TestRaster(0, 0, 10) rast )
 SELECT 'T11.2', ST_Value(rin, 1, 1), ST_Value(rout, 1, 1) FROM op;