millionpoundhat / mower-Python3

mower - For controlling GRASS GIS with Python

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Mower

This repo has been resurrected from perrygeo for some data processing.


A python toolkit to tame the wild GRASS.

pip3 install git+https://github.com/millionpoundhat/mower.git@master#egg=mower

The goal is to provide a dead-simple context manager to handle GRASS Session state, allowing you to run GRASS commands via pygrass without having to deal with ...

GISDBASE, LOCATIONS, MAPSETS, OH MY

Even to GIS analysts who love code, GRASS presents a significant conceptual barrier in it's system of locations and mapsets. They serve a purpose: to organize data into logical bundles based on spatial reference and themes. But many times we don't have a mapset; we just have a raster or vector dataset and we want to use GRASS for some analysis.

The old workflow goes like this:

  • create a new blank mapset
  • import the dataset (making a copy) and create a new location from it's spatial reference
  • exit grass and reenter the new mapset
  • perform the analysis
  • export the results back to files or databases or their intended format
  • clean up by removing the datasets or the LOCATION entirely

All of this required careful management of environment variables, a directory structure to satisfy the GRASS location/mapset requirements and use of shell/system calls even when using Python.

GRASS and the pygrass module solves the system call issue by providing a python API to wrap the underlying C libs directly. But you still need to perform extensive work to make sure you've set up a location/mapset and that your system is set up to work in that environment.

Why can't it be this easy?

  • Start a GRASS session with a dataset (The mapset and location are handled transparently)
  • Do some GRASS analysis
  • That's it ... the GRASS session is automatically cleaned up

Well now it is. Treat your grass session as a python context manager and use pygrass however you want without dealing with location/mapset cruft:

from mower import GrassSession

DEM = "/tmp/dem_meters.img"

with GrassSession(DEM) as gs:
    from grass.pygrass.modules.shortcuts import raster

    # Import/Link to External GDAL data
    raster.external(input=DEM, output="dem")

    # Perform calculations
    raster.mapcalc(expression="demft=dem*3.28084")

    # Export from GRASS to GDAL
    raster.out_gdal("demft", format="GTiff", output="/tmp/demft.tif", overwrite=True)

For contrast, see the example wiki code for working with GRASS without explicitly starting it.

Bash it

If you want to work in the shell instead of python, try this built-in trick

grass -c dem.tif ~/grassdata/mylocation

Which will create a new PERMANENT mapset in the mylocation location within the grassdata gisdbase and dump you directly into that session. Try with -e if you want to create but immediately exit.

About

mower - For controlling GRASS GIS with Python

License:MIT License


Languages

Language:Python 87.1%Language:Shell 12.9%