Index: raster/scripts/plpgsql/st_mapalgebra.sql
===================================================================
--- raster/scripts/plpgsql/st_mapalgebra.sql	(revision 6918)
+++ raster/scripts/plpgsql/st_mapalgebra.sql	(working copy)
@@ -17,13 +17,13 @@
 -- rast raster -  Raster on which the expression is evaluated. (Referred 
 --                by "rast1" in the expression. 
 -- band integer - Band number of the raster to be evaluated. Default to 1.
--- expression text - SQL expression to apply to with value pixels. Ex.: "rast + 2"
--- nodatavalueexpr text - SQL expression to apply to nodata value pixels. Ex.: "2"
 -- pixeltype text - Pixeltype assigned to the resulting raster. Expression
 --                  results are truncated to this type. Default to the 
 --                  pixeltype of the first raster.
+-- expression text - SQL expression to apply to with value pixels. Ex.: "rast + 2"
+-- nodatavalueexpr text - SQL expression to apply to nodata value pixels. Ex.: "2"
 --------------------------------------------------------------------
-CREATE OR REPLACE FUNCTION ST_MapAlgebra(rast raster, band integer, expression text, nodatavalueexpr text, pixeltype text) 
+CREATE OR REPLACE FUNCTION ST_MapAlgebra(rast raster, band integer, pixeltype text, expression text, nodatavalueexpr text) 
     RETURNS raster AS 
     $$
     DECLARE
@@ -41,7 +41,6 @@
         initndvexpr text;
         newpixeltype text;
         skipcomputation int := 0;
-        newhasnodatavalue boolean := FALSE;
     BEGIN
         -- Check if raster is NULL
         IF rast IS NULL THEN
@@ -68,11 +67,11 @@
         END IF;
         
         -- Check for notada value
-        IF ST_BandHasNodataValue(rast, band) THEN
+        IF NOT ST_BandNodataValue(rast, band) IS NULL THEN
             newnodatavalue := ST_BandNodataValue(rast, band);
         ELSE
             RAISE NOTICE 'ST_MapAlgebra: Source raster do not have a nodata value, nodata value for new raster set to the min value possible';
-            newnodatavalue := ST_MinPossibleVal(newrast);
+            newnodatavalue := 0;
         END IF;
         -- We set the initial value of the future band to nodata value. 
         -- If nodatavalue is null then the raster will be initialise to ST_MinPossibleVal 
@@ -154,16 +153,13 @@
                         END IF;
                     END IF;
                     newrast = ST_SetValue(newrast, band, x, y, newval);
-                ELSE
-                    newhasnodatavalue := TRUE;
                 END IF;
             END LOOP;
         END LOOP;
         
-        RETURN ST_SetBandHasNodataValue(newrast, 1, newhasnodatavalue);
+        RETURN ST_SetBandNodataValue(newrast, 1, newnodatavalue);
     END;
-    $$
-    LANGUAGE 'plpgsql';
+    $$ LANGUAGE 'plpgsql';
 
 
 --------------------------------------------------------------------
@@ -171,58 +167,53 @@
 --------------------------------------------------------------------
 CREATE OR REPLACE FUNCTION ST_MapAlgebra(rast raster, band integer, expression text) 
     RETURNS raster
-    AS 'SELECT ST_MapAlgebra($1, $2, $3, NULL, NULL)'
+    AS 'SELECT ST_MapAlgebra($1, $2, NULL, $3, NULL)'
     LANGUAGE 'SQL' IMMUTABLE;
 
-CREATE OR REPLACE FUNCTION ST_MapAlgebra(rast raster, expression text, pixeltype text) 
+CREATE OR REPLACE FUNCTION ST_MapAlgebra(rast raster, pixeltype text, expression text) 
     RETURNS raster
-    AS 'SELECT ST_MapAlgebra($1, 1, $2, NULL, $3)'
+    AS 'SELECT ST_MapAlgebra($1, 1, $2, $3, NULL)'
     LANGUAGE 'SQL' IMMUTABLE;
 
 CREATE OR REPLACE FUNCTION ST_MapAlgebra(rast raster, expression text) 
     RETURNS raster
-    AS 'SELECT ST_MapAlgebra($1, 1, $2, NULL, NULL)'
+    AS 'SELECT ST_MapAlgebra($1, 1, NULL, $2, NULL)'
     LANGUAGE 'SQL' IMMUTABLE;
 
-CREATE OR REPLACE FUNCTION ST_MapAlgebra(rast raster, band integer, expression text, nodatavalexpr text) 
+CREATE OR REPLACE FUNCTION ST_MapAlgebra(rast raster, band integer, expression text, nodatavalueexpr text) 
     RETURNS raster
-    AS 'SELECT ST_MapAlgebra($1, $2, $3, $4, NULL)'
+    AS 'SELECT ST_MapAlgebra($1, $2, NULL, $3, $4)'
     LANGUAGE 'SQL' IMMUTABLE;
 
-CREATE OR REPLACE FUNCTION ST_MapAlgebra(rast raster, expression text, nodatavalexpr text, pixeltype text) 
+CREATE OR REPLACE FUNCTION ST_MapAlgebra(rast raster, pixeltype text, expression text, nodatavalueexpr text)
     RETURNS raster
     AS 'SELECT ST_MapAlgebra($1, 1, $2, $3, $4)'
     LANGUAGE 'SQL' IMMUTABLE;
 
-CREATE OR REPLACE FUNCTION ST_MapAlgebra(rast raster, expression text, nodatavalexpr text) 
-    RETURNS raster
-    AS 'SELECT ST_MapAlgebra($1, 1, $2, $3, NULL)'
-    LANGUAGE 'SQL' IMMUTABLE;
-
 -- Test
 -- Test NULL Raster
-SELECT ST_MapAlgebra(NULL, 1, 'rast + 20', '2') IS NULL FROM ST_TestRaster(0, 0, -1) rast;
+--SELECT ST_MapAlgebra(NULL, 1, 'rast + 20', '2') IS NULL FROM ST_TestRaster(0, 0, -1) rast;
 
 -- Test empty Raster
-SELECT ST_IsEmpty(ST_MapAlgebra(ST_MakeEmptyRaster(0, 10, 0, 0, 1, 1, 1, 1, -1), 1, 'rast + 20', '2'))
+--SELECT ST_IsEmpty(ST_MapAlgebra(ST_MakeEmptyRaster(0, 10, 0, 0, 1, 1, 1, 1, -1), 1, 'rast + 20', '2'));
 
 -- Test has no band raster
-SELECT ST_HasNoBand(ST_MapAlgebra(ST_MakeEmptyRaster(10, 10, 0, 0, 1, 1, 1, 1, -1), 1, 'rast + 20', '2'))
+--SELECT ST_HasNoBand(ST_MapAlgebra(ST_MakeEmptyRaster(10, 10, 0, 0, 1, 1, 1, 1, -1), 1, 'rast + 20', '2'));
 
 -- Test has no nodata value
-SELECT ST_Value(rast, 1, 1), ST_Value(ST_MapAlgebra(ST_SetBandHasNoDataValue(rast, FALSE), 1, 'rast + 20', '2'), 1, 1) FROM ST_TestRaster(0, 0, -1) rast;
+--SELECT ST_Value(rast, 1, 1), ST_Value(ST_MapAlgebra(ST_SetBandNoDataValue(rast, NULL::FLOAT), 1, 'rast + 20', '2'), 1, 1) FROM ST_TestRaster(0, 0, -1) rast;
 
 -- Test has nodata value
-SELECT ST_Value(rast, 1, 1), ST_Value(ST_MapAlgebra(rast, 1, 'rast + 20', 'rast + 2'), 1, 1) FROM ST_TestRaster(0, 0, -1) rast;
+--SELECT ST_Value(rast, 1, 1), ST_Value(ST_MapAlgebra(rast, 1, 'rast + 20', 'rast + 2'), 1, 1) FROM ST_TestRaster(0, 0, -1) rast;
 
 -- Test 'rast' expression (not yet since ST_AddBand(newrast, rast, band, 1) is not implemented)
 -- SELECT ST_Value(rast, 1, 1), ST_Value(ST_MapAlgebra(rast, 1, 'rast', 'rast'), 1, 1) FROM ST_TestRaster(0, 0, -1) rast;
 -- SELECT ST_Value(rast, 1, 1), ST_Value(ST_MapAlgebra(ST_SetBandHasNoDataValue(rast, FALSE), 1, 'rast', NULL), 1, 1) FROM ST_TestRaster(0, 0, -1) rast;
 
 -- Test pixeltype
-SELECT ST_Value(rast, 1, 1), ST_Value(ST_MapAlgebra(rast, 1, 'rast + 20', 'rast + 2', '4BUI'), 1, 1) FROM ST_TestRaster(0, 0, 100) rast;
-SELECT ST_Value(rast, 1, 1), ST_Value(ST_MapAlgebra(rast, 1, 'rast + 20', 'rast + 2', '4BUId'), 1, 1) FROM ST_TestRaster(0, 0, 100) rast;
-SELECT ST_Value(rast, 1, 1), ST_Value(ST_MapAlgebra(rast, 1, 'rast + 20', 'rast + 2', '2BUI'), 1, 1) FROM ST_TestRaster(0, 0, 101) rast;
+--SELECT ST_Value(rast, 1, 1), ST_Value(ST_MapAlgebra(rast, 1, '4BUI', 'rast + 20', 'rast + 2'), 1, 1) FROM ST_TestRaster(0, 0, 100) rast;
+--SELECT ST_Value(rast, 1, 1), ST_Value(ST_MapAlgebra(rast, 1, '4BUId', 'rast + 20', 'rast + 2'), 1, 1) FROM ST_TestRaster(0, 0, 100) rast;
+--SELECT ST_Value(rast, 1, 1), ST_Value(ST_MapAlgebra(rast, 1, '2BUI', 'rast + 20', 'rast + 2'), 1, 1) FROM ST_TestRaster(0, 0, 101) rast;
 
 
 --------------------------------------------------------------------
@@ -315,7 +306,7 @@
 -- ST_IsEmpty
 -- Return TRUE if the raster is empty. i.e. is NULL, width = 0 or height = 0
 --------------------------------------------------------------------
-CREATE OR REPLACE FUNCTION ST_IsEmpty(raster)
+CREATE OR REPLACE FUNCTION ST_IsEmpty(rast raster)
     RETURNS boolean
     AS 'SELECT $1 IS NULL OR ST_Width($1) = 0 OR ST_Height($1) = 0'
     LANGUAGE 'SQL' IMMUTABLE;
@@ -324,7 +315,7 @@
 -- ST_HasNoBand
 -- Return TRUE if the number of band is 0
 --------------------------------------------------------------------
-CREATE OR REPLACE FUNCTION ST_HasNoBand(raster)
+CREATE OR REPLACE FUNCTION ST_HasNoBand(rast raster)
     RETURNS boolean
     AS 'SELECT $1 IS NULL OR ST_NumBands($1) = 0'
     LANGUAGE 'SQL' IMMUTABLE;
@@ -333,7 +324,7 @@
 -- ST_HasNoBand
 -- Return TRUE if the raster do not have a band of this number.
 --------------------------------------------------------------------
-CREATE OR REPLACE FUNCTION ST_HasNoBand(raster, int)
+CREATE OR REPLACE FUNCTION ST_HasNoBand(rast raster, nband int)
     RETURNS boolean
     AS 'SELECT $1 IS NULL OR ST_NumBands($1) < $2'
     LANGUAGE 'SQL' IMMUTABLE;
@@ -346,9 +337,9 @@
 -- NOT YET IMPLEMENTED
 -- Return TRUE if the raster band contains only nodata values.
 --------------------------------------------------------------------
-CREATE OR REPLACE FUNCTION ST_BandIsNoData(raster, int)
+CREATE OR REPLACE FUNCTION ST_BandIsNoData(rast raster, band int)
     RETURNS boolean
-    AS 'SELECT ST_HasNoBand($1, $2) AND ST_BandHasNodataValue($1, $2) AND false'
+    AS 'SELECT ST_HasNoBand($1, $2) AND NOT ST_BandNodataValue($1, $2) IS NULL AND false'
     LANGUAGE 'SQL' IMMUTABLE;
 
 --CREATE OR REPLACE FUNCTION toto()
