Can't get ST_MapAlgebra to return fractional values

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite












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. Any help would be appreciated. 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)









share|improve this question



























    up vote
    3
    down vote

    favorite












    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. Any help would be appreciated. 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)









    share|improve this question

























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      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. Any help would be appreciated. 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)









      share|improve this question















      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. Any help would be appreciated. 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)






      postgis raster postgresql






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 8 hours ago









      ahmadhanb

      19.4k21746




      19.4k21746










      asked 8 hours ago









      geo derek

      180115




      180115




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          4
          down vote













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






          share|improve this answer




















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "79"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f299593%2fcant-get-st-mapalgebra-to-return-fractional-values%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            4
            down vote













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






            share|improve this answer
























              up vote
              4
              down vote













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






              share|improve this answer






















                up vote
                4
                down vote










                up vote
                4
                down vote









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






                share|improve this answer












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







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 1 hour ago









                ThingumaBob

                4,8431222




                4,8431222



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f299593%2fcant-get-st-mapalgebra-to-return-fractional-values%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    Long meetings (6-7 hours a day): Being “babysat” by supervisor

                    Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                    Confectionery