Difference between revisions of "GRASS GIS Jupyter notebooks"

From GRASS-Wiki
Jump to: navigation, search
m (remove dupe)
 
(4 intermediate revisions by 3 users not shown)
Line 2: Line 2:
  
 
You can even change the code and explore your modified script(s).
 
You can even change the code and explore your modified script(s).
 +
 +
== Requirements ==
 +
 +
You need to have a working jupyter installation:
 +
<source lang="bash">
 +
pip2 install jupyter
 +
</source>
 +
 +
== How to run GRASS GIS from Jupyter notebook ==
 +
 +
Running GRASS from Jupyter notebook requires that Jupyter notebook is installed on your system, and that you set up the [https://grass.osgeo.org/grass72/manuals/variables.html environment variables] for using GRASS.
 +
 +
For example you can create a '''script''' called <tt>grass_jupyter.sh</tt> that contains the following lines:
 +
<source lang="bash">
 +
 +
# directory where GRASS GIS lives
 +
export GISBASE=`grass74 --config path`  # or define path to binaries like /usr/local/grass-7.4.svn
 +
echo $GISBASE
 +
 +
# generate GISRC
 +
# Defines the system wide value while in a GRASS session
 +
MYGISDBASE=$HOME/grassdata # Change this path to reflect your own
 +
MYLOC=MyLoc # Change this location name to reflect your own
 +
MYMAPSET=PERMANENT
 +
 +
# Set the global grassrc file to individual file name
 +
MYGISRC="$HOME/.grassrc.$$"
 +
 +
echo "GISDBASE: $MYGISDBASE" > "$MYGISRC"
 +
echo "LOCATION_NAME: $MYLOC" >> "$MYGISRC"
 +
echo "MAPSET: $MYMAPSET" >> "$MYGISRC"
 +
echo "GRASS_GUI: text" >> "$MYGISRC"
 +
 +
# path to GRASS settings file
 +
export GISRC=$MYGISRC
 +
 +
export LD_LIBRARY_PATH=$GISBASE/lib:$LD_LIBRARY_PATH
 +
export PYTHONPATH=$GISBASE/etc/python:$PYTHONPATH
 +
export PATH=$GISBASE/bin:$GISBASE/scripts:$PATH
 +
 +
# start the notebook in the notebook folder
 +
cd $HOME/notebooks # change to notebooks folder (example; update to your path)
 +
 +
nohup jupyter notebook --ip=0.0.0.0 --port=8888 --debug &
 +
</source>
 +
 +
To easily use this script, add this line to your <tt>$HOME/.bashrc</tt> file:
 +
 +
<source lang="bash">
 +
# update path to script accordingly
 +
alias grass_notebook='sh $HOME/grass_jupyter.sh'
 +
</source>
 +
 +
From terminal, in your home directory:
 +
<source lang="bash">
 +
source .bashrc
 +
</source>
 +
 +
Now you can start easily your notebook typing in the terminal:
 +
<source lang="bash">
 +
grass_notebook
 +
</source>
  
 
=== List of selected GRASS GIS Jupyter notebooks ===
 
=== List of selected GRASS GIS Jupyter notebooks ===
Line 7: Line 69:
 
(please expand!)
 
(please expand!)
  
 +
* https://github.com/wenzeslaus/try-grass-in-jupyter
 +
* https://github.com/wenzeslaus/try-grass-in-jupyter-with-bash
 
* OSGeoLive-Notebooks: [https://github.com/OSGeo/OSGeoLive-Notebooks/tree/master/GSoC-2015/Introduction%20to%20GRASS%20GIS Introduction to GRASS GIS]
 
* OSGeoLive-Notebooks: [https://github.com/OSGeo/OSGeoLive-Notebooks/tree/master/GSoC-2015/Introduction%20to%20GRASS%20GIS Introduction to GRASS GIS]
 
* [https://github.com/zarch/workshop-pygrass Workshop on pygrass using IPython notebook]
 
* [https://github.com/zarch/workshop-pygrass Workshop on pygrass using IPython notebook]
Line 13: Line 77:
 
* [https://github.com/lucadelu/docker-stacks/tree/geospatial/geospatial-notebook docker-stacks/geospatial-notebook]
 
* [https://github.com/lucadelu/docker-stacks/tree/geospatial/geospatial-notebook docker-stacks/geospatial-notebook]
 
* [https://github.com/wenzeslaus/geospatial-modeling-course-jupyter/tree/master/notebooks 14 Jupyter Notebooks for learning geospatial analysis and modeling with GRASS GIS]
 
* [https://github.com/wenzeslaus/geospatial-modeling-course-jupyter/tree/master/notebooks 14 Jupyter Notebooks for learning geospatial analysis and modeling with GRASS GIS]
 +
* [https://gist.github.com/joergsteinkamp/43fb13a82aeab9b5c0dcc4433f97f1a6 Simple Opensource GIS examples with Grass and R]
  
 
==== See also ====
 
==== See also ====

Latest revision as of 04:42, 3 September 2019

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.

You can even change the code and explore your modified script(s).

Requirements

You need to have a working jupyter installation:

pip2 install jupyter

How to run GRASS GIS from Jupyter notebook

Running GRASS from Jupyter notebook requires that Jupyter notebook is installed on your system, and that you set up the environment variables for using GRASS.

For example you can create a script called grass_jupyter.sh that contains the following lines:

# directory where GRASS GIS lives
export GISBASE=`grass74 --config path`   # or define path to binaries like /usr/local/grass-7.4.svn
echo $GISBASE

# generate GISRC
# Defines the system wide value while in a GRASS session
MYGISDBASE=$HOME/grassdata # Change this path to reflect your own 
MYLOC=MyLoc # Change this location name to reflect your own 
MYMAPSET=PERMANENT

# Set the global grassrc file to individual file name
MYGISRC="$HOME/.grassrc.$$"

echo "GISDBASE: $MYGISDBASE" > "$MYGISRC"
echo "LOCATION_NAME: $MYLOC" >> "$MYGISRC"
echo "MAPSET: $MYMAPSET" >> "$MYGISRC"
echo "GRASS_GUI: text" >> "$MYGISRC"

# path to GRASS settings file
export GISRC=$MYGISRC

export LD_LIBRARY_PATH=$GISBASE/lib:$LD_LIBRARY_PATH
export PYTHONPATH=$GISBASE/etc/python:$PYTHONPATH
export PATH=$GISBASE/bin:$GISBASE/scripts:$PATH

# start the notebook in the notebook folder
cd $HOME/notebooks # change to notebooks folder (example; update to your path)

nohup jupyter notebook --ip=0.0.0.0 --port=8888 --debug &

To easily use this script, add this line to your $HOME/.bashrc file:

# update path to script accordingly
alias grass_notebook='sh $HOME/grass_jupyter.sh'

From terminal, in your home directory:

source .bashrc

Now you can start easily your notebook typing in the terminal:

grass_notebook

List of selected GRASS GIS Jupyter notebooks

(please expand!)

See also