Pages

Friday, July 29, 2016

Exercise 9: Create a Script Tool

The Objective

The goal of this exercise was to write a script that can be saved and used as a tool. The function of this tool is that it creates a Swiss Hillshade for raster datasets.

The Process

The first step of writing this script is to import the usual modules and extensions. Then, the "GetParametersAsText" function is used to set up the variables for the tool. Next, the tool's processing workflow is established using a try except statement. The script is then run and placed into a toolbox. Lastly, the Add Script Wizard is ran through to add the final touches to the tool's functionality and user interface (ie. help descriptions, credits, etc.)

The Results

#-------------------------------------------------------------------------------
# Name:        Ex9
# Purpose:     Create a script tool
#
# Author:      Zach Miller
#
# Date:        7/28/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')

input_DEM = arcpy.GetParameterAsText(0)

scratchPath = arcpy.GetParameterAsText(1)
fileName = arcpy.GetParameterAsText(2)
filtered_Hillshade = arcpy.GetParameterAsText(3)
aerial_Perspective = arcpy.GetParameterAsText(4)

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"

try:
    # 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 hillshade, 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"

except:
    #report an error message
    arcpy.AddError("Something didn't work")

    #report any error messages it might have generated

    arcpy.AddMessage(arcpy.GetMessages(2))


Sources

All sources provided by Dr. Christina Hupy.

No comments:

Post a Comment