Python: How to Start Scripting in ArcGIS


Python in ArcGIS is intimidating, so I’m starting simple. This is a quick walk through of how to load a script for use and what a basic Python script looks like.

How To Load It

Python scripts are accessible from Toolboxes. When you go into the Catalog, you can create your own custom Toolbox and stash a script like so:




Parameters let your users feed things into the Python—shapefiles, layers, or simplier inputs like strings. Why would you need this? Well, say you wanted to run a set of procedures on a different set of shapefiles every couple of days. Maybe every time, you need to buffer it a certain distance, convert both the original shapes and the buffered shapes into KMZs, append the original shapes into a geodatabase, and modify its attribute table to show when all this was completed.

You can script that with a shapefile parameter to avoid having to click through the buffering, KMZ conversion, and append tools every time.

The Python

So here’s a breakdown of the simplest script I could think of—a less complicated version of what’s described above.

Every time you start off, you’ll need to have Python import arcpy and then specify which map you want to work with. You can pop that MapDocument into a variable with the filepath for easy reference throughout the script. And if you have the document open, you can just reference it like this:

import arcpy

Within each map, there are data frames which hold collections of layers that are dispayed together. If you want do something like add a layer, you’ll need to tell ArcGIS which data frame to work with. ListDataFrames will return a list object with all your frames—you can reference those by index number. It’s 0 based, so if you only have the one data frame, you can pull it into a variable like this:

dataFrame = arcpy.mapping.ListDataFrames(MapDocumentName, "*")[0]

Alright, now that that’s set up, we can move on to actually creating and adding a layer. Now you’ll note that above, I set a parameter called Layer. Parameters are also 0 based, so you can use GetParameterAsText(0) to reference it if you only have one. This pulls the layer the user specified into a variable called workarea.

workarea = arcpy.GetParameterAsText(0)

Once that’s done, you can run a tool on that layer—for instance, the Buffer Analysis:

arcpy.Buffer_analysis(In_PutLayer, Out_Put_FilePath , "200 feet", "FULL", "ROUND", "ALL")

You can adjust the buffer distance and disolve options.  But to actually add that to your map, you’ll need a few additional lines:

newlayer = arcpy.mapping.Layer(FilePath)

Once you’ve got everything in, you have to save that file with a .py extension so that ArcGIS will recognize it when you add it to your toolbox. And that’s it.

A parting tip: Be mindful of your variables. These scripts can get really complicated really fast——easy, descriptive names help if you need to go back and fix something later.

An Example Code

The Copy/Paste Version

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
dataFrame = arcpy.mapping.ListDataFrames(mxd, "*")[0]
workarea = arcpy.GetParameterAsText(0)
arcpy.Buffer_analysis(workarea, r"<em>c:\\Users\\Desktop\\Temp_buffer.shp</em>" , "200 feet", "FULL", "ROUND", "ALL")
newlayer = arcpy.mapping.Layer(r"<em>c:\Users\Desktop\Temp_buffer.shp</em>")



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: