Hi,
Is it possible to use the SHAPE@ or SHAPE@JSON fields tokens in arcpy.da.SearchCursor with a layer (.lyr) as the in_table parameter with ArcGIS 10.3?
When I try it, I got the following error: RuntimeError: A column was specified that does not exist.
If I use the path to the feature class as the in_table parameter, it works.
Solved! Go to Solution.
I was able to reproduce what you found using the SHAPE@ token, that is odd.
Simple workaround is to create a layer (arcpy.mapping.Layer) using the LYR file first and then create the search cursor on it, e.g.:
lyr = arcpy.mapping.Layer(r"C:\Wherever\MyLayer.lyr")
with arcpy.da.SearchCursor(lyr, ["SHAPE@"]) as sc:
...
I was able to reproduce what you found using the SHAPE@ token, that is odd.
Simple workaround is to create a layer (arcpy.mapping.Layer) using the LYR file first and then create the search cursor on it, e.g.:
lyr = arcpy.mapping.Layer(r"C:\Wherever\MyLayer.lyr")
with arcpy.da.SearchCursor(lyr, ["SHAPE@"]) as sc:
...
SearchCursor—Data Access module | ArcGIS Desktop
keep mentioning featureclass or table.. with no mention of featurelayer
an omission? or as by design
Are you trying to pass the path to a layer file directly to a Search Cursor? If so, it doesn't work in any version of ArcMap. What is really odd, it works in ArcGIS Pro for the layer files I tested.
The documentation states the input is: "feature class, layer, table, or table view." A "layer" and a "layer file" are not the same, the latter contains information sufficient to instantiate the former.
It's also odd that you can open a layer file with a search cursor and retrieve attributes other than the shape - I tried pulling the OID@ token from a layer file and it was no problem, switched it to SHAPE@ and it raised an error.
Did you try the wildcard for all? When I did it fails, which may be related to shape being included with the wildcard.
This is one of those cases where it would be good for Esri to update the documentation to reflect both their intent and expected behavior.