Wednesday, July 20, 2016

Geog 491: Exercise 4- Working with Fields and Selections

Goal and Background

To use python to add a field, calculate a field and apply and SQL statement.  The objectives for this exercise are stated below,

  • Set up the script and import the modules.
  • Set up smart variables.
  • Add a field and calculate a field.
  • Select features based on an SQL statement.
  • Add a field and calculate for selected features.
Methods

The comments in green were added along the way in order to keep the script in order.  Add and field and calculate field were used along with an SQL statement in order to produce results to be used in a future exercise.  From these fields, a select tool was used and in the end compactness was calculated.  Below is the script written for this exercise.  Exercise 3 data was used.

Results


#-------------------------------------------------------------------
# Name:        Exercise 4
# Purpose:     Find ideal locations for a ski resort by calculating #              compactness
#
# Author:      Laura Hartley, hartlelj
#
# Date:        7/18/2016
#-------------------------------------------------------------------

#Ensure the program is working
print "script initialized"

#import system modules
import arcpy
from arcpy import env
import os
import time
import datetime

#Allow the script to overwrite existing files
arcpy.env.overwriteOutput = True

#Create variables
print "Creating Variables"
,datetime.datetime.now().strftime("%H:%M:%S")
#input variables
ex3geodatabasePath = "C:\Users\lhartley13\Documents\ArcGIS\EX3\EX3.gdb"

#This filename will be used for the subsequent 'dissolve output' fc name
intersectName = "IntersectedFcs"
dissolveOutput = os.path.join(ex3geodatabasePath,intersectName)+"_Dissolved"

#output variables
selectName = "DissolveFC_Selected"
finalSelect = os.path.join(ex3geodatabasePath,selectName)

print "Adding a field to the dissolve fcs" ,datetime.datetime.now().strftime("%H,%M,%S")
#Add field
arcpy.AddField_management(dissolveOutput, "Area_km2", "FLOAT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")

print "Calculating the area in km2" ,datetime.datetime.now().strftime ("%H:%M:%S")
#Calculate field
arcpy.CalculateField_management(dissolveOutput, "Area_km2",  
"[Shape_Area] / 1000000", "VB", "")

print "Selecting only polygons with areas greater than 2km" ,datetime.datetime.now().strftime ("%H:%M:%S")
#Select (4)
arcpy.Select_analysis(dissolveOutput, finalSelect, "Area_km2 >2")

print "Adding a field for calculating compactness" ,datetime.datetime.now().strftime("%H:%M:%S")
#Add field (2)
arcpy.AddField_management(finalSelect,"Compactness_Float", "FLOAT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")

print "Calculating compactness" ,datetime.datetime.now().strftime("%H:%M:%S")
#Calculate field (2)
arcpy.CalculateField_management(finalSelect, "Compactness_Float"
"[Shape_Area] / ([Shape_Length] )", "VB", "")

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

No comments:

Post a Comment