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)
Solved! Go to Solution.
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
Is everything in the same coordinate system? and your indentation is wrong... it could be a copy/paste problem but you need to use syntax highlighting Code Formatting.... the basics++
Hi Dan.
The indentation I have in the script is correct - I have added the required indent to my initial post. It is the actual select by location that is not working as expected. No errors are being produced. All layers have the same projection.
As Joshua suggests might be the issue... but to confirm, do it manually and copy the code snippet from the
Results Window ...to see the proper syntax and reference to the appropriate objects
In your search cursor, try "Layer" instead of "Suburb".
What happens when you do
len(arcpy.mapping.ListLayers(mxd,"Suburb",df))
Does this list layers actually find the layer object you want to interact with?
Also, lyr is a layer object which I don't think is a valid input to MakeFeatureLayer. Try lyr.name.
Good luck.
Micah
Layer, i.e., feature layer objects, are valid input to Make Feature Layer—Help | ArcGIS Desktop: The input feature class or layer from which to make the new layer.
In this case, I think the OP is simply referring to the old/original layer after creating a new layer and selecting against the new layer:
>>> fc = # path to feature class
>>> lyr1 = arcpy.MakeFeatureLayer_management(fc, "lyr1")
>>> lyr2 = arcpy.MakeFeatureLayer_management(lyr1, "lyr2")
>>> arcpy.GetCount_management(lyr1)
<Result '193'>
>>> arcpy.GetCount_management(lyr2)
<Result '193'>
>>>
>>> arcpy.SelectLayerByAttribute_management(lyr1, "NEW_SELECTION", "OBJECTID = 1")
<Result 'lyr1'>
>>> arcpy.GetCount_management(lyr1)
<Result '1'>
>>> arcpy.GetCount_management(lyr2)
<Result '193'>
>>>
Hi.
I have rewritten the select by location part to:
arcpy.SelectLayerByLocation_management("SUBURB","INTERSECT", "Cadastre_Layer")
but the suburb layer does not have any records selected. I tried creating a feature layer of the suburb feature class without success. I have found select layer by attribute selects the feature being searched for.
To debug, put a Get Count—Help | ArcGIS Desktop after your selection to make sure the selection is actually selecting records.
Hi.
The Get Count confirms there are no records being selected.