[GIS] Arcpy Select by Location Invert

arcpyselect-by-location

I am trying to complete a select by location, and then Invert my selection, the code runs and does not error, but my selection is not inverted. Not sure what could cause this.

Parks=r"Path"
outputParks=r"Path"
arcpy.MakeFeatureLayer_management(Parks, "Parks")
arcpy.MakeFeatureLayer_management(outputParks, "outputParks")

arcpy.SelectLayerbyLocation_management("Parks", "ARE_IDENTICAL_TO", outputParks, selection_type="NEW_SELECTION", invert_spatial_relationship="INVERT")

enter image description here

Best Answer

If you take a look at the tool help:

NOT_INVERT —The result of the query will not be inverted. This is the default.

INVERT —The result of the query will be inverted. If a selection_type option is used, the inversion occurs before the selection is combined with existing selections.

The arguments are case-sensitive, so you'd need to use the INVERT.

Also, make sure both of your input layers are feature layers which you create using Make Feature Layer GP tool. From the code, outputParks doesn't seem to be a feature layer.

You don't provide the overlap_type parameter because you are missing the double quotes after "ARE_IDENTICAL_TO.

If the error persists, try to use the Select Layer By Location GP tool in ArcMap and use these two map layers you are working with. The tool should do the selection and you should get the results expected. If it worked, then you can go to the Results window > right-click the result created from running this GP tool > Copy As Python Snippet. This is what I got after exporting to a Python snippet a successful selection of features that mismatch between two feature layers.

arcpy.SelectLayerByLocation_management(in_layer="AlternativeRoutes", 
                                       overlap_type="ARE_IDENTICAL_TO", 
                                       select_features="AlternativeRoutes_1", 
                                       search_distance="", 
                                       selection_type="NEW_SELECTION", 
                                       invert_spatial_relationship="INVERT")

This is the complete code that is executed outside of ArcMap and the selection does work.

import arcpy

routes = r"C:\GIS\Temp\ArcGISHomeFolder\scratch.gdb\AlternativeRoutes"
routes1 = r"C:\GIS\Temp\ArcGISHomeFolder\scratch.gdb\AlternativeRoutes_1"
arcpy.MakeFeatureLayer_management(in_features=routes, out_layer="AlternativeRoutes")
arcpy.MakeFeatureLayer_management(in_features=routes1, out_layer="AlternativeRoutes_1")

arcpy.SelectLayerByLocation_management(in_layer="AlternativeRoutes",
                                       overlap_type="ARE_IDENTICAL_TO",
                                       select_features="AlternativeRoutes_1",
                                       search_distance="",
                                       selection_type="NEW_SELECTION",
                                       invert_spatial_relationship="INVERT")

out_diff = r"C:\GIS\Temp\ArcGISHomeFolder\scratch.gdb\diff"
arcpy.CopyFeatures_management("AlternativeRoutes", out_diff)
print arcpy.GetCount_management(out_diff)