Friday, July 15, 2016

Exercise 3: Geoprocessing with Python

Goals and Objectives

The purpose of this exercise was to provide an introduction to geoprocessing by way of recreating the Exercise 1 results using PyScripter.  This involved exporting a geoprocessing model as a script, adding several smart variables, and modifying the exported script. 


Methods

This exercise carried out the exact same processes as those used in Exercise 1, only this time using Python instead of Model Builder.  While developing the script, comments were utilized to annotate each section of code.  The following steps were taken: 

  • importing system modules
  • importing Python modules: os, time, datetime
  • creating variables
  • inputting feature class names
  • linking geodatabase path with file names
  • processing data: clipping, selecting, buffering, intersecting, dissolving*
  • debugging and running script

*To expedite carrying out these processes, the model from Exercise 1 was exported to a Python script and portions of that code were copied and updated.  

Results

The final script is displayed below.  After successfully running this script, the results were viewed in ArcMap. 

#-------------------------------------------------------------------
# Name:         Ex3
# Purpose:      Finds ideal areas for ski resorts
#
# Author:      williaan
#
# Created:     15/07/2016

#-------------------------------------------------------------------

#import system modules
import arcpy
from arcpy import env
import os
import time
import datetime
arcpy.env.overwriteOutput = True

#create variables
print "Creating Variables" ,datetime.datetime.now().strftime("%H:%M:%S")

#input geodatabase
ex1geodatabasePath = "Q:\StudentCoursework\CHupy\GEOG.491.801.2167\WILLIAAN\Ex_1\Ex1.gdb"

#input names
nfsLand = "NFS_Land"
snowDepth = "SnowDepth_in"
temp = "Temperature_F"
studyArea = "StudyArea"
airports = "Airports"

#linking geodatabase with name
nfsInput = os.path.join(ex1geodatabasePath,nfsLand)
snowDepthInput = os.path.join(ex1geodatabasePath,snowDepth)
tempInput = os.path.join(ex1geodatabasePath,temp)
studyAreaInput = os.path.join(ex1geodatabasePath,studyArea)
airportsInput = "Q:\StudentCoursework\CHupy\GEOG.491.801.2167\WILLIAAN\Ex_1\Ex1.gdb\Airports_Project"

#output variables
ex3geodatabasePath = "Q:\StudentCoursework\CHupy\GEOG.491.801.2167\WILLIAAN\Ex_3\Ex3_results.gdb"

#clipped
nfsLandClip = os.path.join(ex3geodatabasePath,nfsLand) + "_Clip"
snowDepthClip = os.path.join(ex3geodatabasePath,snowDepth) + "_Clip"
tempClip = os.path.join(ex3geodatabasePath,temp) + "_Clip"
airportsClip = os.path.join(ex3geodatabasePath,airports) + "_Clip"

#selected
snowDepthSelected = os.path.join(ex3geodatabasePath,snowDepth) + "_Selected"
tempSelected = os.path.join(ex3geodatabasePath,temp) + "_Selected"
airportsSelected = os.path.join(ex3geodatabasePath,airports) + "_Selected"

#buffer output
airportsBuffered = os.path.join(ex3geodatabasePath,airports) + "_Buffered"

#intersect output
intersectName = "IntersectedFcs"
intersectOutput = os.path.join(ex3geodatabasePath,intersectName)

#dissolve output
dissolveOutput = os.path.join(ex3geodatabasePath,intersectName) + "_Dissolved"

#final select
finalSelect = os.path.join(ex3geodatabasePath,intersectName) + "_MoreThan2km2"

#begin processing 
print "Starting to Process" ,datetime.datetime.now().strftime("%H:%M:%S")


#clip all feature classes to the study area
print "Clipping fcs to within the study area" ,datetime.datetime.now().strftime("%H:%M:%S")
#clip nfs land
arcpy.Clip_analysis(nfsInput, studyAreaInput, nfsLandClip, "")

#clip temp
arcpy.Clip_analysis(tempInput, studyAreaInput, tempClip, "")

#clip snow depth
arcpy.Clip_analysis(snowDepthInput, studyAreaInput, snowDepthClip, "")

#clip airports
arcpy.Clip_analysis(airportsInput, studyAreaInput, airportsClip, "")


#select meaningful values from clipped classes
print "Selecting meaningful values from clipped classes" ,datetime.datetime.now().strftime("%H:%M:%S")
#select temp
arcpy.Select_analysis(tempClip, tempSelected, "gridcode<32")

#select snow depth
arcpy.Select_analysis(snowDepthClip, snowDepthSelected, "gridcode>240")

#select airports
arcpy.Select_analysis(airportsClip, airportsSelected, "OwnerType = 'Pu' AND hasTower = 'Y'")


#buffering selected airports
print "Buffering selected airports" ,datetime.datetime.now().strftime("%H:%M:%S")
#buffer
arcpy.Buffer_analysis(airportsSelected, airportsBuffered, "40 Miles", "FULL", "ROUND", "ALL", "", "PLANAR")


#intersecting the fcs
print "Intersecting the fcs" ,datetime.datetime.now().strftime("%H:%M:%S")
#intersect
arcpy.Intersect_analysis([snowDepthSelected,tempSelected,nfsLandClip,airportsBuffered], intersectOutput, "ALL", "", "INPUT")


#dissolving the intersected fcs
print "Dissolving the intersected fcs" ,datetime.datetime.now().strftime("%H:%M:%S")
arcpy.Dissolve_management(intersectOutput, dissolveOutput, "", "", "SINGLE_PART", "DISSOLVE_LINES")

print "Script is complete."


No comments:

Post a Comment