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
Answer
Creating a layer file for your clip shapefile and using selection would be how I would accomplish this task. Remember that when a geoprocessing tool is used it will only perform the process on selected features. In the below script, I first create a feature layer from the clip shapefile, to allow for selection. I then iterate through each feature class to be clipped. For each iteration, I iterate through each feature in the clip layer file. I select each feature based on OID, and then perform my clip. The output naming convention is different than in your script (out_poly variable), but you can tweak this to your liking. It has been a while since I've used the older version of the search cursor, so hopefully I got that code correct.
Code:
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
outFolder = r'Z:\Documents\SIG\tests\sorties'
fcList = arcpy.ListFeatureClasses()
arcpy.MakeFeatureLayer_management (pochoir, "pochoirlyr") #Create layer to allow for selection
OIDField = arcpy.Describe ("pochoirlyr").OIDFieldName #Get name of OID field
for fc in fcList:
arcpy.MakeFeatureLayer_management (os.path.join (arcpy.env.workspace, fc), "lyr")
pocursor = arcpy.SearchCursor ("pochoirlyr")
for porow in pocursor:
sql = '"' + OIDField + '" = ' + str (row.getValue(OIDField)) #SQL to select one feature
arcpy.SelectLayerByAttribute_management ("pochoirlyr", "", sql) #Select polygon feature by OID
arcpy.SelectLayerByLocation_management ("lyr", "", "pochoirlyr") #Select feature layer to be clipped by selected clip feature
if arcpy.Describe ("lyr").FIDSet: #Check for selection
out_poly = (outFolder, fc + "_" + str (row.getValue(OIDField))) #Output feature class
arcpy.ClipAnalysis(fc, "pochoirlyr", out_poly)
del porow
del pocursor
arcpy.Delete_management ("lyr")
arcpy.SelectLayerByAttribute_management ("pochoirlyr", "CLEAR_SELECTION")
I hope this helps!
EDIT: Edited so that clip is only performed if features overlap.
No comments:
Post a Comment