[GRASS-dev] Python Scripting
Dan D'Alimonte
dan at dalimonte.ca
Thu Jul 17 11:23:58 EDT 2008
I'm curious if any though has been given to developing an actual GRASS
library for Python that goes beyond calling executables with system calls?
I'm thinking about a model that encapsulates the GRASS environment and
allows for both low-level processing like the C library, and high-level
use of existing modules like shell scripts.
I'll admit I have not given this a lot of though, but a hypothetical,
and quickly thought-out, example could be something like:
from grass import module, layer, cell, parser
def r_add(inLayers, outLayer):
for outCell in outLayer.cells:
sum = cell(type=outLayer.cellType)
for l in inLayers:
c = l.getCellAtSameLocation(outCell)
if c.value==cell.null:
sum.value = cell.null
break
sum.value += c.value
outCell.value = c.value
outLayer.commit()
if __name__ == "__main__":
# Set up module information here
# Set up and run parser
# Open input layers
# Create new layer for output
# call r_add()
# close layers
I don't know if this would even be feasible, but I think it would make a
nice addition to GRASS's Python support. If done right it would even
allow other python-based GRASS modules to be called-on without having to
make a system-call (eg. from raster_add import r_add).
As to existing modules, what about a helper function to access then?
module.executeModule( name="r.stats", options={ "input":
"elevation.dem,slope,aspect", "fs": ",", "output": "elev.csv"},
flags=["q", "1", "n", "g"] )
It was just a thought, and would require a lot more thought and work to
design properly. I, frankly, do not have enough understanding of GRASS
internals to even know where to begin.
-- Dan.
More information about the grass-dev
mailing list