I am trying to find out the counties that don't contain any stores in ArcGIS using Python. Basically, I have a point layer (representing the stores) and a polygon layer (counties). I have managed to write some code to find out the counties that DO contain the stores (it was pretty simple actually). The code is below.
import arcpy
arcpy.env.overwriteOutput = True
path="C:/Users/XARDAS/Documents/ArcGIS/Packages/Romania1000k_9E5B7FEC-6005-4D3A-81EA-E95FAACEF69E/v101/ro1mil.gdb"
arcpy.MakeFeatureLayer_management(path+"/Counties", "Counties_lyr")
arcpy.MakeFeatureLayer_management(path+"/Stores", "Stores_lyr")
arcpy.SelectLayerByAttribute_management("Stores_lyr", "NEW_SELECTION","Type=1")
arcpy.SelectLayerByLocation_management("Counties_lyr","INTERSECT","Stores_lyr",0,"NEW_SELECTION")
So this gives me the counties that have stores but I would like to somehow inverse the intersection for the program to give me the ones that don't have any stores. I have thought about just deleting the selected counties but I don't think that it would be too nice.
-
1Select the ones that do intersect then select layer by attribute with SWITCH_SELECTION help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//…Michael Stimson– Michael Stimson2017年06月06日 23:45:08 +00:00Commented Jun 6, 2017 at 23:45
-
1Cross-posted as stackoverflow.com/q/44400795/820534PolyGeo– PolyGeo ♦2017年06月07日 00:09:47 +00:00Commented Jun 7, 2017 at 0:09
1 Answer 1
After selecting the points using select by location, you need to switch the selection using again select by attribute. I Added variables to easily track each step. You need to do something like this:
import arcpy
arcpy.env.overwriteOutput = True
path="C:/Users/XARDAS/Documents/ArcGIS/Packages/Romania1000k_9E5B7FEC-6005-4D3A-81EA-E95FAACEF69E/v101/ro1mil.gdb"
country_lyr = arcpy.MakeFeatureLayer_management(path+"/Counties", "Counties_lyr")
stores_lyr = arcpy.MakeFeatureLayer_management(path+"/Stores", "Stores_lyr")
att_Select = arcpy.SelectLayerByAttribute_management("Stores_lyr", "NEW_SELECTION","Type=1")
loc_Select = arcpy.SelectLayerByLocation_management("Counties_lyr","INTERSECT","Stores_lyr",0,"NEW_SELECTION")
switch_att_Select = arcpy.SelectLayerByAttribute_management(loc_Select, "SWITCH_SELECTION")