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)

Add VBA Code to ArcMap

November 22, 2008 by  
Filed under ArcGIS, GIS Tips, Programming

Once you find a some quality VBA code for your GIS application, you can begin thinking how to put the code to use. Check out my VBA Code Resources post for great resources.

So now that you have that block of code or *.dll file, what do you do with it?  Well, here are two quick videos to show how to implement VBA code.

Add VBA Code to Toolbar Button


Add DLL File

Note: Videos are broadcast through youtube.com.  Some browsers may block this content.

Related Blogs

Intro to Programming with ArcObjects

ArcObjects Tutorial I-Create Custom Command

Start Programming in GIS

Related Books

Start Programming in GIS

November 10, 2008 by  
Filed under ArcGIS, GIS Tips, Programming

GIS in and of itself is a very technical system.  Just think about the visual part of a map and then add in the data that lies behind the visualization.  Then on top of everything, add the ability to do extensive analysis with the data.  Each of these parts are held together with a Graphic User Interface such as ArcGIS, MapInfo, or GoogleEarth.  While already being overwhelmed by the data alone, think about the programming that goes into these GUI’s.  You may tell yourself that programming is an area that you plan to stay far away from; however. I believe that there are extremely great benefits to knowing a little bit about programming.

In order to get your feet wet in GIS programming, I would suggest learning a little bit about Visual Basic for Applications. VBA is an event driven form of Visual Basic.  It usually relies on applications to serve as its host as it does not work independently.  Microsoft Software is the main housing unit for VBA, but it does come with other software packages such as AutoCAD, WordPerfect, and ArcGIS. VBA allows the user to perform repetitive and/or extensive tasks in a simple to use format.  These formats can include but are not limited to a Button, Tool, Form, and Macro.

To Begin, find where the Visual Basic for Applications program is located. In ArcMap or ArcCatalog click Tools -> Marcos -> Visual Basic Editor… (This is basic for all applications with VBA). The shortcut is Atl+F11. This will open Microsoft Visual Basic in a new window.

Start by exploring the various areas presented. Familiarize yourself with the Menu bar. Many of the options will be completely foreign, but you will learn how to apply them to programming eventually.

Next, you should see a Project Explorer Window (If you do not see this window, click View -> Project Explorer). Read more

VBA Code Resources

November 10, 2008 by  
Filed under ArcGIS, GIS Tips, Programming, Websites

Finding resources for programming with VBA in GIS is a challenging task.  I have search high and low, and still I have found very little resources.  Here is what I have found so far:

1. ArcScripts

This the best place to find code.  Since it is maintained by ESRI, there is no larger collected anywhere online.  The GIS community uplaods various scripts and the site puts them in an easy to search format.  Many programming languages are available.

Here are a few of my favorite:

Read more