Sunday, March 12, 2017

Python Scripting

Python scripting is used to automate GIS processes. It is done in order to be able to process large amounts of data more efficiently to reduce overhead.  Once proficient in the scripting language, one can execute processes much faster than running them all manually.

#-------------------------------------------------------------------------------
# Name:        Exercise 5: Data Gathering
#
# Author:      Kevin Trushenski
#
# Created:     08/03/2017
#
# Purpose: To learn how to write a python script to project, clip, and load data into a geodatabase.
#-------------------------------------------------------------------------------

#import python module and spatial analyst

import arcpy
from arcpy import env
from arcpy.sa import *

#check out the spatial analyst extension
arcpy.CheckOutExtension("spatial")

#set environment settings
arcpy.env.workspace = "Q:\StudentCoursework\CHupy\GEOG.337.001.2175\TRUSHEKL\Ex5\Data"
arcpy.env.overwriteOutput = True
print "{}" .format(env.workspace)

#get list of rasters from workspace
rs_list = arcpy.ListRasters()
for raster in rs_list:
    print(raster)


#loop through the rasters in the loop
for raster in rs_list:
    #define the outputs
    rasterOut = "{}_Out.tif".format(raster)
    rasterExtract = "{}_Extract.tif".format(raster)

    #project the rasters
    arcpy.ProjectRaster_management(raster, rasterOut, "Q:\StudentCoursework\CHupy\GEOG.337.001.2175\TRUSHEKL\Ex5\Data\TrempWebDATA.gdb\Boundaries\County_Boundary")

    #extract the raster and copy the raster into the geodatabase
    outExtractByMask = ExtractByMask(rasterOut, "Q:\StudentCoursework\CHupy\GEOG.337.001.2175\TRUSHEKL\Ex5\Data\TrempWebDATA.gdb\Boundaries\County_Boundary")
    outExtractByMask.save(rasterExtract)
    arcpy.RasterToGeodatabase_conversion(rasterExtract, "Q:\StudentCoursework\CHupy\GEOG.337.001.2175\TRUSHEKL\Ex5\Data\TrempWebDATA.gdb")
    print "Raster to Geodatabase Conversion {} Successful".format(rasterExtract)


print "The script is complete"



#-------------------------------------------------------------------------------
# Name:      Ex 7 Network Analysis
# Purpose: To create a script that will select active mines that don't have a rail loading station on-site.  It also eliminates mines within 1.5 km of a rail because it is likely a rail spur has already been added.
#
# Author:      Kevin Trushenski
#
# Created:     10/04/2017
#-------------------------------------------------------------------------------

#import system modules
import arcpy
#set environments
from arcpy import env
env.workspace = "Q:\StudentCoursework\CHupy\GEOG.337.001.2175\TRUSHEKL\ex7\ex7.gdb"
arcpy.env.overwriteOutput = "true"

#Set variables

all = "all_mines"
active= "active_mines"
mines = "Status_mine"
norail = "mines_norail"
wi= "wi"
rail = "rails_wtm"
worail = "mines_norail_final"

#Set up the field delimiters for the SQL statements

field1= arcpy.AddFieldDelimiters(all, "Site_Statu")
field2= arcpy.AddFieldDelimiters(all, "Facility_T")

#SQL statement to select active mines

activeSQL = field1 + "=" + "'Active'"

#SQL statement for field facility_T LIKE mine

mineSQL = field2 + "LIKE" + "'%Mine%'"

#SQL statement for field facility_T NOT rail

norailSQL = "NOT" + field2 + "LIKE" + "'%Rail%'"

#Make a Layer from the feature class with mine status = active

arcpy.MakeFeatureLayer_management(all, active, activeSQL)

#Make a layer from the feature class with facility type = mine

arcpy.MakeFeatureLayer_management(active, mines, mineSQL)

#Make a layer from the feature class without rails

arcpy.MakeFeatureLayer_management(mines, norail, norailSQL)

#Select

arcpy.SelectLayerByLocation_management(norail, "INTERSECT", wi)
arcpy.SelectLayerByLocation_management(norail, "WITHIN_A_DISTANCE", rail, "1.5 KILOMETER", "REMOVE_FROM_SELECTION")

arcpy.CopyFeatures_management(norail, worail)


print "The script is complete."


#-------------------------------------------------------------------------------
# Name:  Exercise 8: Raster Analysis Python
# Purpose: To write a python script to generate a weighted index model in order to place more emphasis on a certain variable in the Risk Model from Exercise 8
#
# Author:      Kevin L Trushenski
#
# Created:     16/05/2017
#-------------------------------------------------------------------------------
#import system modules
import arcpy

#set environments
from arcpy import env
env.workspace = "Q:\StudentCoursework\CHupy\GEOG.337.001.2175\TRUSHEKL\Ex5\Data\TrempWebDATA.gdb"
arcpy.env.overwriteOutput = "true"
arcpy.CheckOutExtension("Spatial")

#Create variables
Parks= arcpy.Raster("parksReclass")
Res= arcpy.Raster("res_reclass")
River= arcpy.Raster("river_clip_reclass")
Schools= arcpy.Raster("schoolReclass")
Farm= arcpy.Raster("farm_reclass")

#Weight most important variable
outweight= (Res*1.5)

#Set up equation for weighted index model
Weighted= (outweight + Parks + River +Schools + Farm)

#Save to geodatabase
Weighted.save("weighted_result")

print "The Script is Complete."


No comments:

Post a Comment