The main purpose of this exercise is to use a number of raster and vector tools to identify common characteristics of landslides in Oregon. The objectives are stated below:
After creating the basic start of the script (importing system modules, allowing script to overwrite existing files, etc) naming conventions were set up for output files. Lists were created next because many features were to be created and then deleted later on. Lists make this process easier. Field names were set and geoprocessing tools were run. Later, and update cursor was used in order to update or delete rows in specified feature classes and more. Statistics were calculated and exported to a MS Excel file. The script for this exercise is stated below.
# Name: Exercise 6
# Purpose: Calculate the landscape characteristics of landslides # based on precipitation, slope, and landcover.
#
# Author: Laura Hartley, hartlelj
#
# Date: 7/25/2016
#-------------------------------------------------------------------
#Ensure the program is working
print "Script initialized"
#import system modules
import arcpy
from arcpy import env
import os
import shutil
import time
import datetime
from arcpy.sa import *
from arcpy.ddd import *
#Allow the script to overwrite existing files
arcpy.env.workspace = "C:\Users\lhartley13\Documents\ArcGIS\EX6"
arcpy.env.overwriteOutput = True
arcpy.env.outputCoordinateSystem = "C:\Users\lhartley13\Documents\ArcGIS\EX5\Ex5\Ex5.gdb\OregonBoundary"
#NAD_1983_HARN_Oregon_Statewide_Lambert
arcpy.CheckOutExtension('3D')
arcpy.CheckOutExtension('spatial')
#Objective Two: Create Variables
geodatabasePath = "C:\Users\lhartley13\Documents\ArcGIS\EX6\Ex6.gdb"
featureDataPath = "C:\Users\lhartley13\Documents\ArcGIS\EX6\Ex6.gdb\OregonFCs"
otherDataFolder = "C:\Users\lhartley13\Documents\ArcGIS\EX6"
landslidePts = "C:\Users\lhartley13\Documents\ArcGIS\EX6\Ex6.gdb\OregonFCs\LandslidePoints_NW"
precipData = "C:\Users\lhartley13\Documents\ArcGIS\EX6\Ex6.gdb\NW_Oregon_PrecipitationData_800m"
landUseData = "C:\Users\lhartley13\Documents\ArcGIS\EX6\Ex6.gdb\NLCD2011_Oregon_NW"
slopeData = "C:\Users\lhartley13\Documents\ArcGIS\EX5\Ex5\Ex5.gdb\NW_Oregon_Slope_30m"
pointName = os.path.basename(landslidePts).rstrip(os.path.splitext(landslidePts)[1])
pointSelectOutput = os.path.join(geodatabasePath,pointName) +"_Selected"
pointsWithLandUse = os.path.join(geodatabasePath,pointName) +"_LU"
pointsWithSlope = os.path.join(geodatabasePath,pointName) +"_slope"
pointsWithPrecip = os.path.join(geodatabasePath,pointName) +"_Precip"
pointsBuffered=pointSelectOutput + "_Buffer"
bufferedSlopeStatistics = pointsBuffered + "_Slope"
bufferedSlideTypeSummary = pointsBuffered + "_Summary_SlideType"
bufferedLandUseSummary = pointsBuffered +"_Summary_LULC"
bufferedBothSummary = pointsBuffered + "_Summary_Both"
buffered_TabulateLULC = pointsBuffered + "_TabulateLULC"
tabulateLULC_ExcelTable = os.path.join(otherDataFolder,os.path.basename(buffered_TabulateLULC).rstrip(os.path.splitext(buffered_TabulateLULC)[1]))+".xls"
createdFCs = []
createdFCs.append(pointSelectOutput)
createdFCs.append(pointsWithLandUse)
createdFCs.append(pointsWithSlope)
createdFCs.append(pointsWithPrecip)
#field names
uniqueIDfield = "UNIQUE_ID"
bufferDistanceField = "BufferDistance"
slideLengthmFieldName = 'Slide_Length_m'
rasterValueFieldName = "RASTERVALU"
lulcFieldName = "NLCD_2011"
moveClassFieldName = "MOVE_CLASS"
pointSlopeFieldName = 'Slope_Pt'
pointPrecipFieldName = 'PRECIP_mm'
bufferedSlopeFieldName = 'Slope_Buffered_Mean'
calculatedSlopeFieldName = 'Slope_Mean_Calculated'
#Process: Select
print "Selecting!" ,datetime.datetime.now().strftime("%H:%M:%S")
arcpy.Select_analysis(landslidePts,pointSelectOutput,"LENGTH_ft<>0 AND WIDTH_ft<>0AND MOVE_CLASS IN ('Debris Flow','Earth Flow','Flow')")
#Add land use class information to the landslide points only for their specific locations
ExtractValuesToPoints(pointSelectOutput,landUseData,pointsWithLandUse, "NONE","ALL")
arcpy.AlterField_management(pointsWithLandUse,rasterValueFieldName,"LULC_Value","LULC Value")
#Add slope information to the landslide points using interpolation
ExtractMultiValuesToPoints(pointsWithLandUse,[[slopeData,pointSlopeFieldName],[precipData,pointPrecipFieldName]],"BILINEAR")
#Process- Add Field
arcpy.AddField_management(pointsWithLandUse,bufferDistanceField,"FLOAT","","","","","NULLABLE","NON_REQUIRED","")
#Process- Calculate Field
arcpy.CalculateField_management(pointsWithLandUse,bufferDistanceField, "(!LENGTH_ft!+!WIDTH_ft!)/2*0.3048", "PYTHON_9.3","")
#Process- Add Field
arcpy.AddField_management(pointsWithLandUse,slideLengthmFieldName,"FLOAT","","","","","NULLABLE","NON_REQUIRED","")
#Process- Calculate Field
arcpy.CalculateField_management(pointsWithLandUse,slideLengthmFieldName,"!LENGTH_ft!*0.3048","PYTHON_9.3","")
#Process- Buffer (2)
print "Buffering!" ,datetime.datetime.now().strftime("%H:%M:%S")
arcpy.Buffer_analysis(pointsWithLandUse,pointsBuffered,bufferDistanceField,"FULL","ROUND","NONE","","PLANAR")
#Process- Zonal Statistics as Table
arcpy.gp.ZonalStatisticsAsTable_sa(pointsBuffered,uniqueIDfield,slopeData,bufferedSlopeStatistics,"DATA","ALL")
#Process- Join Field
arcpy.JoinField_management(pointsBuffered,uniqueIDfield,bufferedSlopeStatistics,uniqueIDfield,"Min;Max;Range;Mean;Std")
#Alter the joined field name so it's easily indentifiable
arcpy.AlterField_management(pointsBuffered,"MEAN",bufferedSlopeFieldName,bufferedSlopeFieldName)
#Process- Add Field for the search and update cursor
arcpy.AddField_management(pointsBuffered,calculatedSlopeFieldName,"FLOAT","","","","","NULLABLE","NON_REQUIRED","")
#Create and use an update cursor to set the slope value to the buffered value for all that are not null,
#and the point for features that have a null buffered value
print "Setting values based on buffered and slope points!" ,datetime.datetime.now().strftime("%H:%M:%S")
listOfFields=[pointSlopeFieldName,bufferedSlopeFieldName,calculatedSlopeFieldName]
#The update cursor takes a list of field and allows for the values to be changed by specific functions
with arcpy.da.UpdateCursor(pointsBuffered,listOfFields) as cursor:
#it allows for iteration by row so each value will be calculated independently
for row in cursor:
if(row[1]==None):
row[2] = row[0]
cursor.updateRow(row)
else:
row[2] = row[1]
cursor.updateRow(row)
del cursor
#Calculate summary statistics for precip, slope, and slide area, based on movement class
print "Calculating summary statistics for precip, slope, and slide area, based on movement class",datetime.datetime.now().strftime("%H:%M:%S")
arcpy.Statistics_analysis(pointsBuffered,bufferedSlideTypeSummary,[[pointPrecipFieldName,"MEAN"],[pointSlopeFieldName,"MEAN"],[bufferDistanceField,"MEAN"],[slideLengthmFieldName,"MEAN"]],moveClassFieldName)
#Calculate summary statistics for precip, slope, and slide area, based on LULC data
print "Calculating summary statistics for precip, slope, and slide area, based on LULC" ,datetime.datetime.now().strftime("%H:%M:%S")
arcpy.Statistics_analysis(pointsBuffered,bufferedLandUseSummary,[[pointPrecipFieldName,"MEAN"],[pointSlopeFieldName,"MEAN"],[bufferDistanceField,"MEAN"],[slideLengthmFieldName,"MEAN"]],lulcFieldName)
#Calculate summary statistics for precip, slope, and slide area, based on movement class and LULC class
print "Calculating summary statistics for precip, slope, and slide area, based on LULC" ,datetime.datetime.now().strftime("%H:%M:%S")
arcpy.Statistics_analysis(pointsBuffered,bufferedBothSummary,[[pointPrecipFieldName,"MEAN"],[pointSlopeFieldName,"MEAN"],[bufferDistanceField,"MEAN"],[slideLengthmFieldName,"MEAN"]],[lulcFieldName,moveClassFieldName])
#Tabulate area is used to identify how much of each buffer falls within different LULC classes
print "Tabulating the area of each LULC class for each buffered point"
TabulateArea(pointsBuffered,uniqueIDfield,landUseData,lulcFieldName,buffered_TabulateLULC,15)
#Export the table to excel for additional analysis
arcpy.TableToExcel_conversion(buffered_TabulateLULC,tabulateLULC_ExcelTable)
for fc in createdFCs:
if arcpy.Exists(fc):
print fc +"Exists, and will now be deleted"
arcpy.Delete_management(fc)
print "script is complete" ,datetime.datetime.now().strftime("%H:%M:%S")