1

I am trying to test if each feature of a feature class list intersect each feature of a shapefile (clip layer). I'm working with arcpy in ArcGIS 10.0

The arcgis desktop help says that if the feature comes from a geodatabase, all the features will be copied but if it's a layer, only selected will be copied (http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001700000035000000).

In my code, I'm working with a feature class list from a geodatabase.In order to copy only selected, I first convert each feature class to a feature layer :

for fc in fcList:
    for row in rows:
        where_clause = "secteur = '{0}'".format(secteur)
        arcpy.MakeFeatureLayer_management(pochoir, "ptmp", where_clause)#pochoir is #the clip feature. For each row I make a tmp corresponding to the geometry that I'll use #after.
        arcpy.MakeFeatureLayer_management(fc, "toselect")
        arcpy.SelectLayerByLocation_management("toselect", "INTERSECT", "ptmp")
        arcpy.CopyFeatures_management("toselect", "selected") ## Here is the problem : #"selected" should be empty when not intersecting with "ptmp" but the whole "toselect" (so #the whole fc) is copied even if not intersecting. 
        nbrow = arcpy.GetCount_management("selected")
        outName = "{0}_{1}_{2}".format(fc, secteur, count)

        if nbrow > 0: ## So all the layers goes in the if even if they do not intersect.
            do something
        else:
            do something else

1 Answer 1

2

What you first want to do is check for a selection. This can be done by checking if arcpy.Describe (layer).FIDSet returns anything. FIDSet is a property of a layer describe object. It returns OIDs of selected features, and nothing if there are no selected features.

Try this after you make your selection:

if arcpy.Describe ("toselect").FIDSet:
    arcpy.CopyFeatures_management("toselect", "selected")

If you want an empty feature class returned when there is no selection, you'll have to code an else: make feature class, or something along those lines.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.