ArcPy – Accessing Geometry with SearchCursor in ArcGIS 10.0

arcgis-10.0arcpy

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

# I know I repeat myself. Any idea to avoid it ?

Move secteur = row.secteur outside the if/else statements (before if nbrow > 0:).


Move rows = arcpy.SearchCursor(pochoir) inside the for loop (after for fc in fcList:)


I think that you aren't accessing the geometry properly. Reference Reading Geometries and Working with geometry in Python.

# tells you the shapeFieldName of pochoir
shapeName = arcpy.Describe(pochoir).shapeFieldName

# actually stores the geometry somewhere
p = row.getValue(shapeName)

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.

for row in rows:
    id = row.secteur
    where_clause = "secteur = '{}'".format(id)
    arcpy.MakeFeatureLayer_management(pochoir, "ptmp", where_clause)

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.

result = arcpy.GetCount_management("ptmpselect")
nbrow= int(result.getOutput(0))

if nbrow > 0: ...

or just the following (although for beginners I recommend keeping things more spread out, like above):

result = arcpy.GetCount_management("ptmpselect")

if int(result.getOutput(0)) > 0: ...

For improved readability, I'd suggest:

outName = "{0}_{1}_{2}".format(fc, secteur, count)

and then use outName instead of fc + "_" + str(secteur) + "_" + string(count) twice.

Related Question