Water Snake Programming: A simple technical report
By Casey J. McLaughlin
Sometimes GIS development is about making maps easier for other people. ArcGIS’s Python Add-ins functionality is a really easy way of automating map tasks and making mapping easier for end-users. My first add-in was really simple but also reminded me why I’m only a part-time programmer…..
Many years ago I built a map document for helping TMDL permit reviewers see what was going on in the watershed. Users work in a database and when ready, open a document that displays the watershed. There are 147 HUCs approximating watersheds in Region 7. I wrote a VBA script that fires when the document is opened which goes and grabs the HUC_ID from a database generated text file. The script then sets the definition query and zooms to the HUC. When the map opens, the user can start working right in the area of interest. It’s a fairly simple routine that saves users from a few simple steps.
All good things require updates and this summer the application needed a minor update – of course we also added 50+ data layers so maybe it wasn’t minor! Besides data, the script I wrote 5+ years ago also needed revision, especially because ArcGIS has discontinued support for VBA.
Yes, ArcGIS Desktop 10 does support Microsoft VBA. However ArcGIS 10 is the last version with VBA support, so we encourage you to start the migration process. Python is an integral part of ArcGIS Desktop for automating tasks and the new add-in capabilities allow developers to easily create and deploy ArcMap customizations
ESRI recommends migrating VBA code to Python. I think this is fantastic – Python is an open source, easy to learn, widely supported, multi-use, and generally fun language. When I first started writing Python, I spent an hour writing a program that had taken me a week in VBA (maybe a slight exaggeration but back then I felt programming in VBA was like getting turned into a newt…and yes, I got better).
ArcGIS now supports a really easy interface for creating Python add-ins. The new method is really easy but I haven’t been programming in ArcGIS for awhile so I watched the 60 minute training video and immediately started programming (yes, I read the docs, honestly). The program I wrote performs the same function; when opening a TMDL document, find the HUC_ID and zoom to it.
The python script:
def openDocument(self) mxd = arcpy.mapping.MapDocument(“current”) mxd_name = os.path.splitext(os.path.basename(mxd.filePath)) ##Only try this if it is a TMDL document or in the TMDL directory if mxd_name.find("TMDL") == 0: #Open the IFO file and parse out the HUC ID ifo = mxd.filePath.rstrip(mxd.filePath[-3:]).upper() + "ifo" #check if it exists and then parse it out openFile = open(ifo, 'r') huc = openFile.readline().split(",") ## Grabs the 3rd comma delimited element from the 1st line df = mxd.activeDataFrame lyr = arcpy.mapping.ListLayers(mxd, "8 Digit HUC", df) ## Should look at source? lyr.definitionQuery = u"HUC_ID ='" + huc.strip() + "'" recordCount = len(arcpy.Describe(lyr).fidset.split(";")) ##Get number of features, if it is 1, zoom, otherwise it should error if recordCount == 1: df.extent = lyr.getExtent() arcpy.RefreshActiveView() else: pythonaddins.MessageBox(lyr.name + " found " + str(recordCount) + " records for HUC " + str(huc) ,"Error Selecting HUC", 0) return
I was happy because I tested the script in the Python window in ArcGIS and it worked! I could make the startUp() function fire, but since none of the data was loaded, my function needed to be in openDocument(). Again, smooth sailing until I realized I couldn’t make the openDocument() function fire….or any function that wasn’t the startUp()! I didn’t find anything helpful during an internet search so I took a walk around the lake and a simple thought occurred to me about extensions. They need to be clicked ON to work! This was my gotcha moment – that time when I realized one little detail that I had overlooked.
I’m all done writing my extension and it works well-enough (suggestions are welcome). Now the users can zoom directly to their area of interest. Of course, I think it’d work better as a web map, but I’ll leave that for a future request, perhaps in 5 more years.
Casey McLaughlin is a first generation Geospatial Enthusiast who has worked with EPA since 2003 as a contractor and now as the Regional GIS Lead. He currently holds the rank of #1 GISer in EPA Region 7′s Environmental Services Division.
The views expressed here are intended to explain EPA policy. They do not change anyone's rights or obligations. You may share this post. However, please do not change the title or the content, or remove EPA’s identity as the author. If you do make substantive changes, please do not attribute the edited title or content to EPA or the author.
EPA's official web site is www.epa.gov. Some links on this page may redirect users from the EPA website to specific content on a non-EPA, third-party site. In doing so, EPA is directing you only to the specific content referenced at the time of publication, not to any other content that may appear on the same webpage or elsewhere on the third-party site, or be added at a later date.
EPA is providing this link for informational purposes only. EPA cannot attest to the accuracy of non-EPA information provided by any third-party sites or any other linked site. EPA does not endorse any non-government websites, companies, internet applications or any policies or information expressed therein.