Pages

Monday, July 18, 2016

Exercise 4: Working With Fields and Selections

The Objectives
The goal of this exercise was to add and calculate fields as well as select features using SQL statements in Python. This was done by using the script (and data) from exercise three and modifying some of the tools used.

The Process

First, the system modules needed for this exercise were imported. Next, variables were set up. Then, tools 'add field', 'calculate field', and 'select' were used. Finally, compactness was calculated and the exercise was completed.

The Results
#-------------------------------------------------------------------------------
# Name:        Ex4
# Purpose:     Calculate a Field and Apply an SQL Statement
#
# Author:      Zach Miller
#
# Date:        7/18/2016
#-------------------------------------------------------------------------------

#Ex4

#ensuring the program is working
print "Script initialized"

#import system models
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"Create Variables",datetime.datetime.now().strftime("%H:%M:%S")

#input geodatabase
ex3geodatabasePath="Q:\StudentCoursework\CHupy\GEOG.491.801.2167\MILLERZM\BlankResults_Ex3.gdb"

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

#outputVariables
selectName = "DissolveFC_Selected"
finalSelect = os.path.join(ex3geodatabasePath,selectName)
print "Adding a field to the dissolved fcs",datetime.datetime.now().strftime("%H:%M:%S")

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

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

# Process: Select (4)
arcpy.Select_analysis(dissolveOutput, finalSelect, "Area_km2 >2")
print "Adding a field for calculating compactness",datetime.datetime.now().strftime("%H:%M:%S")

# Process: Add Field (2)
arcpy.AddField_management(finalSelect, "Compactness_Float", "FLOAT", "", "", "", "","NULLABLE", "NON_REQUIRED", "")
print "Calculating compactness" ,datetime.datetime.now().strftime("%H:%M:%S")

# Process: Calculate Field (2)
arcpy.CalculateField_management(finalSelect, "Compactness_Float", "[Shape_Area] / ([Shape_Length] * [Shape_Length] )", "VB", "")


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

Sources

All sources provided by Dr. Christina Hupy

No comments:

Post a Comment