Python Select By Location

7319
10
Jump to solution
07-27-2017 07:40 PM
DaveMarkey
New Contributor III

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)

10 Replies
DanPatterson_Retired
MVP Emeritus

if it works manually within arcmap and doesn't as a standalone script, then the data are likely in a different coordinate system.  I would get the Extent object of both participating files and perform a cursory check to see if they at least overlap