Wednesday, March 8, 2017

Python Script

The following exercises demonstrate the ability to perform functions using python script and a familiarity of coding and python language fundamental to GIS applications.

EXERCISE 7

Exercise 7 prepares data for network analysis to be performed. In preparation, a series of queries and select by location was performed to select only the data needed to be used in network analysis

#-------------------------------------------------------------------------------
# Name:        Exercise 7
# Purpose:
#
# Author:      kraegebj
#
# Created:    04/23/2017
# Copyright:   (c) kraegebj 2017
# Licence:     <your licence>
#-------------------------------------------------------------------------------

import arcpy
from arcpy import env
arcpy.env.workspace ="Q:\StudentCoursework\CHupy\GEOG.337.001.2175\KRAEGEBJ\Excersices\Exercise_7\ex7.gdb"
arcpy.env.overwriteOutput=True

#Set up the variables
Fc1 = "all_mines"
Fc2 = "rail_terminals"
Fc3 = "rails_wtm"
Fc4 = "wi"
Fc5 = "active_mines"
Fc6 = "status_mines"
Fc7 = "mines_norail"
Fc8 = "mines_norail_final"

#Set up the field delimiters for the SQL statments
field1 = arcpy.AddFieldDelimiters (Fc1, "Site_Statu")
field2 = arcpy.AddFieldDelimiters (Fc1, "Facility_T")

#SQL statement to select active mines
activeSQLExp = field1 + "=" + "'Active'"
mineSQLExp = field2 + " LIKE " + "'%Mine%'"
norailSQLExp = " NOT " + field2 + "LIKE" + "'%Rail%'"

#Make a layer from the feature class with mine status = active
arcpy.MakeFeatureLayer_management (Fc1, "active_mines", activeSQLExp)

#Make a layer from the feature class with facility type = mine
arcpy.MakeFeatureLayer_management (Fc5, "status_mines", mineSQLExp)

#Make a layer from the feature class without rails
arcpy.MakeFeatureLayer_management (Fc6, "mines_norail", norailSQLExp)

#Select by location
arcpy.SelectLayerByLocation_management (Fc7, "INTERSECT", Fc4)
arcpy.SelectLayerByLocation_management (Fc7, "WITHIN_A_DISTANCE", Fc3, "1.5 KILOMETER", "REMOVE_FROM_SELECTION")

arcpy.CopyFeatures_management (Fc7, Fc8)

print "The script is complete"

EXERCISE 5

Exercise 5 reprojects previously gathered tifs--NLCD, DEM, and NASS from the USDA and USGS (see Exercise 5 blog)--into a single projection to be used in conjunction to give a comprehensive view of Trempealeau County, Wisconsin.

#-------------------------------------------------------------------------------
# Name:        Exercise 5
# Purpose:
#
# Author:      kraegebj
#
# Created:     08/03/2017
# Copyright:   (c) kraegebj 2017
# Licence:     <your licence>
#-------------------------------------------------------------------------------

#Import system modules and Spatial Analyst
import arcpy
from arcpy import env
from arcpy.sa import *
arcpy.CheckOutExtension("spatial")

#Set environment settings
arcpy.env.workspace = "Q:\StudentCoursework\CHupy\GEOG.337.001.2175\KRAEGEBJ\Excersices\Exercise_5\Working\Script"
arcpy.env.overwriteOutput = True
print "{}".format(env.workspace)

#Get a list of rasters in the workspace
listOfRasters = arcpy.ListRasters()
print "{}".format(listOfRasters)

#Loop through the rasters in the loop
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\KRAEGEBJ\Excersices\Exercise_5\Working\Script\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\KRAEGEBJ\Excersices\Exercise_5\Working\Script\TrempWebDATA.gdb\Boundaries\County_Boundary')
    outExtractByMask.save(rasterExtract)
    arcpy.RasterToGeodatabase_conversion(rasterExtract, 'Q:\StudentCoursework\CHupy\GEOG.337.001.2175\KRAEGEBJ\Excersices\Exercise_5\Working\Script\TrempWebDATA.gdb')
    print "Raster to Geodatabase conversion {} sucessful".format(rasterExtract)

>>>
Q:\StudentCoursework\CHupy\GEOG.337.001.2175\KRAEGEBJ\Excersices\Exercise_5\Working\Script
[u'DEM.tif', u'NASS.tif', u'NLCD2011.tif']
Raster to Geodatabase conversion DEM.tif_Extract.tif sucessful
Raster to Geodatabase conversion NASS.tif_Extract.tif sucessful
Raster to Geodatabase conversion NLCD2011.tif_Extract.tif sucessful

>>>

No comments:

Post a Comment