Improved Integration of GRASS and Jupyter

Start GRASS GIS

GRASS Renderer

The GrassRenderer class creates and displays GRASS maps as PNG images. There are two ways to add elements to the display. First, the name of the GRASS display module can be called as an attribute by replacing the "." with "_" in the module name. For example:

m = GrassRenderer()
m.d_rast(map="elevation")

Alternatively, GRASS display modules can be called with the run() method:

m = GrassRenderer()
m.run("d.rast", map="elevation")

To display the image, call show().

We can also have multiple instances of GrassRenderer. Here, we create another map then go back and modify the first map.

By default the display extent (and resolution if applicable) is derived from the first raster or vector layer:

To respect computational region, set use_region=True:

You can also use a saved region:

GRASS 3D Renderer

The Grass3dRenderer class creates 3D visualizations as PNG images. The m.nviz.image module is used in the background and the function render() accepts parameters of this module. The Grass3dRenderer objects have overlay attribute which can be used in the same way as GrassRenderer and 2D images on top of the 3D visualization. To display the image, call show().

First, let's create the object:

Now, render a 3D visualization of an elevation raster as a surface colored using, again, the elevation raster:

To add a raster legend on the image as an overlay using the 2D rendering capabilities accessible with overlay.d_legend:

Finally, we show

Now, let's color the elevation surface using a landuse raster (note that the call to render removes the result of the previous render as well as the current overlays):

Switching Mapsets and Session Management

The init function returns a reference to a session object which can be used to manipulate the current session. The session is global, i.e., the global state of the environment is changed. The session object is a handle for accessing this global session. When the kernel for the notebooks shuts down or is restarted, the session ends automatically. The session can be explicitly ended using session.finish(), but that's usually not needed in notebooks.

Additionally, the session object can be used to change the current mapset. Here, we will switch to mapset called PERMANENT:

Now we could add more data to the PERMANENT mapset or modify the existing data there. We don't need to do anything there, so we switch back to the mapset we were in before: