{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "name = '2016-06-10-arcgis-intro'\n", "title = 'Introduction to ArcGIS and its Python interface'\n", "tags = 'gis, maps, basics'\n", "author = 'Melanie Froude'" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from nb_tools import connect_notebook_to_post\n", "from IPython.core.display import HTML\n", "\n", "html = connect_notebook_to_post(name, title, tags, author)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Today Melanie lead the meeting with a session on the ArcGIS software and how we can use Python to automatise the geospatial data processing. The slides are available below." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We started with a brief introduction to the types of data and analysis you can do in ArcGIS. Then Melanie demonstrated how to produce a 3D terrain model using the ArcScene toolbox." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Presentation" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# embed pdf into an automatically resized window (requires imagemagick)\n", "w_h_str = !identify -format \"%w %h\" ../pdfs/arcgis-intro.pdf[0]\n", "HTML(''.format([int(i)*0.8 for i in w_h_str[0].split()]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We all agreed that ArcGIS has a lot to offer to geoscientists. But what makes this software even more appealing is that you can work in a command-line interface using Python (ArcPy module)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So we looked at how to run processes using the Python window command-by-command and how you might integrate ArcGIS processes within a longer script. This was exemplified by Melanie's script that she used to analyse vegetation regrowth after a volcanic eruption.\n", "\n", "The script takes two vegetation photos in GeoTIFF format retrieved by Landsat as input and calculates the Normalised Difference Vegetation Index (NDVI) for each of them. We can then compare the output to see how vegetation has changed over the time period." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "
  • Standard ArcGIS uses Python 2.7 (Python 3 is available in ArcGIS Pro)\n", "
  • The commands below require ArcGIS installed, and hence are not in executable cells in this notebook.\n", "
  • " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ArcPy script example: NDVI of the two geotiff images" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Import modules\n", "\n", "```\n", "import arcpy, string, arcpy.sa\n", "from arcpy import env\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Check out extension and set overwrite outputs\n", "\n", "```\n", "arcpy.CheckOutExtension(\"spatial\")\n", "arcpy.env.overwriteOutput = True\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Stop outputs being added to the map\n", "\n", "```\n", "arcpy.env.addOutputsToMap = \"FALSE\"```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Set workspace and declare variations\n", "\n", "```\n", "env.workspace = (\"/path/to/demo/demo1\")\n", "print(arcpy.env.workspace)\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Load the data\n", "\n", "```\n", "rasterb3 = arcpy.Raster(\"p046r28_5t900922_nn3.tif\")\n", "rasterb4 = arcpy.Raster(\"p046r28_5t900922_nn4.tif\")\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Describe variables\n", "\n", "```\n", "desc = arcpy.Describe(rasterb4)\n", "print(desc.dataType)\n", "print(desc.meanCellHeight)\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Calculate the NDVI\n", "\n", "```\n", "Num = arcpy.sa.Float(rasterb4-rasterb3)\n", "Denom = arcpy.sa.Float(rasterb4 + rasterb3)\n", "NDVI1990 = arcpy.sa.Divide(Num, Denom)\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Save the result as another .tif image\n", "\n", "```\n", "NDVI1990.save(\"/path/to/demo/demo1/NDVI1990.tif\")\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Do the same calculation for the images from a later year\n", "\n", "```\n", "rasterb3a = arcpy.Raster(\"L71046028_02820050721_B30.TIF\")\n", "rasterb4a = arcpy.Raster(\"L71046028_02820050721_B40.TIF\")\n", "\n", "Num = arcpy.sa.Float(rasterb4a-rasterb3a)\n", "Denom = arcpy.sa.Float(rasterb4a + rasterb3a)\n", "NDVI2005 = arcpy.sa.Divide(Num, Denom)\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And after saving the second result, calculate the NDVI difference\n", "```\n", "NDVI2005.save(\"/path/to/demo/demo1/NDVI2005.tif\")\n", "\n", "NDVIdiff = NDVI2005 - NDVI1990\n", "NDVIdiff.save(\"/path/to/demo/demo1/NDVIdiff.tif\")\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The result is shown in the slide 5." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", " \n", "

    This post was written as an IPython (Jupyter) notebook. You can view or download it using\n", " nbviewer.

    \n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "HTML(html)" ] } ], "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.5.0" }, "widgets": { "state": {}, "version": "1.1.2" } }, "nbformat": 4, "nbformat_minor": 0 }