forked from OSGeo/grass
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Jupyter: Add non-interactive display (OSGeo#1668)
* Non-interactive display functions with rendering based on d-commands. * Rendering encapsulated in a class. Each object holds unique settings. * Examples in notebook are now showing this new API. * Deleted display_settings function. * Rendering class init accepts env parameter as all well-behaved functions in grass.script. * grass.script is used as a backend for running commands. * Rendering run method provides general way of rendering with any d-command (which is not monitor-focused). * Errors reported as exceptions, e.g., ValueError. * Added pathlib Path object supported for filenames.
- Loading branch information
Showing
5 changed files
with
139 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
from .setup import * | ||
from .display import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# MODULE: grass.jupyter.display | ||
# | ||
# AUTHOR(S): Caitlin Haedrich <caitlin DOT haedrich AT gmail> | ||
# | ||
# PURPOSE: This module contains functions for non-interactive display | ||
# in Jupyter Notebooks | ||
# | ||
# COPYRIGHT: (C) 2021 Caitlin Haedrich, and by the GRASS Development Team | ||
# | ||
# This program is free software under the GNU General Public | ||
# License (>=v2). Read the file COPYING that comes with GRASS | ||
# for details. | ||
|
||
import os | ||
from pathlib import Path | ||
from IPython.display import Image | ||
import grass.script as gs | ||
|
||
|
||
class GrassRenderer: | ||
"""The grassRenderer class creates and displays GRASS maps in | ||
Jupyter Notebooks.""" | ||
|
||
def __init__( | ||
self, env=None, width=600, height=400, filename="map.png", text_size=12 | ||
): | ||
"""Initiates an instance of the GrassRenderer class.""" | ||
|
||
if env: | ||
self._env = env.copy() | ||
else: | ||
self._env = os.environ.copy() | ||
|
||
self._env["GRASS_RENDER_WIDTH"] = str(width) | ||
self._env["GRASS_RENDER_HEIGHT"] = str(height) | ||
self._env["GRASS_TEXT_SIZE"] = str(text_size) | ||
self._env["GRASS_RENDER_IMMEDIATE"] = "cairo" | ||
self._env["GRASS_RENDER_FILE"] = str(filename) | ||
self._env["GRASS_RENDER_FILE_READ"] = "TRUE" | ||
|
||
self._legend_file = Path(filename).with_suffix(".grass_vector_legend") | ||
self._env["GRASS_LEGEND_FILE"] = str(self._legend_file) | ||
|
||
self._filename = filename | ||
|
||
self.run("d.erase") | ||
|
||
def run(self, module, **kwargs): | ||
"""Run modules from "d." GRASS library""" | ||
# Check module is from display library then run | ||
if module[0] == "d": | ||
gs.run_command(module, env=self._env, **kwargs) | ||
else: | ||
raise ValueError("Module must begin with letter 'd'.") | ||
|
||
def show(self): | ||
"""Displays a PNG image of the map (non-interactive)""" | ||
return Image(self._filename) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters