4

I've written a custom callback function for ST_MapAlgebra and I want to pass additional arguments to it. The function is pretty useless - it just generates a raster filled with normal random values according to a user-specified mean and standard deviation.

I tried including the extra arguments in the string, like so:

...
ST_MapAlgebra(
    b1.rast, -- raster to operate on
    1,
    'generate_random_raster(double precision[][][], integer[][],  ''3'', ''1'')'::regprocedure
  ),
...

And like this:

...
ST_MapAlgebra(
    b1.rast, -- raster to operate on
    1,
    'generate_random_raster(double precision[][][], integer[][],  VARIADIC ARRAY[3,5]::text')'::regprocedure
  ),
...

It works fine if I hard code the values I want to use, so the callback itself isn't a problem.

How do I pass additional arguments to a custom ST_MapAlgebra function?

2
  • Excellent question. The ST_MapAlgebra function is very powerful, but not super well documented. There is a very entertaining and completely nuts blog post from Boston GIS that is the best treatment i have seen on this subject. Mar 30, 2017 at 11:40
  • I've been working on changing that a bit - I'm new to this and I want to help smooth out the learning curve for other newbies. I've written a couple examples if you'd like to check them out.
    – nronnei
    Mar 31, 2017 at 18:00

1 Answer 1

6

Additional arguments are passed as the 6th, and so on, arguments, after the custom function signature:

ST_MapAlgebra(b1.rast, -- raster to operate on
              1, -- band
              'generate_random_raster(double precision[], integer[], text[])'::regprocedure, -- custom function signature
              pixeltype, -- can be null
              extenttype, -- can be null
              customextent, -- can be null
              distancex, -- can be null
              distancey, -- can be null
              firstextraargument,
              secondextraargument,
              ...
  )

Some demo scripts can be found on github.

6
  • 2
    I think the OP is asking about the variadic args Mar 30, 2017 at 17:10
  • I was indeed referring to the variadic arguments.
    – nronnei
    Mar 30, 2017 at 18:17
  • 1
    firstextraargument and secondextraargument are variadic arguments. Mar 30, 2017 at 21:49
  • 1
    If you could provide an example of actually using them, it would be really useful. It is an area where the docs are a bit sparse. Mar 31, 2017 at 7:36
  • I agree this needs more doc or a good article. You can find an (a bit complex) example in the ST_ExtractToRaster() function of the PostGIS Addons: github.com/pedrogit/postgisaddons/blob/master/… Mar 31, 2017 at 14:41

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.