Thursday, April 14, 2016

Python Scripting Three: Creating a Feature Class with Search Criteria

The Python script below was created to find the Wisconsin sand mines most likely to deteriorate Wisconsin public roads via the transportation of sand from the mine to the nearest rail depot. The script took all 129 recorded mines in Wisconsin and thinned down the number to 44. These 44 mines only were not classified as a rail depot or and are further than 1.5 kilometers away from a rail line. 





#-------------------------------------------------------------------------------
# Name: EX_7 Mine Selection
# Purpose: To select sand mines based on below criteria for the state
#    of wisconsin
#
# Author:      James S. Erickson
#
# Created:     04/12/2016
#-------------------------------------------------------------------------------
# Criteria for sand mine selection
# 1) The mine must be active.
# 2) The mine must not also have a rail loading station on-site. If the mine is
#       also a rail loading station the sand will be loaded directly onto the
#       rail line and sand will not be trucked to the nearest rail depot ?
#       therefore it will not impact local roads.
# 3) If the mine is within a 1.5 km of the rail it is likely that a spur has been
#       built. The railroad data set is not completely up to date and therefore
#       does not have all rail spurs. Therefore we will eliminate any mines that
#       are within 1.5 km of the railroads.
#-------------------------------------------------------------------------------

#importing arcpy module to the script and setting enviornments
print "starting setup"
import arcpy
#enviornments from arcmap- can set home geodatabase with this amongst all other enviornmental settings
from arcpy import env
#env.workspace = <file path> is the line to set this geodatabase as home
env.workspace = "Q:\StudentCoursework\CHupy\GEOG.337.001.2165\ERICKSJS\EX_7\ex7.gdb"
print "workspace defined"
# able to overwrite.
arcpy.env.overwriteOutput = True
print "Overwrite enabled"
print "Setup Complete"


#defining variables
Mines = "all_mines"
terminals = "rail_termimals"
rails = "rails_wtm"
state = "wi"
# Script created layers below
norails = "mines_norail"
M_type = "type_mines"
active = "active_mines"
norailfinal = "mines_norail_final"
print "Variables defined"

#setting up field delimeters for later SQL statements
#active Mines:
field_AM = arcpy.AddFieldDelimiters(Mines, "Site_Statu")
#mine facility type; used for selecting "mines" and deselecting "rail"
field_FT = arcpy.AddFieldDelimiters(Mines, "Facility_T")
print "Field Delimiters defined"

#SQL statement for selecting acitve mines
activeSQL = field_AM + " = " + "'Active'"
#SQL statement for "mine" in facility type
mineSQL = field_FT + " LIKE " + "'%Mine%'"
#SQL statement for no rails in facility type
norailSQL = "NOT" + field_FT + "LIKE" + "'%Rail%'"
print "SQL statements defined"

#Creating feature layer from SQL statements
# Making feature layer for active mines
arcpy.MakeFeatureLayer_management(Mines, active, activeSQL)
# Making feature layer for mine facility type
arcpy.MakeFeatureLayer_management(active, M_type, mineSQL)
# Making feature layer to exclude Rail in feature type
arcpy.MakeFeatureLayer_management(M_type, norails, norailSQL)
print "Feature layers constructed"

print "Selecting mines"
# removing all mines within 1.5km of a railroad
# selecting all mines in Wisconsin
arcpy.SelectLayerByLocation_management(norails, "COMPLETELY_WITHIN", state)
#removing mines from above selection within 1.5km of railroad
arcpy.SelectLayerByLocation_management(norails, "WITHIN_A_DISTANCE", rails,
    "1.5 KILOMETERS", "REMOVE_FROM_SELECTION")

#printing the number of selected mines
featCount = arcpy.GetCount_management(norails)
print "Number of mines selected: {}".format(featCount)

#Creating new feature class from selected mines/saving selected features
arcpy.CopyFeatures_management(norails, norailfinal)
print "New feature class saved"

print "Script Complete"


No comments:

Post a Comment