Learning to code for GIS

Although I have a degree in GIS and work as an analyst, I am not a programmer; I do not have a computer science degree nor did my GIS coursework include programming. [Note: it should have!] It did not take long at all to realize however, that the ability to automate geoprocessing functions, and at least be familiar with the code that builds web applications and databases are key capabilities for someone in this profession. So begins the journey of exploration.

Fortunately, every tool in Arc comes with a Tool Help button that opens a help page that includes detailed syntax and code samples.

Arc Tool

Arc Tool













The Python window example explains how to run the tool one time, but for a single use it may be more efficient to use the graphical interface to launch the tool. But what if I need to run the tool 1,000 times or chain a string of tools together for each iteration?

Python Window Example

Python Window Example





Aha, the stand-alone script examples even provide some insight on how to incorporate the tool into a script that allows for looping, branching, and can be saved for later reuse.

Python Script Example

Python Script Example








But I don’t have a programming background, so how do I learn how to wrap the geoprocessing tool in a script? A good beginner’s guide to python such as “Python Programming for the Absolute Beginner” by Michael Dawson or “Python Scripting for ArcGIS” by Paul Zanbergen teaches the basics of programming along with the specifics of the language, and in the second case even uses geoprocessing examples. Now we are getting somewhere.

On one project, I broke the required steps into several sections and wrote the code as about 5 different scripts. I did this because I was not comfortable with viewing the stack in PythonWin or the various options for controlling the flow of the program during a debugging session (Step Into, Step Over, Step Out),

Debugging with PythonWin

Debugging with PythonWin









and relied on scattering print statements throughout the code to monitor variables. When they were eventually merged into a single script, it was a hairy mess with tons of repetition including variables defined multiple times throughout. What a nightmare to update. Let’s practice using functions to compartmentalize tasks, using the debugging tools, and place parameters and constants at the beginning of the script so that updates are quick and easy. Nice.

Hmm, this script can take a long time to complete. Wonder if there is a way to speed things up? Let’s use the ‘in_memory’ workspace (http://resources.arcgis.com/en/help/main/10.2/index.html#//002w0000005s000000) whenever possible and only read and write from disk when absolutely necessary. While we’re at it, let’s use the built-in ‘profile’ and ‘cProfile’ modules (https://docs.python.org/2/library/profile.html) to get some numbers on how long each part of the script is taking, and look for opportunities to utilize performance best practices such as those found here: https://wiki.python.org/moin/PythonSpeed/PerformanceTips/ . Ok, now we have something that works, saves a ton of labor hours, is maintainable, and performs reasonably efficiently. So what about error-checking, objects, classes, and saving your own modules and packages…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s