import arcpy,sys sdeConn = r"Database Connections\\Test.sde" muniLoc = "Municipalities" luLoc = "Land_Use" tempLoc = "tempMuniLuRatio" lu_lyr = "lu_lyr" arcpy.env.workspace = sdeConn try: print "MakeFeatureLayer_management lu_lyr" arcpy.MakeFeatureLayer_management(luLoc, lu_lyr) prematchcount = int(arcpy.GetCount_management(lu_lyr).getOutput(0)) print "MakeFeatureLayer_management muni_lyr" #arcpy.MakeFeatureLayer_management(muniLoc, "muni_lyr") #print "SelectLayerByLocation_management NEW_SELECTION" #arcpy.SelectLayerByLocation_management(lu_lyr, "COMPLETELY_CONTAINS",muniLoc,"","NEW_SELECTION") print "SelectLayerByLocation_management SWITCH_SELECTION" arcpy.SelectLayerByLocation_management(lu_lyr, "COMPLETELY_CONTAINS",muniLoc,"","SWITCH_SELECTION") postmatchcount = int(arcpy.GetCount_management(lu_lyr).getOutput(0)) if prematchcount == postmatchcount: print "SelectLayerByLocation_management DID NOT WORK" else: print "SelectLayerByLocation_management LOOKS GOOD" if arcpy.Exists(tempLoc): print "Delete_management " arcpy.Delete_management(tempLoc) print "CopyFeatures_management " arcpy.CopyFeatures_management(lu_lyr,tempLoc) except Exception: e = sys.exc_info()[1] print(e.args[0])
Result is just same as lu_lyr even after SWICH_SELECTION option.
Where should I take a look to make this work?
Thanks,
Jack
Solved! Go to Solution.
Is there a selection in your ArcMap session? It looks like you once made a selection and then commented it out.
If there is no prior selection, you are running up against a couple of quirks of GetCount and SelectLayerByLocation. 1.) GetCount returns the count of all features if there is no selection. Your variable "prematchcount" returns a count of all features because there is no selection. Later, "postmatchcount" returns a count of all features, because all features are selected. 2.) When you use "SWITCH_SELECTION", the second and third parameters are ignored, so there is no spatial component to the tool whatsoever at that point.
Hi Gyeyoung.
I've been checking the syntax on Esri help.
I see that the 4th parameter "search_distance" is optional, but is a numeric parameter. So, what I think it is doing, is assume that you don't give an optional numeric parameter, and it reads the empty string as the 5th parameter. With other words, try this:
arcpy.SelectLayerByLocation_management(lu_lyr, "COMPLETELY_CONTAINS",muniLoc,0,"SWITCH_SELECTION")
...giving zero instead of empty strig "". I think it will take it as the numeric 4th parameter, and after it will execute your switch selection.
Hope it helps. Good luck!
Luis Pascual
Is there a selection in your ArcMap session? It looks like you once made a selection and then commented it out.
If there is no prior selection, you are running up against a couple of quirks of GetCount and SelectLayerByLocation. 1.) GetCount returns the count of all features if there is no selection. Your variable "prematchcount" returns a count of all features because there is no selection. Later, "postmatchcount" returns a count of all features, because all features are selected. 2.) When you use "SWITCH_SELECTION", the second and third parameters are ignored, so there is no spatial component to the tool whatsoever at that point.
Darren is right on with SWITCH_SELECTION. Maybe you really meant "REMOVE_FROM_SELECTION"?
To avoid confusion, I always use SWITCH_SELECTION in my code with SelectLayerByAttribute (simpler syntax):
arcpy.SelectLayerByAttribute(lyr, "SWITCH_SELECTION")
And there is a whole bunch of indentations after the ifs that need to be sorted out.