AnsweredAssumed Answered

Python Select By Location

Question asked by dmarkey on Jul 27, 2017
Latest reply on Aug 24, 2017 by Dan_Patterson

I am attempting to do a select by location in python. For some reason no records are being selected. There are three polygons in the Cadastre layer and the suburb layer is for the whole state. I am wanting to select the suburb polygon that intersects the Cadastre layer. When I do this with ArcMap menu options it produces the expected result. When done via python either as a script tool or in the python window, no records are being selected. Do both layers need to be feature layers? I initially only produced a feature layer for the suburb layer. Both being feature layers made no difference. My AddMessage is temporary to test if the selection is being done. I will be using the selected record to update a text element within my Layout window. My script is shown below.

 

# Import arcpy module
import arcpy
from arcpy import env
import sys
import os

 

mxd = arcpy.mapping.MapDocument("Current")
df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]

arcpy.MakeFeatureLayer_management("Cadastre","CadastreLayer")
for lyr in arcpy.mapping.ListLayers(mxd,"Suburb",df):
    arcpy.MakeFeatureLayer_management(lyr, "Layer")

    arcpy.SelectLayerByLocation_management("Layer","intersect","CadastreLayer")

    with arcpy.da.SearchCursor("Suburb",["NAME"]) as cursor:
        for row in cursor:

           arcpy.AddMessage("Suburb is: ", row)

Outcomes