I'm trying to write a script that clips all feature classes in fcList
by the successive rows of a clip feature. I'm using arcpy with ArcGIS 10.0
The problem is that I need to verify whether the current fc
of the fcList
overlaps the current row of the clip rows. Here is the code (I commented the lines with my doubts, questions, remarks):
import arcpy, os
arcpy.env.workspace = r'Z:\Documents\SIG\tests\synthese_tocorrect.gdb'
fcList = arcpy.ListFeatureClasses() #the feature class to be clipped by each of the polygon of the clip layer
pochoir = r'Z:\Documents\SIG\tests\decoupe.shp' #the clip layer with multiple polygons
rows = arcpy.SearchCursor(pochoir)
count = 0
outFolder = r'Z:\Documents\SIG\tests\sorties'
for fc in fcList:
for row in rows:
p = row.Shape #I'm trying to get only the geometry of the current row but I thing it's not working
arcpy.MakeFeatureLayer_management(p,"ptmp") #...the output is empty while it should be the first polygon corresponding to the first row of the clip layer.
arcpy.SelectLayerByLocation_management("ptmp", "INTERSECT", fc)
arcpy.CopyFeatures_management("ptmp","ptmpselect")
nbrow = arcpy.GetCount_management("ptmpselect")
if nbrow > 0:
secteur = row.secteur #The integer ID of each polygon of the clip layer
out_poly = (outFolder, fc + "_" + str(secteur) + "_" + string(count))
arcpy.ClipAnalysis(fc, "ptmpselect", out_poly)
arcpy.DeleteManagement("ptmp")
arcpy.DeleteManagement("ptmpselect")
count = count + 1
else:
secteur = row.secteur # I know I repeat myself. Any idea to avoid it ?
out_poly = (outFolder, fc + "_" + str(secteur) + "_" + string(count))
del row, rows, pochoir, count, outFolder, fc, fcList
Best Answer
Move
secteur = row.secteur
outside the if/else statements (beforeif nbrow > 0:
).Move
rows = arcpy.SearchCursor(pochoir)
inside the for loop (afterfor fc in fcList:
)I think that you aren't accessing the geometry properly. Reference Reading Geometries and Working with geometry in Python.
Alternatively, don't mess with the geometry. Make a feature layer for each row by including a
where_clause
to select only that row from the original feature class.You aren't actually counting the rows yet. The
GetCount_management
function needs a couple steps to get the row count as a numeric value.or just the following (although for beginners I recommend keeping things more spread out, like above):
For improved readability, I'd suggest:
and then use outName instead of
fc + "_" + str(secteur) + "_" + string(count)
twice.