extending rio-calc


Gregory, Matthew
 

Hi all,

I've found rio-calc to be really useful for raster algebra expressions. I've come up with a few additional functions that are useful in my workflow that seem to work well within rio-calc.

I'm certain, however, that I don't know how to introduce these functions into rio-calc without directly registering these functions with snuggs within calc.py (e.g. https://github.com/rasterio/rasterio/blob/master/rasterio/rio/calc.py#L159-L164).

Is there a plug-in way of extending rio-calc to bring in additional user-defined functions?

thanks, matt


Sean Gillies
 

Hi Matt,

No, we don't have a plugin system for rio-calc. If this is for your personal workflows, maybe a sitecustomize.py or usercustomize.py file that imported and patched snuggs would be worth a try?

I saved the following to sitecustomize.py in the site-packages directory of a Python environment I named "play".

import snuggs
snuggs.func_map["test"] = lambda *args: print(*args)

This isn't going to really work because it doesn't return any values that rio-calc can use, but you can see that the module does get imported and snuggs does get patched:

(play) $ rio calc "(test 'hi hi')" RGB.byte.tif -o /tmp/calc.tif
hi hi
Traceback (most recent call last):
   ...

This is the closest thing Python has to built-in extensibility. Limitations are discussed at https://www.python.org/dev/peps/pep-0648/#limitations-of-sitecustomize-py.


On Fri, Oct 29, 2021 at 8:45 AM Gregory, Matthew <matt.gregory@...> wrote:
Hi all,

I've found rio-calc to be really useful for raster algebra expressions.  I've come up with a few additional functions that are useful in my workflow that seem to work well within rio-calc.

I'm certain, however, that I don't know how to introduce these functions into rio-calc without directly registering these functions with snuggs within calc.py (e.g. https://github.com/rasterio/rasterio/blob/master/rasterio/rio/calc.py#L159-L164). 

Is there a plug-in way of extending rio-calc to bring in additional user-defined functions?

thanks, matt







--
Sean Gillies


Gregory, Matthew
 

Thanks Sean,

 

This is really helpful.  It works for the handful of functions in my workflow, so this will get me through.  It’s always humbling to find out a corner of Python that I didn’t know existed - thanks for pointing me to sitecustomize.py.

 

Not that I would assume that you’d want to manage this, but is there a pathway (or desire) for user-contributed functions to make their way into snuggs/rio-calc or would you prefer the user customization route?

 

thanks, matt

 

 

From: main@rasterio.groups.io <main@rasterio.groups.io> On Behalf Of Sean Gillies via groups.io
Sent: Saturday, October 30, 2021 11:40 AM
To: main@rasterio.groups.io
Subject: Re: [rasterio] extending rio-calc

 

[This email originated from outside of OSU. Use caution with links and attachments.]

Hi Matt,

 

No, we don't have a plugin system for rio-calc. If this is for your personal workflows, maybe a sitecustomize.py or usercustomize.py file that imported and patched snuggs would be worth a try?

 

I saved the following to sitecustomize.py in the site-packages directory of a Python environment I named "play".

 

import snuggs
snuggs.func_map["test"] = lambda *args: print(*args)

 

This isn't going to really work because it doesn't return any values that rio-calc can use, but you can see that the module does get imported and snuggs does get patched:

 

(play) $ rio calc "(test 'hi hi')" RGB.byte.tif -o /tmp/calc.tif
hi hi
Traceback (most recent call last):

   ...

 

This is the closest thing Python has to built-in extensibility. Limitations are discussed at https://www.python.org/dev/peps/pep-0648/#limitations-of-sitecustomize-py.

 

 

On Fri, Oct 29, 2021 at 8:45 AM Gregory, Matthew <matt.gregory@...> wrote:

Hi all,

I've found rio-calc to be really useful for raster algebra expressions.  I've come up with a few additional functions that are useful in my workflow that seem to work well within rio-calc.

I'm certain, however, that I don't know how to introduce these functions into rio-calc without directly registering these functions with snuggs within calc.py (e.g. https://github.com/rasterio/rasterio/blob/master/rasterio/rio/calc.py#L159-L164). 

Is there a plug-in way of extending rio-calc to bring in additional user-defined functions?

thanks, matt






--

Sean Gillies