Use SHAPE@ tokens in arcpy.da.SearchCursor with layer (.lyr)

1538
5
Jump to solution
09-04-2018 11:45 AM
MaximeDemers
Occasional Contributor III

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.

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
JamesMacKay3
Occasional Contributor

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:
  ...

View solution in original post

5 Replies
JamesMacKay3
Occasional Contributor

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:
  ...
DanPatterson_Retired
MVP Esteemed Contributor

SearchCursor—Data Access module | ArcGIS Desktop 

keep mentioning featureclass or table.. with no mention of featurelayer

an omission? or as by design

JoshuaBixby
MVP Esteemed Contributor

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.

JamesMacKay3
Occasional Contributor

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.

JoshuaBixby
MVP Esteemed Contributor

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.

0 Kudos