Select to view content in your preferred language

How to determine if Select By Location returns nothing

852
2
02-20-2012 12:47 PM
GlennKammerer
Emerging Contributor
Windows XP Pro SP3
ArcView 10.0 SP2
Python 2.6

I want to do a basic field population by looping through a source polygon feature layer and selecting all target features from the target feature layer that are completely within the current polygon. If the selection returns nothing then I want to do nothing, otherwise I'll do a calculate on the target layer. If I'm not mistaken if I perform a field calculation on a layer with nothing selected, then all features get calculated, right? I really don't want to do that. Any ideas? Thanks!
Tags (2)
0 Kudos
2 Replies
StacyRendall1
Frequent Contributor
I think arcpy.GetCount_management(featureClass) ought to do the trick. Just be careful - to actually use it you need to do something like:

int(arcpy.GetCount_management(featureClass).getOutput(0))


Then you can use a conditional to check for change after the selection (if the selection doesn't actually select anything, the count won't change), i.e.:

initialCount = int(arcpy.GetCount_management(featureClass).getOutput(0))
# do selection on featureClass
selectedCount = int(arcpy.GetCount_management(featureClass).getOutput(0))
if initialCount != selectedCount:
    # do stuff
else:
    # no selection, don't do stuff


Let me know how you get on.

Stacy
0 Kudos
GlennKammerer
Emerging Contributor
Thanks Stacy, worked like a charm!
0 Kudos