3

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)
5

You specify the return pixeltype to be '16BSI' (16bit signed integer); given this, ST_MapAlgebra won't return a float, not even with a gun to it's head...

Try with '32BF' (32bit float).

  • Thanks so much. That was exactly what the issue was. – geo derek Oct 20 '18 at 21:43
  • @geoderek you're welcome! if my or any anwer to your questions helped solving your problem, would you care to accept those (click the tick next to an answer)? – ThingumaBob Oct 21 '18 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.