Wednesday, July 20, 2016

Geog 491: Exercise 3- Introduction to Geoprocessing with Python

Goal and Background

The main goal of this exercise is to export a geoprocessing model as a script and add multiple smart variables and modify the exported script.  The objectives needed to follow in order to complete this exercise were:

  • Export a geoprocessing model as a python script.
  • Importing modules and setting up a script.
  • Creating smart variables.
  • Setting up a geoprocessing workflow.

Methods

The exercise 1 data was used in this exercise in order to further manipulate the data.  Green comments in the script below were used to describe what was happeneing in the process below it.  Print statements along with time stamps were used to keep track of when the tools began running along with getting more familiar with print and time statements.  Below is the script used in exercise 3 in order to produce the above stated objectives.

Results

#-------------------------------------------------------------------
# Name:        Exercise 3
# Purpose:     To export a geoprocessing model as a script, add     #              several smart
#              variables and modify the exported script.  Finding   #              ideal areas for
#              ski resorts
#
# Author:      Laura Hartley, hartlelj
#
# Date:        7/15/2016
#-------------------------------------------------------------------
#ensure the program is working
print "script initialized"

#import arcpy and set up the environments
import arcpy
from arcpy import env
import os
import time
import datetime

#overwrite allows us to overwrite files and save over them
arcpy.env.overwriteOutput = True

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

#input geodatabse
ex1geodatabasePath = "C:\Users\lhartley13\Documents\ArcGIS\EX1\Ex1.gdb"

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

#linking geodatabse 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 = "C:\Users\lhartley13\Documents\ArcGIS\EX1\Ex1.gdb\Airports_project"

#output Variables
ex3geodatabasePath = "C:\Users\lhartley13\Documents\ArcGIS\EX3\EX3.gdb"

#clip the layers
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"

#select the layers
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) + "_Buffer"

#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")

print "Clipping fcs to within the study area" ,datetime.datetime.now().strftime("%H:%M:%S")
#clip all of the feature classes to the study area
#clip snow depth
arcpy.Clip_analysis(snowDepthInput, studyAreaInput,
snowDepthClip, "")

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

#clip nfs land
arcpy.Clip_analysis(nfsInput, studyAreaInput, nfsLandClip, "")

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

#select meaningful values froom the clipped classes
print "Selecting meaningful values from the clipped classes",datetime.datetime.now().strftime("%H:%M:%S")
arcpy.Select_analysis(snowDepthClip,snowDepthSelected, "gridcode >= 250")

#process: select(2)
arcpy.Select_analysis(tempClip, tempSelected, "gridcode<32")

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

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

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

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


print "Calculations complete",datetime.datetime.now().strftime("%H:%M:%S")

No comments:

Post a Comment