Cast Feature Layer to Layer in Arcpy/Pro

450
9
Jump to solution
05-05-2021 03:45 PM
MikeMacRae
Regular Contributor

Hello all. I have a script I am updating from ArcGIS Desktop (Python 2) to ArcGIS Pro (Python 3). As part of the older script I would cast a feature class as a feature layer via MakeFeatureLayer_management and then I would further cast that feature layer to a Layer object to get access to the layer's properties:

 

arcpy.MakeFeatureLayer_management(fc, "lyr")
lyr = arcpy.mapping.Layer("lyr")

 

 It appears that the Pro version of arcpy doesn't actually have a layer object in the mapping (mp) module anymore. So, if I were to do something similar:

 

arcpy.MakeFeatureLayer_management(fc, "lyr")
lyr = arcpy.mp.Layer("lyr")

 

which returns:

AttributeError: module 'arcpy.mp' has no attribute 'Layer'

I also tried:

 

arcpy.MakeFeatureLayer_management(fc, "lyr")
lyr = arcpy.mp.LayerFile("lyr")

 

and threw the error:

ValueError: lyr

So, at the end of the day, I need to take a geodatabase feature class and somehow cast it as a layer in order to manipulate layer properties in arcpy for Pro. Any suggestion how I can do that. Points for avoiding saving the layer to disk.

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
Luke_Pinner
MVP Regular Contributor

Just use the layer object returned by the MakeFeatureLayer result:

 

layer = arcpy.management.MakeFeatureLayer(fc, "lyr")[0]
layer
<arcpy._mp.Layer object at 0x0000023785BDD080>
layer.name
'lyr'

 

I don't have access to Desktop/ArcMap to test if the `[0]` indexing works like it does in Pro, but you could have used something like the following in ArcMap instead of casting:

 

layer = arcpy.MakeFeatureLayer_management(fc, "lyr").getOutput(0)

 

 

 

 

 

 

 

View solution in original post

9 Replies
DanPatterson
MVP Honored Contributor

See the code example.  Your layer will have the name "lyr"

Layer—ArcGIS Pro | Documentation

a Layerfile is a *.lyr or *.lyrx on disk

LayerFile—ArcGIS Pro | Documentation

 


... sort of retired...
MikeMacRae
Regular Contributor

Hey Dan. I looked at the code examples before, however to get to a single layer, it appears that you have to list out the maps from a project and then list the layers out. The examples don't speak to casting directly to a layer object without listing them out from some source. For example in the Desktop help doc, example #1 on the second line, it shows how to grab a layerfile and cast it as directly as a layer object via arcpy.mapping.Layer.I find it odd that Pro arcpy does not have that layer object in the new mapping class.

0 Kudos
mody_buchbinder
New Contributor III

Hi

The best way I found to do it is to use arcpy.SaveToLayerFile_management on the feature layer into some temporary folder. Then use the lyrx that was created to create a arcpy.mp.LayerFile

Not very elegant but does the work and does not take too much time.

Have Fun 

MikeMacRae
Regular Contributor

Thanks. Yes, so far that's the only way I can see doing this, at this point. It's not how I want to handle the data, so I'm hoping to avoid saving to disk.

0 Kudos
Luke_Pinner
MVP Regular Contributor

Just use the layer object returned by the MakeFeatureLayer result:

 

layer = arcpy.management.MakeFeatureLayer(fc, "lyr")[0]
layer
<arcpy._mp.Layer object at 0x0000023785BDD080>
layer.name
'lyr'

 

I don't have access to Desktop/ArcMap to test if the `[0]` indexing works like it does in Pro, but you could have used something like the following in ArcMap instead of casting:

 

layer = arcpy.MakeFeatureLayer_management(fc, "lyr").getOutput(0)

 

 

 

 

 

 

 

View solution in original post

MikeMacRae
Regular Contributor

Thanks @Luke_Pinner I am forgetting the subtleties of the switch to Pro. I didn't realized there was a sub-module for 'management' now as opposed to direct calls to Data Management tools. i.e.

Desktop 10.x:

arcpy.MakeFeatureLayer_management

vs ArcGIS Pro:

arcpy.management.MakeFeatureLayer

And nice call on the indexing of the layer object too. That's not something I would have intuitively thought to do.

 

Cheers,

Mike

0 Kudos
DanPatterson
MVP Honored Contributor

Mike

 MakeFeatureLayer_management or arcpy.management.MakeFeatureLayer

both work in Pro


... sort of retired...
Luke_Pinner
MVP Regular Contributor

arcpy.subpackage.Tool and arcpy.Tool_subpackage works in both Pro and Desktop.

DanPatterson
MVP Honored Contributor

another example here

Re: Script stopped working properly with upgrade o... - Esri Community

All paths lead to the same destination

In fact, if one is interested in the scripts follow the trail  for MakeFeatureLayer

Toolbox location

C:\...Install_Folder...\Resources\ArcToolBox\toolboxes

C:\...Install_Folder...\Resources\ArcToolBox\toolboxes\Data Management Tools.tbx\MakeFeatureLayer.tool

Script and function location

C:\...Install_Folder...\Resources\ArcPy\arcpy\management.py


... sort of retired...