GRASS GIS Jupyter notebooks: Difference between revisions
(+Windows tricks) |
No edit summary |
||
(5 intermediate revisions by 2 users not shown) | |||
Line 16: | Line 16: | ||
* NSF platform: https://wholetale.org/ | * NSF platform: https://wholetale.org/ | ||
** GRASS GIS Introduction, by Anna Petrasova: https://dashboard.wholetale.org/run/63d832bab4a0197e9a9ecdad?tab=files | ** GRASS GIS Introduction, by Anna Petrasova: https://dashboard.wholetale.org/run/63d832bab4a0197e9a9ecdad?tab=files | ||
'''Windows related:''' | |||
* [https://github.com/ncsu-geoforall-lab/GIS714-assignments/blob/main/GRASS_GIS_Foundations/JupyterOnWindows_standalone_Tutorial.ipynb GRASS GIS Jupyter Notebooks on Windows standalone installer tutorial] | |||
* [https://github.com/ncsu-geoforall-lab/GIS714-assignments/blob/main/GRASS_GIS_Foundations/JupyterOnWindows_OSGeo4W_Tutorial.ipynb GRASS GIS Jupyter Notebooks on Windows with OSGeo4W] | |||
'''Mac related:''' | |||
* [https://github.com/ncsu-geoforall-lab/GIS714-assignments/blob/main/GRASS_GIS_Foundations/JupyterOnMac_Tutorial.ipynb GRASS GIS Jupyter Notebooks on Mac] | |||
'''Workbook Collections:''' | '''Workbook Collections:''' | ||
Line 105: | Line 112: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Note that the executable on Windows is '''not called''' <tt>'''grass'''</tt> like on Linux but <tt>'''grass8'''</tt> or <tt>'''grass83'''</tt> (to avoid a strange Python import error). | |||
That should do the trick. If not, try simply providing a full path yourself including the extension (e.g., <tt>C:\...\ | That should do the trick. If not, try simply providing a full path yourself including the extension (e.g., <tt>C:\...\grass84.bat</tt>), e.g. | ||
<syntaxhighlight lang="python"> | |||
# Ask GRASS GIS where its Python packages are. | |||
sys.path.append( | |||
subprocess.check_output(["C:\\Program Files\\GRASS GIS 8.4\\grass84.bat", "--config", "python_path"], text=True).strip() | |||
) | |||
</syntaxhighlight> | |||
This can be made simpler by adding the path to where your <tt>'''grass84.bat'''</tt> is to your "Path" environment variable on Windows, which you can edit at the account level, or system level via the "Edit environment variables for your account" and "Edit the system environment variables" respectively. If you do the above, you can simplify the code snippet to: | |||
<syntaxhighlight lang="python"> | |||
# Ask GRASS GIS where its Python packages are. | |||
sys.path.append( | |||
subprocess.check_output(["grass84", "--config", "python_path"], text=True).strip() | |||
) | |||
</syntaxhighlight> | |||
You can also call <tt>'''grass84'''</tt> from a Powershell instance or command prompt when you add the directory where you grass84.bat is installed and you can access grass command from the shell like you can on Linux or Mac. | |||
== Infrastructure == | == Infrastructure == |
Latest revision as of 05:17, 18 December 2024
A Jupyter Notebook is a web application that allows you to create and share documents that contain scripts and code, equations, visualizations and explanatory text, combined.
Get started
Read the intro in the official documentation for grass.jupyter package.
List of selected GRASS GIS Jupyter notebooks
(add yours to the list!)
Intro notebooks:
- Official notebooks in OSGeo/grass repo (jupyter_example.ipynb on Binder)
- Unleash the power of GRASS GIS with Jupyter (FOSS4G 2022 workshop)
- GRASS GIS: From Beginner to Power User (FOSS4G 2021 workshop)
- wenzeslaus/try-grass-in-jupyter-with-bash (experimental)
- NSF platform: https://wholetale.org/
- GRASS GIS Introduction, by Anna Petrasova: https://dashboard.wholetale.org/run/63d832bab4a0197e9a9ecdad?tab=files
Windows related:
- GRASS GIS Jupyter Notebooks on Windows standalone installer tutorial
- GRASS GIS Jupyter Notebooks on Windows with OSGeo4W
Mac related:
Workbook Collections:
- 14 Jupyter Notebooks for learning geospatial analysis and modeling with GRASS GIS (Python and command line versions)
Species Distribution Modeling:
- NSF platform: https://wholetale.org/
- Using satellite data for Species Distribution Modeling with GRASS GIS and R, by Veronica Andreo https://dashboard.wholetale.org/run/64388157b3b74e434bd8f1ac?tab=files
Image processing/OBIA:
- GRASS GIS for remote sensing data processing and analysis
- GRASS GIS 8 and processing of multitemporal EO data
- An open-source semi-automated processing chain for urban OBIA classification
Python and PyGRASS:
- Workshop on PyGRASS using IPython notebook
- Developing custom GRASS tools (FOSS4G 2022 workshop)
- How to write a Python GRASS GIS 7 addon (FOSS4G Europe 2015 workshop)
- Jupyter Notebook Geospatial Python Stack
R and GRASS GIS:
Wildfire Modeling:
Running GRASS GIS in Jupyter Lab with docker
- JupyterGrass: GRASS GIS in Jupyter Lab
- docker-compose file and instructions: https://gist.github.com/manuelep/e56fe90399c44e02e7cb97d5ef95edbd
Running GRASS GIS in a Jupyter notebook locally
Both Jupyter and GRASS GIS have so called environments or sessions which need to be combined. Existing online environments with example notebooks are usually prepared to run right away, but locally, one must prepare the necessary software setup.
Software requirements
You need to have a working GRASS GIS installation.
You need to have a working Jupyter installation. If you are using pip, you can install Jupyter using:
pip install jupyter
GRASS GIS and the Python you used to install Jupyter need to know about each other.
On Linux, this will be usually true and no special steps are needed. GRASS GIS just uses the system Python and you presumably use pip which uses system Python too.
On Windows, install Jupyter using the Python which is available in GRASS session. There is no system Python on Windows, so, e.g., installing Jupyter in conda and standalone GRASS GIS results in two different and disconnected Python installations. See this NCSU GIS714 Jupyter On Windows Tutorial for installing Jupyter using pip which is linked with Python linked to GRASS GIS.
Start Jupyter, then GRASS GIS
jupyter lab
In a notebook, start the session using the grass.jupyter package.
On Windows, due to a lack of system Python shared among all applications, it is easiest to start GRASS GIS (to get a command line with Python) and start Jupyter from there, see the NCSU GIS714 Jupyter On Windows Tutorial. (The session in command line will be replaced by a new session in the notebook.)
Start GRASS GIS, then GRASS GIS
If the above does not work for you or you want your notebooks to not deal with GRASS session at all, start GRASS shell and then start Jupyter from there, for example:
$ grass ...
GRASS > jupyter lab
This can be done in one command, avoiding need for an additional step in the GRASS shell:
$ grass ... --exec jupyter lab
For example:
$ grass ~/data/world_wgs84/coastal --exec jupyter lab
Windows tricks
If you get the error
FileNotFoundError: [WinError 2] The system cannot find the file specified
Note that the executable on Windows is not called grass like on Linux but grass8 or grass83 (to avoid a strange Python import error). That should do the trick. If not, try simply providing a full path yourself including the extension (e.g., C:\...\grass84.bat), e.g.
# Ask GRASS GIS where its Python packages are.
sys.path.append(
subprocess.check_output(["C:\\Program Files\\GRASS GIS 8.4\\grass84.bat", "--config", "python_path"], text=True).strip()
)
This can be made simpler by adding the path to where your grass84.bat is to your "Path" environment variable on Windows, which you can edit at the account level, or system level via the "Edit environment variables for your account" and "Edit the system environment variables" respectively. If you do the above, you can simplify the code snippet to:
# Ask GRASS GIS where its Python packages are.
sys.path.append(
subprocess.check_output(["grass84", "--config", "python_path"], text=True).strip()
)
You can also call grass84 from a Powershell instance or command prompt when you add the directory where you grass84.bat is installed and you can access grass command from the shell like you can on Linux or Mac.
Infrastructure
- NSF platform: https://wholetale.org/
- Using satellite data for SDM with GRASS GIS and R, by Veronica Andreo https://dashboard.wholetale.org/run/64388157b3b74e434bd8f1ac?tab=files
- GRASS GIS Introduction, by Anna Petrasova: https://dashboard.wholetale.org/run/63d832bab4a0197e9a9ecdad?tab=files
- Colab (Google)
- Binder
Tools
- GRASS GIS focused converter from HTML with pre code tags to Jupyter Notebook
- RISE: Jupyter/IPython Slideshow Extension, https://rise.readthedocs.io/en/stable/