The goal of this exercise is to create a custom script tool. The tool will generate a swiss hillshade. A swiss hillshade is a slightly modified hillshade. It uses several different filters to generate the terrain. Below are the objectives for this exercise:
- Set the up the script.
- Set up the parameters using the “GetParatermersAsText” option.
- Set local variables.
- Set up a try except loop for the calculation of the hillshade.
- Add the workflow to the loop: divide the DEM, calculate a hillshade, run focal statistics, and add the DEM.
- Set up the except statement to print and error message.
- Create a custom toolbox.
- Use the Add Script Wizzard to add a custom script tool.
- Test your tool.
Methods
The regular parameters were set up to start the script. Simple parameters were set up using GetParameterAsText. Then the loops were set up. A DEM from a previous exercise was created in order to produce the map in the resuts below.
Results
Below is the swiss hillshade created from the script and also the script that was created for this exercise.
#-------------------------------------------------------------------
# Name: Exercise 9- Create a script tool
# Purpose: To create a custom script tool. The tools will generate a swiss hillshade
#
# Author: Laura Hartley, hartlelj
#
# Date: 8/4/2016
#-------------------------------------------------------------------
#Ensure the program is working
print "Script initialized"
#import system modules
import os
import shutil
import time
import datetime
#Import arcpy module
import arcpy
from arcpy import env
from arcpy.sa import*
from arcpy.ddd import *
arcpy.env.overwriteOutput = True
arcpy.CheckOutExtension('3D')
arcpy.CheckOutExtension('spatial')
#Objective two
input_DEM = arcpy.GetParameterAsText(0)
scratchPath = arcpy.GetParameterAsText(1)
fileName = arcpy.GetParameterAsText(2)
filtered_Hillshade = arcpy.GetParameterAsText(3)
aerial_Perspective = arcpy.GetParameterAsText(4)
#Objective three
Z_factor = "1"
print "Creating local variables!" ,datetime.datetime.now().strftime("%H:%M:%S")
#Local variables:
divide_By = "5"
dEM_div_5 = os.path.join(scratchPath,fileName) +"_divHS"
default_Hillshade = os.path.join(scratchPath,fileName) +"_HS"
#Objective four
try:
#Objective five, process: divide
print "dividing input DEM by 5" ,datetime.datetime.now().strftime("%H:%M:%S")
arcpy.Divide_3d(input_DEM,divide_By,dEM_div_5)
#Process: Hillshade
print "Create default hillshade" ,datetime.datetime.now().strftime("%H:%M:%S")
arcpy.HillShade_3d(input_DEM,default_Hillshade,"315","45","NO_SHADOWS",Z_factor)
#Process: Focal statistics
print "Create a median filter for the hillshade effect" ,datetime.datetime.now().strftime("%H:%M:%S")
outFocalStatistics = FocalStatistics(default_Hillshade,NbrRectangle(4,4,"CELL"),"MEDIAN","NODATA")
outFocalStatistics.save(filtered_Hillshade)
#Process: plus
print "Add the divided dem to the default hilshade, creating an aerial perspective" ,datetime.datetime.now().strftime("%H:%M:%S")
arcpy.Plus_3d(dEM_div_5,default_Hillshade,aerial_Perspective)
print "The script is complete"
#Objective six
except:
#report an error message
arcpy.AddError("Something didn't work")
#report any error messages it might have generated
arcpy.AddMessage(arcpy.GetMessages(2))
No comments:
Post a Comment