I have a 4-band raster dataset that I have imported into a Postgres/PostGIS table using raster2pgsql. I am Using ST_MapAlgebra() with a callback function to perform some band math on the dataset. I have set the return type as double precision. However, I am finding that anytime I return a decimal value (e.g., .25 or -.25) it only returns zero. However, if I return a whole number (e.g., 25 or -15), I can receive the values back from the call. I played around with this, but can’t figure out why this is occurring.
Here is the syntax of my callback function:
CREATE OR REPLACE FUNCTION urban_veg.ndvi_callback (
value double precision[][][],
pos integer[][],
VARIADIC userargs text[]
)
RETURNS double precision AS
$$
DECLARE
r1_val double precision;
r4_val double precision;
result double precision;
BEGIN
r1_val := value[1][1][1]::double precision;
r4_val := value[2][1][1]::double precision;
result := (r4_val-r1_val)/(r4_val+r1_val);
RETURN result;
END;
$$
LANGUAGE 'plpgsql' IMMUTABLE COST 1000;
The call to the function is:
ST_MapAlgebra(
ARRAY[
ROW(rast, 1),
ROW(rast, 4) ]::rastbandarg[],
'urban_veg.ndvi_callback(
double precision[],integer[],text[]
)'::regprocedure, '16BSI'::text)