So yesterday’s post got quite a few views. Someone asked “…but you said you could do this in python”. Since I couldn’t sleep last night I did it in Python.
I’ve had this long running discussion in my head “So What are you good at…” . I can easily list the things I’m not: electrical, plumbing, carpentry, and programming. I like programming. It’s fun. When I started using ArcInfo oh-so-many-years ago you had to know AML or you were going to not have a lot of fun. I hated Visual Basic just enough to not go in that direction when this version of Arc we all know and love came out. I learned perl. Then started this long running infatuation with Python. I use it just enough to be dangerous.
In yesterday’s example you had a watershed you needed to separate. We did it in Model Builder. Today – Python. Instead of an iterator we declare a Search Cursor. I won’t go into all the ins and outs of python formatting or why you type pythong at times when you mean python. I will encourage you to read the ArcGIS help. There are a lot of good examples of how to use python to make your life easier. Python is more than just an ArcGIS tool. You can learn it and then step into ArcGIS and still be lost (or at least I am) trying to learn all the functions and ways to do things. There is no doubt a better way to do what I am going to show you.
You need to separate out the watersheds. Open up a python window….and start typing. I assume at some point you’ve been curious enough to dump a model into python. My phrase of “start typing” is misleading – you have to know what to type. Location of the Python icon is below.
It takes me a while usually to get “in the mood” and I start off making mistakes. Lots of mistakes. The thing you need to do is declare a search cursor. Locate the Field and loop through the results. It’s half art. The nice thing about the python window is you can load files into it and save information out of it. It will also prompt you along through the help window. Eventually I got tired of making mistakes.
I saved what I was doing into a text editor and made changes and load these back into a window. Expressions are a pain. There are too many quotes and double quotes and what not to make it fun. I always forget. I always have to go back and think and look at examples. Eventually – success! Here is what I ended up with (documented for your enjoyment)
import arcpy #declare a cursor rows = arcpy.SearchCursor(fc) #Loop through the cursor for row in rows: #Grab the Field huc = row.HUC_10 #Build an expression Pre = '\"HUC_10\" = ' #Format the variable to get it in Quotes Post = "'%s'" % huc #build the expression expression = Pre + Post arcpy.Select_analysis(fc, "C:\\temp\\watershed_" + huc + ".shp", expression)
So what did we learn here? You can do this in Python if you’re not scared to type. You can save it and re-run it (like model builder) and it’s another tool to get you an answer. It took me a bit longer because I had forgotten about formatting the variables to get quotes and SQL all straightened out. I have no doubt there is a better way to do this. Also – if you know your Arc Python lingo – it takes about the same amount of time as model builder – maybe less. Since I forget some things it took me a bit longer.
Know your data and your tools. Have some fun.