Goal
The docs say that we can pass additional arguments to ST_MapAlgebra callbacks, but how do we actually do that?
In this post, I’ll show you how to pass additional arguments (the VARIADIC
arguments referred to by the text array called userargs
in the documentation) to ST_MapAlgebra
callbacks in PostGIS.
Purpose
I had some problems with this while I was writing my post on simulating multi-band rasters, and the docs leave something to be desired.
I ended up asking about it on GIS Stack Exchange, and this post is my attempt to boost that answer.
The Answer
Thanks to Pierre Racine, one of the creators of PostGIS, we have the answer! As he demonstrates in this example:
|
|
Here’s how it looks in my use case:
|
|
Now, you might be thinking, “That’s exactly how the docs (jump to ‘35.4.5. SQL Functions with Variable Numbers of Arguments’) say you pass VARIADIC
arguments, Nick!” And I read those too!
I don’t know why it didn’t work the first time I tried it, but as I explained on SE, I have a very strong suspicion it had something to do with my inexperience with PostGIS in general and how it works with double/single quotes in particular. I hope this helps you avoid the same problem.
If you’re interested in learning more about how you can use ST_MapAlgebra
, check out my Raster Math Series.
If you’d like to see more about writing ST_MapAlgebra
callbacks that accept userags, you can dig through these examples suggested by Pierre Racine, or this totally crazy but really cool one from Regina Obe on setting up the famous Game of Life in PostGIS.