iterating through a shapefile

6631
5
04-04-2012 12:01 PM
MikeKnowles
New Contributor
I'm new to geoprocessing with Python (but with a basic knowledge of Python) and have been trying to loop through a shapefile to perform analysis on each polygon in the shapefile.  I have seen references to looping through directories. etc. but haven't been able to translate them to working with polygons within a shapefile.

Thanks,
Mike

Using ArcGIS 9.3 on XP.
Tags (2)
0 Kudos
5 Replies
DarrenWiens2
MVP Honored Contributor
Check out cursors. Also, reading and writing geometries.
0 Kudos
Jasonvan_Warmerdam
New Contributor
Hi Mike,

Well if you're new to Python and arcpy it may seem confusing but I'll give you some code to work with. Shapefiles are not just a single file, so you need to use the arcpy module to run some of the commands. Here's a basic program that takes all the shapefiles in a directory and saves them to a list, it then iterates over every shapefile in the directory doing a print statement to the geoprocessor. Of course you could modify the print statement to do any type of geoprocessing. I haven't tried this code, but in theory it should work.

# Import your modules, arcpy is assuming you are running ArcGIS 10x
import arcpy, sys, os, fileinput, glob, string
from arcpy import env

# Get a directory as an argument from the user. This directory is where your shapefiles are
workingDirectory = sys.argv[1]

# This sets your workspace as the user defined working directory
env.workspace = WorkingDirectory

#This establishes the variable featureClass as a list of shapefiles using the arcpy command ListFeatureClasses
featureClass = arcpy.ListFeatureClasses()

# For loop to iterate over every shapefile in the workingDirectory that is referenced as the variable featureClass
for shapefile in featureClass:
     arcpy.AddMessage(shapefile)

0 Kudos
MikeKnowles
New Contributor
I've written the following code based on "Writing Geoprocessing Scripts with ArcGIS" but am getting an error which I can't figure out. What is Python storing the data as (string, list, etc)? Has there been changes to how ArcGIS interprets the code?

Using Python 2.5.1; ArcGIS 9.3; Windows XP.


import arcgisscripting

GP = arcgisscripting.create(9.3)
GP.overwriteoutput = 1

GP.workspace = "C:\\USER\\FLATHER\\great_plains_corridor\\experimental"

rows = GP.SearchCursor("C:\\USER\\FLATHER\\great_plains_corridor\\experimental\\polys_dd.shp")
row = rows.reset()

row = rows.Next()

for row in rows: # just in case, the following two lines are indented in the code but didn't appear indented in the preview
print row(0)
row = rows.Next()

The error is:
Traceback (most recent call last):
File "C:\Python25\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript
exec codeObject in __main__.__dict__
File "C:\USER\FLATHER\great_plains_corridor\experimental\script3.py", line 23, in <module>
for row in rows:
TypeError: 'geoprocessing cursor object' object is not iterable
0 Kudos
DarrenWiens2
MVP Honored Contributor
A cursor "returns an enumeration object that will, in turn, hand out row objects". I'm not quite sure why (I started with Python in ArcGIS 10), but in 9.3 I think it likes "while row:" rather than "for row in rows:".
0 Kudos
MikeKnowles
New Contributor
In addition to the while row: change, I needed to specify the fields I wanted to print.

while row:
    print row.POLYS_DD_, row.polys_dd_i
    row = rows.Next()
0 Kudos