The Art Of Debugging In ArcPy

 Debugging Time...




Today I wanted to share a post about debugging code and error handling. I am going to talk about why debugging is such an important topic in software development. The n we are going to take e a look at handling errors to make incorrect code work. Below are three examples from ArcPy Python scripts in which we look at both debugging errors and handline errors to pass the code for GIS data located inside of an Arc Pro project file. 

This is specifically for ArcPy which is the Python framework for Arc GIS


Syntax Debugging (Spelling can make or break your code, as well as spaces)

Mod2_Script1_Template.py contains two errors and/or exceptions in it. Fix these errors so that the
script runs.

The successful script should print out the names of all fields on the Module2/Data/parks.shp attribute table.

In order to run this script, I needed to open Arc GIS Pro and verify that the data is in my Arc Pro project. When opening arc GIS Pro, I could see the following data...


I can see all 4 data layers visible, so I can run my code. (parks, bike routes, facilities, county)

I debugged the script finding multiple errors, such as spelling, incorrect spaces in the print statement, Camel casing methods, the for loop was backwards where field should be referencing fields, and the for loop needs to print out the layer files without incorrect spaces. 

This is the Script 1 Output I completed. 

 Part 1 script results

o Prints out a list of the field names within park.shp



For Loop Debugging

 My second script is Mod2_Script2_Template.py and it contains several errors/exceptions in it. I need to fix these errors so that the script runs.

Before running this script, I need to verify that the four available shapefiles (.shp) are added to the ArcGIS Project TravisCountyAustinTX.aprx. The successful script should printout the names of all layers in the Project.

In order to fix this script, I did the following...

I fixed the spelling for the aprx, the file path, and I fixed the file path for the file path variable. I made sure arcpy.listMaps() is camel cased. I ensured the concatenating print statement had a plus sign with appropriate spaces. SpatialRef = desc.spatialReference needs to be spelled correctly. The dataset needs to be called in arcpy.Describe. 

The for loop needs a colon at the end for both for loop sections, and the listLayers() needs to reference m in maps. I also put an str aroiund lyr.name to stringify the name of each layer so I can then print out each layer name in the print statement. I made sure aprx was lower cased and correct. 

This is my Script 2 Output...MNo

 Part 2 script results

o Prints out a list of layers for each data frame in the TravisCountyAustinTx.aprx


 Try and except for error handling


For this final script, we are going to try some error handling! So we are not going to fix the errors in our script, but we are going to use a Try and Except statement to bypass the incorrect code to still run our script. So Try and Except are our keywords for this exception...




I first fixed the file path of my script. then I used a try: statement and indented my code that was incorrect such as the project variable, the lyrList variable, and the for loop to bring the layer names and the del statements. 

I then used the except Exception as e: to "except my code" and run the script to section B, which I had to change the file path and the rest of the code ran perfectly because part B falls within the except statement! 

I listed the data layers for my project in a for loop, with the name, data source and spatial reference. 

Script 3 Output

Part 3 script results

o Prints out an error message for Part A and the name, spatial reference, and scale for three data frames in the TravisCountyAustinTx.aprx.



 

 I hope you enjoyed some Debugging tips!

https://pro.arcgis.com/en/pro-app/latest/arcpy/get-started/what-is-arcpy-.htm

https://pro.arcgis.com/en/pro-app/latest/arcpy/get-started/debugging-python-code.htm







Comments

Popular posts from this blog

TIN's vs. DEM's: 3D Elevation Models In Arc GIS Pro

Isarithmic Mapping: Annual Rainfall in Washington

Ward 7 Public Schools in Washington D.C.