Pages

Monday, July 18, 2016

Exercise 3: Intro to Geoprocessing With Python

The Objectives

The goal of this assignment was to become familiar with importing modules to set up a script, creating smart variables, and exporting geoprocessing models as scripts. This task was accomplished by simply recreating the steps done in the first exercise of the class, through scripting in Python.

The Process

First, there had to be a connection to the model created in exercise one, this connection was made by exporting the model as a .py file and saved to a geodatabase created for exercise three. Then, ArcPy modules and Python modules were imported into Python in order to be able to write the script. These modules included: os, time, and datetime. Next, variables were created by linking files to their respective geodatabases then tools were used to manipulate the data. Once the data was clipped, buffered, selected, etc. it was exported to the new geodatabase that was created for this exercise. Lastly, the data was cleaned-up, so-to-speak, and the script generated a new file within the geodatabase.

The Results

#-------------------------------------------------------------------------------
# Name:        Ex3
# Purpose:     Finds ideal areas for ski resorts
#
# Author:      Zach Miller
#
# Date:        7/15/2016
#-------------------------------------------------------------------------------
# Ensuring the program is working
print "Script initialized"

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

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

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

#inout 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\MILLERZM\Ex1.gdb\Airports_Project"

#output variables
ex3geodatabasePath = "Q:\StudentCoursework\CHupy\GEOG.491.801.2167\MILLERZM\BlankResults_Ex3.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"
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 temp
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 from the clipped classes
print "Selecting only 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"Script is complete"

Sources

All sources provided by Dr. Christina Hupy

No comments:

Post a Comment