Assignment: Calling ArcGIS from Python
1. Researching Watersheds
Create a python tool that finds the area of the watershed behind the of the North Fork Dam. You'll need a shapefile for the North Fork dam and a DEM of Oregon. These are available in the "Data" page listed to the left.
Create a Python module (file) with a function that returns the watershed boundary based on a DEM and an inintial point. The parameters to the function should be a file path to a DEM, a file path to a shapefile with one or more points for the dam(s), and a file path to the final polygon shapefile. The function will need to:
- Create fill (fills sinks in a DEM):
- Spatial Analyst -> Hydrology -> Fill
- Create flow direction raster from DEM:
- Spatial Analyst -> Hydrology -> Flow Direction
- Create flow accumulation raster from DEM
- Spatial Analyst -> Hydrology -> Flow Accumulation
- Find the the pixel with the highest accumulated flow behind a dam
- Spatial Analyst -> Hydrology -> Snap Pour Point
- For the run Spatial Analyst Watershed tool to create a raster
- Spatial Analyst -> Hydrology -> Watershed
- Convert the Raster to a Polygon
- Conversion Tools -> From Raster -> Raster to Polygon
Below is some code to get you started. Notice that in one function call you'll specify an input and output file path while in another, ArcGIS returns a raster "object" which you then save to a file. You'll need to change the file paths and you'll have to determine the format and values for the "clipping bounds".
import arcpy # connect to ArcGIS arcpy.CheckOutExtension("Spatial") arcpy.env.overwriteOutput=True # Allows us to run the script repeatedly without deleting the intermdiate files # Clip the DEM to the area of interest arcpy.Clip_management(OriginalFolder+"dem30",ClippingBounds,ProcessingFolder+"DemClip3.img","#","#","NONE") print("Clipped DEM") # Create a DEM with sinks filled FilledRaster=arcpy.sa.Fill(ProcessingFolder+"DemClip3.img") FilledRaster.save(ProcessingFolder+"FilledRaster.img")
Turn-in:
- A finished Python script with documentation and exception handling
- A Python module with a funtion to compute the watershed and full documentation
Extra credit:
Print out other statistics about the watershed, run the script for a number of dams in Oregon.