{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Viewshed Analysis" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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.\n", "\n", "This interactive notebook is available online thanks to the [https://mybinder.org](Binder) service. To run the select part (called a *cell*), hit `Shift + Enter`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Starting GRASS in Jupyter Notebooks" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Import Python standard library and IPython packages we need.\n", "import os\n", "import subprocess\n", "import sys\n", "\n", "# Ask GRASS GIS where its Python packages are.\n", "sys.path.append(\n", " subprocess.check_output([\"grass\", \"--config\", \"python_path\"], text=True).strip()\n", ")\n", "\n", "# Import the GRASS GIS packages we need.\n", "import grass.script as gs\n", "import grass.jupyter as gj\n", "\n", "# Start GRASS Session\n", "session = gj.init(\"../../data/grassdata\", \"nc_basic_spm_grass7\", \"user1\")\n", "\n", "# Set computational region to elevation raster\n", "gs.run_command(\"g.region\", raster=\"elevation@PERMANENT\", flags=\"pg\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Viewshed analysis\n", "Compute viewshed from a new 32 story tower located in downtown Raleigh, NC." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "gs.parse_command(\"g.region\", raster=\"elevation\", flags=\"apg\")\n", "gs.write_command(\"v.in.ascii\", input=\"-\", stdin=\"%s|%s\" % (642212, 224767), output=\"viewpoints\")\n", "gs.run_command(\"r.viewshed\", input=\"elevation\", output=\"tower_los\", coordinates=\"642212,224767\", observer_elevation=\"165\", max_distance=\"10000\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Display result with basemap:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "vs_map = gj.InteractiveMap()\n", "\n", "vs_map.add_raster(\"tower_los\", opacity=0.7)\n", "vs_map.add_vector(\"viewpoints\")\n", "vs_map.add_layer_control()\n", "\n", "vs_map.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.10" } }, "nbformat": 4, "nbformat_minor": 4 }