This notebook is a basic introduction to viewshed analysis in GRASS GIS in Jupyter Notebook. In addition to common Python packages, it demonstrates the usage of grass.script
, the Python API for GRASS GIS, and grass.jupyter
, an experimental Jupyter Notebook specific package that helps with the launch of GRASS GIS and with displaying maps.
This interactive notebook is available online thanks to the https://mybinder.org service. To run the select part (called a cell), hit Shift + Enter
.
# Import Python standard library and IPython packages we need.
import os
import subprocess
import sys
# Ask GRASS GIS where its Python packages are.
sys.path.append(
subprocess.check_output(["grass", "--config", "python_path"], text=True).strip()
)
# Import the GRASS GIS packages we need.
import grass.script as gs
import grass.jupyter as gj
# Start GRASS Session
session = gj.init("../../data/grassdata", "nc_basic_spm_grass7", "user1")
# Set computational region to elevation raster
gs.run_command("g.region", raster="elevation@PERMANENT", flags="pg")
Compute viewshed from a new 32 story tower located in downtown Raleigh, NC.
gs.parse_command("g.region", raster="elevation", flags="apg")
gs.write_command("v.in.ascii", input="-", stdin="%s|%s" % (642212, 224767), output="viewpoints")
gs.run_command("r.viewshed", input="elevation", output="tower_los", coordinates="642212,224767", observer_elevation="165", max_distance="10000")
Display result with basemap:
vs_map = gj.InteractiveMap()
vs_map.add_raster("tower_los", opacity=0.7)
vs_map.add_vector("viewpoints")
vs_map.add_layer_control()
vs_map.show()