How do I clip a shapefile to a single-part shapefile using ArcGIS 9.3.1 and Python 2.5?
Example: I have a set of lines (lines.shp) I am trying to clip to each record in a shapefile called Grids.shp In grids.shp there is a field called [NAME] with a record for each grid; grid-1, grid-2, ...
The grids represent Map extents, and the lines will be roads/streams/other base layers (could not post base layers publicly so have provided Lines.shp as an example).
I have been trying to use the SearchCursor method but have some trouble defining objects and whatnot as I am new to Python. I got this same process to work in V10 but the V10 syntax is way different/simpler, especially for cursors and uses Python 2.6
EDIT : Clip_wCursor_v2a.py ESRI Forum with python script attached.
# ---------------------------------------------------------------------------
# Clip_wCursor_v2.py
# Created for ArcGIS 9.3.1
# (clip generated by ArcGIS/ModelBuilder)
# Attempted cursor mods by a newb, Donovan
# ---------------------------------------------------------------------------
# Import system modules
import sys
import string
import os
import arcgisscripting
# Create the Geoprocessor object
gp = arcgisscripting.create(9.3)
# Load required toolboxes...
gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Analysis Tools.tbx")
# Designate a workspace
gp.workspace = r"C:\gis\data"
# Assign variables...
clipto = "grids.shp" # grids to clip to
clipit = "lines.shp" # features to be clipped
# Cursor time...
rows = gp.SearchCursor(grids)
row = rows.Next()
feat = row.Shape # used as the clip_features in the Clip_analysis
while row:
n = str(row.NAME) # assign a variable for the processing message based on a field
print "clipping to: " +n # tells you what grid was clipped
# Clip_analysis <in_features> <clip_features> <out_feature_class> {cluster_tolerance}
# output format, grid_1_c.shp, grid_2_c.shp, etc...
gp.Clip_analysis(clipit, feat, str(row.NAME)+"_c"+".shp", "") #update str if necessary
row=rows.next()
# reset the array...
del rows
del gp
print "...fin..."
print "no error checking happened, examine your workspace..."
appreciate all the input,
I could only mark one as the correct answer, and i considered the fact that my original error was because of the lack of a while
and improper use of row
in the clip function, although the makefeaturelayer method is definitely valid!
row
object problem.row.Shape
reference would update along with the row loop, but I guess it does. I work in Arc10, so my cursors are iterable (while loops give me headaches :) ).