Pages

Monday, March 13, 2017

Python Scripts

Python Script for Exercise 8
#-------------------------------------------------------------------------------
# Name:        Ex 8 - Part Two
# Purpose:     Calculates
#
# Author:      Zach Miller
#
# Created:     05/15/2017
# Copyright:   (c) millerzm 2017
#-------------------------------------------------------------------------------

#import spatial settings
import arcpy
from arcpy import env
from arcpy.sa import*
arcpy.CheckOutExtension("spatial")

#set workspace
arcpy.env.workspace = "Q:\StudentCoursework\CHupy\GEOG.337.001.2175\MILLERZM\ex8\Exercise_8.gdb"
arcpy.env.overwriteOutput = True
print "{}".format(env.workspace)

#Set up variables
streams = arcpy.Raster("dist2river_reclass")
farms = arcpy.Raster("dist2farms_reclass")
schools = arcpy.Raster("dist2schools_reclassify")
people = arcpy.Raster("dist2people_reclass")
wildlife = arcpy.Raster("dist2WLA_reclassify")

#Calculating the most important variable
outweight = (schools*1.5)

#Overlaying rasters with weighted variable
weighted = (outweight + streams + farms + people + wildlife)
weighted.save("weighted_result")


print "The script is complete"

Python Script for Exercise 7

#-------------------------------------------------------------------------------
# Name:        Ex 7 - part 1
# Purpose: To perform a network analysis on frac sand transportation
#
# Author:      millerzm
#
# Created:     10/04/2017
#-------------------------------------------------------------------------------

#import system modules and set up workspace
import arcpy
from arcpy import env
arcpy.env.overwriteOutput=True
arcpy.env.workspace = "Q:\StudentCoursework\CHupy\GEOG.337.001.2175\MILLERZM\Ex7\ex7.gdb"

#set up variables
all = "all_mines"
active = "active_mines"
status = "status_mines"
railess = "mines_norail"
wi ="wi"
wtm = "rails_wtm"
final = "mines_norail_final"

#Add field delimiters for the SQL statements
field1 = arcpy.AddFieldDelimiters(all, "Site_Statu")
field2 = arcpy.AddFieldDelimiters(all, "Facility_T")

#SQL statements to select active mines, have mine facility type, and do not have rail
activeSQL = field1 + "=" + "'Active'"
statusSQL = field2 + "LIKE" + "'%Mine%'"
norailSQL = "NOT" + field2 + "LIKE" + "'%Rail%'"

#create feature layer from SQL statement
arcpy.MakeFeatureLayer_management(all, "active_mines", activeSQL)
arcpy.MakeFeatureLayer_management(active, "status_mines", statusSQL)
arcpy.MakeFeatureLayer_management(status, "mines_norail", norailSQL)

#select all mines from mines_norail layer within wi layer
arcpy.SelectLayerByLocation_management(railess, "INTERSECT", wi)

#remove all mines within 1.5 km of rails within mines_norail_final layer
arcpy.SelectLayerByLocation_management(railess, "WITHIN_A_DISTANCE", wtm, "1.5 KILOMETERS", "REMOVE_FROM_SELECTION")

#save selected features
arcpy.CopyFeatures_management(railess, final)


print "The script is complete"

Discussion

This script works by first bringing in the neccessary system modules and setting up the workspace so python knows where to grab the data from and where to put the outputs once tools in the script have been ran. Nest, variables were assigned to be defined throughout the script. *** To my pleasure, the script worked the first time and I ran into no real issues with it. The script selected 44 mines to be used for the network analysis.

Python Script for Exercise 5


#-------------------------------------------------------------------------------
# Name:        Ex 5
# Purpose:      Project, clip, and load data into geodatabase
#
# Author:      Zach Miller
#
# Created:     08/03/2017
# Copyright:   (c) millerzm 2017
# Licence:     <your licence>
#-------------------------------------------------------------------------------

#import spatial settings
import arcpy
from arcpy import env
from arcpy.sa import*
arcpy.CheckOutExtension("spatial")

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

#get a list of rasters
ListOfRasters = arcpy.ListRasters()
print "{}".format(ListOfRasters)

#loop through the rasters
for raster in ListOfRasters:
    #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\MILLERZM\Ex5\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\MILLERZM\Ex5\TrempWebDATA.gdb\Boundaries\County_Boundary")
    outExtractByMask.save(rasterExtract)
    arcpy.RasterToGeodatabase_conversion(rasterExtract, "Q:\StudentCoursework\CHupy\GEOG.337.001.2175\MILLERZM\Ex5\Work")
    print "Raster to Geodatabase conversion {} successful".format(rasterExtract)


print "Script is complete"

No comments:

Post a Comment