Applying symbology from a .lyr or .lyrx file has been an on-going battle for me. I had an earlier post describing a problem with 1.4. I was able to get past that, but now find a different problem at 2.0.
I am in the process of upgrading a couple of applications from 1.4 to 2.0 I have encountered a problem when I attempt to apply the symbology of a .lyr or .lyrx file to a layer brought into the session by the application. This had been working in 1.4
I wanted to see how Pro itself handled this by running the GeoProcessing Tool "Apply Symbology From Layer" interactively through native Pro. I manually brought in a non symbolized grid layer into Pro. I ran the tool and entered the grid layer as the input layer. For the Symbology layer, I navigated to a .lyrx file that is built from a different but identically structured grid. As soon as I finished browsing to this file and selected it, the "ApplySymbology From Layer" tool 'seized up'.
Regarding the .lyrx file. I am able to add it directly to a pro-session with no problems and proper symbolization. If I do this, I am able to run the Apply Symboloby From Layer tool interactively, using the just added grid (from the .lyrx) file as the symbol layer and the tool works. It appears the version 2.0 tool only works with fully qualified layers that have been added to the session and not with providing it .lyr or .lyrx files that point to and are symbolized the same way.
I have a series of screen captures illustrating this experience with using the tool with a .lyrx file and then with a layer already added to Pro.
This image shows a non symbolized grid file added to Pro
This image shows trying to run the tool using a .lyrx file as the symbology layer source. This image also shows the output screen from Visual Studio that captures how it behaved after adding the .lyrx file to the Symbology Layer input:
It simply seizes up.
As a test to make sure the .lyrx file I was using was healthy, I added it to the Pro session through the Add Data button. This image shows it successfully added:
The following image shows running the Apply Symboloby From Layer tool using the just added GRID from the .lyrx file. When I populate the Symbology Layer input, the tool does not misbehave:
The final image shows the initial non-symbolized grid now with the proper symbology.
This seems to confirm that the tool is not properly handling .lyrx files when provided as a the symbology source.
Any assistance is appreciated. I will be unable to reply to any responses for a few days.
Thanks again.
Solved! Go to Solution.
Ditto. My script tool returns no errors but the .lyrx symbology isn't reflected on my layers. Have tried closing and re-opening my project to no avail. Any new workarounds for this??
The issue has been fixed with the SDK only. The developers are aware that it is not working in Python and are looking into it. In my case, I had a simple point symbol to assign and the following steps managed to assign the symbology to my new layer:
They are ways to update some of the symbology properties as well if needed. I did not need to in my case. I hope this helps until the issue gets resolved.
Christine Dartiguenave
Esri Water Resources Team
I was trying to find the answer for python because the arcpy.management.ApplySymbologyFromLayer tool was not working for me.
I wanted to pass along the workaround that I found as it might help people. This bit of code worked for me:
#Get the layer from the map
lyr = myMap.listLayers(SymLayers[n])[i]
#Get the layer file on disk
lyrFile = arcpy.mp.LayerFile(LayerPath + SymLayers[n] + '.lyrx')
#Set the map layer's symbology to the layer file on disk's symbology
lyr.symbology =lyrFile.listLayers()[0].symbology
@williamwinner wow! This works beautifully, thank you!! I was about to pull my hair out with ApplySymbologyFromLayer 😂.
Here is a .NET API code snippet that does the same thing:
//Note: Run within QueuedTask.Run
//Get the Layer Document from the lyrx file
var lyrDocFromLyrxFile = new LayerDocument(layerFile); //.lyrx file
var cimLyrDoc = lyrDocFromLyrxFile.GetCIMLayerDocument();
//Get the renderer from the layer file
var rendererFromLayerFile = ((CIMFeatureLayer)cimLyrDoc.LayerDefinitions[0]).Renderer as CIMUniqueValueRenderer;
//Apply the renderer to the feature layer
//Note: If working with a raster layer, use the SetColorizer method.
featureLayer?.SetRenderer(rendererFromLayerFile);