Python

In this class, we will be learning how to write code, using Python, and specifically using PyScripter.  Utilizing python can be used to automate processes in GIS that would otherwise take much more time and energy.  I will be posting Python codes, along with explanations for what the codes are, and what they do.



#-------------------------------------------------------------------------------
# Name:         Project,clip and load all the data into a geodatabase
# Purpose:
#
# Author:      Jacob Henden
#
# Created:     10/19/2015

#-------------------------------------------------------------------------------
#This code is used to project the rasters we have and clip them to Trempealeau county
from arcpy.sa import *
import arcpy
from arcpy import env
arcpy.CheckOutExtension("spatial")

# Set the workspace

arcpy.env.workspace= "Q:\StudentCoursework\CHupy\GEOG.337.001.2161\HENDENJL\Ex5\Raster"
arcpy.env.overwriteOutput = True

#Get and print a list of TIF rasters from the workspace

rasters = arcpy.ListRasters("*", "TIF")
for raster in rasters:
    print(raster)

#set up an output variable

#Run a for loop so that it goes through every raster in the list
for raster in rasters:
    rasterOut = '{}_Out.tif'.format(raster)
    rasterExtract = '{}_Extract.tif'.format(raster)

    #project rasters into an appropriate coordinate system
    #Set the output coordinate system by using the directory of a feature class
    #located in the TrempWebDATA.gdb


 arcpy.ProjectRaster_management(raster, rasterOut, "Q:\StudentCoursework\CHupy\GEOG.337.001.2161\HENDENJL\Ex5\Raster\TrempWebDATA.gdb\Boundaries\County_Boundary")
    print raster + " has been projected."

    #Clip the rasters to the TMP County Boundary

    outExtractByMask = ExtractByMask(rasterOut,"Q:\StudentCoursework\CHupy\GEOG.337.001.2161\HENDENJL\Ex5\Raster\TrempWebDATA.gdb\Boundaries\County_Boundary")
    print raster + " has been extracted"

    #Use Raster to Geodatabase function to load the appropriately clipped and
    #Projected rasters into the a goedatabase


    arcpy.RasterToGeodatabase_conversion (outExtractByMask,"Q:\StudentCoursework\CHupy\GEOG.337.001.2161\HENDENJL\Ex5\Raster\TrempWebDATA.gdb")
    print raster + "has been sent to the TMP geodatabase"

print "the script has completed"


#-------------------------------------------------------------------------------
# Name:        Excercise 7
# Purpose:
#
# Author:      hendenjl
#
# Created:     10/11/2015
# Copyright:   (c) hendenjl 2015
# Licence:     <your licence>
#-------------------------------------------------------------------------------

#  In this excersise we use python to perform queries on our data.
#  we will be selected active mines that are further than 1.5 miles away
#  from a rail line

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

#Step 1:  Set variables
#Step 2:  Add field delimters
#Step 3:  Build a SQL statements using a field delimiters
#Step 4:  Create Feature Layers using the SQL statements and the MakeFeatureLayer_managementfunction
#Step 5:  Selectbylocation to remove mines within 1.5km of a rail line
#Step 6:  Save the output using CopyFeatures tool

# Step 1
am= "all_mines"
acm= "active_mines"
mf= "mine_facilities"
sm= "status_mines"
mnr= "mines_norail"
mnrf= "mines_norail_final"
frt= "final_rail_terminals"
rt= "rail_terminals"
rw= "rails_wtm"
wi= "wi"

#Step 2
status = arcpy.AddFieldDelimiters (am, "Site_Statu")
facility = arcpy.AddFieldDelimiters (am, "Facility_T")
norail = arcpy.AddFieldDelimiters(am, "Facility_T")

#Step 3
activeSQL = status + "=" + "'Active'"
mineSQL = facility + "LIKE" + "'%Mine%'"
norailSQL = "NOT" + norail + "LIKE" + "'%Rail%'"

print "SQL satements written"

#Step 4
arcpy.MakeFeatureLayer_management(am, acm, activeSQL)
arcpy.MakeFeatureLayer_management(am, mf, mineSQL)
arcpy.MakeFeatureLayer_management(am, mnr, norailSQL)

print "feature layers created"

#Step 5
arcpy.SelectLayerByLocation_management(mnr, "INTERSECT", wi)
arcpy.SelectLayerByLocation_management(mnr, "WITHIN_A_DISTANCE", "rails_wtm","1.5 KILOMETERS", "REMOVE_FROM_SELECTION")

print "mines within 1.5KM removed"

#Step 6
arcpy.CopyFeatures_management(mnr, "Q:\StudentCoursework\CHupy\GEOG.337.001.2161\HENDENJL\Ex7\ex7.gdb")      

print "the script has completed"

#-------------------------------------------------------------------------------
# Name:        Exercise 8
# Purpose:
#
# Author:      Jacob Henden
#
# Created:     12/11/2015
#-------------------------------------------------------------------------------

# In this python script we generate a weighted index model
# for sand mining suitability.  A weighted index model allows you

# to account for the fact that not all factors in your model will be of equal 
# importance.  Therefore, you can multiple rasters by numbers in order to 
# increase or decrease the effect that they will have on your final model
# in this lab we will use rasters generated in the mining suitability model 
# lab.


# Steps
# Step 1.  Import system modules and set workspace
# Step 2.  Import spatial analyst 
# Step 3.  Set variables
# Step 4.  Assign weight
# Step 5.  Add rasters together
# Step 6.  Save output
# Step 7.  Print statement


# Step 1 Import system modules and set workspace

import arcpy
env.workspace="Q:\StudentCoursework\CHupy\GEOG.337.001.2161\HENDENJL\EX8\TrempWebDATA.gdb"
arcpy.env.overwriteOutput = True

# Step 2.  Import spatial analyst

arcpy.CheckOutExtension("Spatial")

#Step 3.  Set variables

streams= arcpy.Raster("distance_streams")
Residential= arcpy.Raster("resident_rank")
Landfill= arcpy.Raster("lanfills_ranked")
School= arcpy.Raster("schools_ranked")
farm= arcpy.Raster("farm_rank")

# Step 4.  Assign weight
# Choose one raster, and give it a stronger weight.
# Multiply this factor by 1.5
# I chose streams, because disruption could have
# large environmental costs


outweight= (streams*1.5)

# Step 5. Add rasters together

weighted= (outweight + farm + School + Landfill + Residential )

# Step 6.  Save the output
weighted.save("weighted_ranking")

# Step 7.  Print statement
print "The script has completed"

No comments:

Post a Comment