Taking advantage of Python in ArcGIS

May 12, 2011 by  
Filed under Programming

Python is the next big programming language for ArcGIS. With the addition of the ArcPy module in ArcGIS 10 there is so much more that can be done with Python. In previous version of ArcGIS VBA made is easy to customize the interface and basic processes. At the release of ArcGIS 10.1 VBA will no longer be supported.

So now that Python has been proven as a great tool, who do you begin.  Sure you can start with www.python.org but the is so much information is on the site you are not sure where to start.  Well, there is a endless source for finding code samples.  Search the ArcGIS Help Library to find the tool you would like to use.  Then scroll to near the bottom and there is a code sample to use in the Python Window or as a stand-alone script for that particular tool.

Another  method that saves a lot of time is to use ModelBuilder and create a model for the desired process.  Once you get the model working then export the model as a script (Model > Export > Script).  The output of this export provides a great starting point for expanding on the python code to get the task accomplished.

Here is a quick example from the Buffer tool help document:

Python Widnow Script
import arcpy
arcpy.env.workspace = "C:/data"
arcpy.Buffer_analysis("roads", "C:/output/majorrdsBuffered" "100 Feet", "FULL", "ROUND", "LIST", "Distance")

Stand-alone Script
# Name: Buffer.py
# Description: Find areas of suitable vegetation which exclude areas heavily impacted by major roads
# Author: ESRI

# import system modules
import arcpy
from arcpy import env

# Set environment settings
env.workspace = “C:/data/Habitat_Analysis.gdb”

# Select suitable vegetation patches from all vegetation
veg = “vegtype”
suitableVeg = “C:/output/Output.gdb/suitable_vegetation”
whereClause = “HABITAT = 1″
arcpy.Select_analysis(veg, suitableVeg, whereClause)

# Buffer areas of impact around major roads
roads = “majorrds”
roadsBuffer = “C:/output/Output.gdb/buffer_output”
distanceField = “Distance”
sideType = “FULL”
endType = “ROUND”
dissolveType = “LIST”
dissolveField = “Distance”
arcpy.Buffer_analysis(roads, roadsBuffer, distanceField, sideType, endType, dissolveType, dissolveField)

# Erase areas of impact around major roads from the suitable vegetation patches
eraseOutput = “C:/output/Output.gdb/suitable_vegetation_minus_roads”
xyTol = “1 Meters”
arcpy.Erase_analysis(suitableVeg, roadsBuffer, eraseOutput, xyTol)

Comments

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!