This section will have you use and extend an existing "class". We'll learn more about classes in the near future.
The purpose of this module is to provide an adapter between our scripts and ArcGIS. When ArcGIS changes, we can just change the adapter functions and we don't have to change all of our scripts.
The module below contains Python code to interface with ArcMap. Copy the class below into Wing and save it as a file named "MyArcModule".
################################################################################### # ArcGIS Interface Module # This module includes classes to interface with ArcGIS. # The functions provide insulation from ArcGIS changes and provides friendlier error # messages. # Author: Jim Graham # Date: 4th of November, 2011, Modified: March 5th, 2017 ################################################################################### import arcpy arcpy.CheckOutExtension("Spatial") # make sure the spatial analyst is checked out arcpy.env.overwriteOutput=True # allow overwrites def Aspect(InputFilePath,OutputFilePath): # perform aspect on a DEM and return it """ Performs an aspect calculation using ArcGIS Inputs: - InputFilePath: The full path to the file to process - OutputFilePath: Full path to the file to save the results to """ try: TheRaster=arcpy.sa.Aspect(InputFilePath) # Call ArcGIS to perform the aspect transform TheRaster.save(OutputFilePath) except Exception, err: # an error occurred (probably in arcGIS) raise RuntimeError("** Error: Aspect Failed ("+str(err)+")")
Notice that the module imports "arcpy" so we don't have to import it in our other code. It also checks out the Spatial extension and allows overwrites. Now we never have to worry about them again, we just include the module in our main scripts and it is taken care of. There is also a function to call ArcPy that takes an input and output file path. This will make all the function calls similar. I've added a message to the error so we know the error occurred in the aspect function.
To use the Module, we just add it to our main script. I'm going to put it into a try/except block in case ArcGIS is not installed and we get an error.
import os try: import MyArcModule # set the input to the path where the original files are InputPath="C:/Temp/Inputs/" OutputPath="C:/Temp/Outputs/" # get a list of all the files in the input folder TheList=os.listdir(InputPath) for TheFile in TheList: # break up the file name into pieces based on periods TheFileName, TheFileExtension = os.path.splitext(TheFile) # create the full path to the file InputFilePath=InputPath+TheFileName+TheFileExtension # determine if this is a file (not a directory), has just a name and extension, and the extension is "img" if (TheFileExtension==".img"): print("Found a file to process:"+TheFileName) # put additional processing steps here OutputFilePath=OutputPath+"Aspect.img" MyArcModule.Aspect(InputFilePath,OutputFilePath) # let the user know which files we've completed print("Done with Aspect for "+TheFileName) # Let everyone know we are done with the slope except Exception, err: # an error occurred print(str(err)) print("Completed Script") # Let everyone know we are done with all processing
Notice that all we have to do is import our module. Then we can call the functions in the module without having to worry about the details of the ArcGIS interface. We also have to do a little more work to get the error string back from our class but this is the standard method to get a message from an exception in Python.
The script above is one model for a complete script to process GIS data using ArcGIS. You can vary from this script as you desire but it's important to think about the design goals as you do.
© Copyright 2018 HSU - All rights reserved.