This week I had the great fortune of attending the 2014 Esri Federal GIS Conference. GIS professionals from federal, state, and local agencies joined industry experts for two days of workshops, immersion summits, speaking engagements, and hands-on training sessions.
Of course, with over 100 different professional development workshops and seminars to choose from, the real difficulty was prioritizing events. Given that I urged all of us to brush up on our technical skills last week, I thought I should take my own advice. Thus I spent the majority of my time attending skill-based seminars. For the purposes of this post, I’d like to share what I learned at the ‘Intro to Python for GIS’ class.
For those of you looking for ‘big picture’ coverage from thought leaders in industry and government, GovLoop has some fantastic reporting of the GIS conference here, here and here. But for those thinking about dipping their toes in the coding pool, I’d like to share a little bit of what I learned.
ArcPy: Making Life Easier One String at a Time
These days there are two kinds of GIS users: those who use Python and those who don’t-but-desperately-want-to-learn-how. (I happen to fall into the second category.) For the uninitiated, Python is an open source coding language with a large development community and an easy to read syntax. Python was integrated into ArcGIS a few years ago using ArcPy, a site package designed to help users more easily and more quickly perform geographic data analysis, data conversion, data management, and map automation. In short, the primary motivation for using ArcPy is to create automated workflows that eliminate the need to repeatedly perform laborious or tedious steps when performing analyses in GIS. It also allows people to document their work and share with peers for replication. While intimidating at first, learning even some of the basics will make you that much more efficient – and accurate – when performing analyses.
Python Help is Everywhere in ArcGIS
The easiest way to start using Python in GIS is to click any one of the data analysis tools in the toolbox. When you click the ‘help’ button for that tool, you’ll find sample Python code for that tool. Go ahead and cut and paste that code and start experimenting.
Python Basics
If you are still blown away by the code, you aren’t alone. Therefore, a good next step is to become familiar with some basic elements of Python code:
- Comments: Putting a ‘#’ in front of any line of code will tell your program to ignore whatever comes after it when processing. People use this option to include comments, either for themselves or for others who may view their code. They are incredibly useful for keeping track of where you are or as a reminder about what you were thinking at the time. For example:
# Now I am going to perform the next function…here we go!
- Variables: Just like in middle school algebra, a variable in Python is used as placeholder for something else. It can be a number, text, or a true/false value. Here is an example: states = 50. So now every time you type the word ‘states’ in your code, the output will read 50. Variables are incredibly useful not only because they make your code more readable, but they also allow for quick configuration if you need to change your values. You might run an analysis on states in the U.S. one day, and then states in India the next. Using the variable “states” means that you just need to redefine your variable (as opposed to swapping out the specific number in every instance you reference it in your workflow).
- Lists: A list is simply a collection of related values grouped together, separated by commas and enclosed by brackets. For example, a list might look like this: directions = [North, East, South, West]. The variable “directions” now carries with it all the directional values in an ordered in sequence.
- Conditionals: One of the backbones of basic coding in the conditional statement. Basically you are telling it to do something “if” the inputted value meets your predetermined criteria. Python also uses the “else” keyword to perform another action if the condition is not else. For example:
Other Newbie Tips
- Python is case sensitive: If you name your variable Best_variable this is different than best_variable.
- Spaces, Not Tabs: Python uses blank spaces for defining programming blocks. Always use the space bar instead of the tab function.
- Equal sign: The equal sign ‘=’ is reserved for defining variables. When checking for equivalency, use ‘==.’ This is a very common mistake so it is good to be aware of this distinction from the start.
The world of Python is expansive, and I could probably create a dozen long articles just scratching at the surface of this language – and this is before getting into ArcPy. But I’m just a beginner myself, so I’ll end with a series of resources from the real experts.* For those beginning their Python or ArcPy journeys, best of luck! I’ll meet you on the other side.
Resources
- Google: This is the easiest way to find a quick answer to a burning question you may have. More often than not your search bar will save you in a pinch.
- Learn Python the Hard Way: I’m currently going through the exercises. Despite the name, it is a great way to get your feet wet. There are paid videos and a book, but you can learn a lot from the free html tutorials.
- Codecademy: This is an excellent online community for learning coding languages (including Python). Classes are free, and there a lot of fun, interactive tools to keep motivated.
- ArcPy Café: A blog maintained by the guys who created Python for ArcGIS. It is full of recipes for creating ArcGIS workflows using Python.
- StackOverflow: A Q & A website for all of your programming needs. There is a rich archive of questions, and who knows, you might even be able come up with your own coding quandary!
- Python Scripting for ArcGIS (the book): This is published by Esri, so you can take some comfort in the legitimacy of its guidance. It is not the most inexpensive option, but considering how many work hours are lost when we tumble down the programming rabbit-hole (especially in ArcGIS), it is probably worth the money.
* A very special ‘thank you’ is owed to Esri instructor Joel McCune for getting me started on my Python journey. Joel also has his own website and has produced a ton of great content on Python and ArcGIS.
Leave a Reply
You must be logged in to post a comment.