What the hell is Python? You may or may not be asking. Well, it is a "high-level programming language" according to wikipedia. Not so long ago, to me it was just some other thing that ESRI was installing on my machine in an already excruciatingly long process. Then I actually tried to use it for some geoprocessing in ArcToolbox, and actually have found it quite a valuable skill to have. I do find the syntax to be cumbersome to use, but my programming started with Visual Basic, so it is much more concise than VB. Anyway, here are my reasons.
- ESRI ArcGIS is using it for a lot of their ArcTools. They seem to be supporting it at least for the next few versions, unlike VBA. (Keep reading as I discuss nonESRI reasons further down)
- You can do a lot with Python outside of the ESRI environment too.
- You don't need ArcGIS in order to do geoprocessing with it.
- You get to use cool libraries like matplotlib to create your charts instead of Excel.
- It is used in a lot of web programming, like MapServer.
Why am I recommending getting started with Python, when I invested time and effort in creating VBA tutorials (as yet unfinished). VBA is still useful as the syntax is similar to VB.NET. But VB.NET has a bigger learning curve. Python will be supported for a while, and you can get started with it while staying within the ArcGIS environment.
How should I get started then? Assuming you are using ArcGIS 9.3. You should install PythonWin as well as that came on the installation disc separately. If you don't have access to that disc, or admin privledges, I'll describe this using something else. Pick a task that you use ArcToolbox for often. Let's use Clipping as an example that should be available to anyone with any license. Add two generic polygon layers to work with. Add ArcToolbox if you don't have it open already, right click and create new toolbox. Right click on your new toolbox and add a new model. Drag the Analysis Tools -> Extract -> clip tool to the model and you should see a square and oval. Double click on Clip and fill out the form as you normally would. Click ok and you should see two blue ovals added. Go to Model --> Export --> To Script --> Python. Call it what you want. Discard the model. Go to where you saved the file with a .py extension. Right-click on it and select edit with IDLE. Two windows open. A note here. Version 9.3 of Arc uses version 2.5 ofPython, and only 2.5. Version9.2 uses only 2.4. You can't use a different version of Python with a different version of Arc. Python will install multiple versions rather than overwrite existing ones. It is possible that you still have 2.4 from arcgis 9.2 still installed. Or another program might have put 2.6 on. Look at the window opened that is called Python Shell and it will tell you what version. If it opened with the wrong version Go to Start --> All programs --> Python 2.5 --> IDLE. When that opens got to File--> Open and open up the Python file that way. It is really important that you get the version right, or else your tool will just crash. Now you have your first bit of code. I suggest you go through it line by line to understand what is happening:
# ---------------------------------------------------------------------------
# clip_example.py
# Created on: Tue Mar 09 2010 12:06:20 PM
# (generated by ArcGIS/ModelBuilder)
# ---------------------------------------------------------------------------
# Import system modules
import sys, string, os, arcgisscripting
# Create the Geoprocessor object
gp = arcgisscripting.create()
# Load required toolboxes...
gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Analysis Tools.tbx")
# Local variables...
statesp020_Clip_shp = "C:\\WorkSpace\\FlowMap\\statesp020_Clip.shp"
statesp020 = "statesp020"
statesp020_Project = "statesp020_Project"
# Process: Clip...
gp.Clip_analysis(statesp020, statesp020_Project, statesp020_Clip_shp, "")
The # is a comment and it is not processed by the compiler. Import loads different libraries. ESRI has a special one called arcgisscripting that you use to create a geoprocessing object (gp). This gives you access to all the ArcTools. Paths to files use either the forward slash / or double backslash \\ but not a single backslash \. This is because strings use this slash as a special character and it would confuse python if you did this. If you change the statesp020 and statesp020_Project (your variable names will be different) to equal their actual path to the file, then, in theory you should just be able to go to Run --> Run Module and it will perform the operation.
Your next task: Figure out how to loop through all the feature classes in a folder and clip them. You already have one important line of code gp.Clip_analysis. You just need to do the looping bit. Follow the isntructions on this page for more information. As always, the ESRI help is quite good and extensive (which does make it difficult to find things).
What if I don't use ESRI, and I use MapInfo or Manifold you are now asking. Well I'm sorry for you. Just kidding, those programs have their merits too, no really they do. :). Unfortunately, you cant use python with those programs, but there is something called GDAL that is an open source geoprocessing library (and more) that you can use instead. Here is a lesson on installing GDAL for python. And Chris Garrard has been generous in posting his course presentations that show step by step how to work with python and GDAL. I recommend even for ESRI users giving that a once over.
I hope this has convinced you and given you a start on using Python with GIS. There are plenty of tutorials that will give you more of the basics of Python Syntax, but I find it better to start with the GIS stuff, which I'm familiar with, and then look up the Python bits I'm less familiar with.