Hi.
I've created a pythonscript contained in an ArcGIS Pro Toolbox (atbx), where I'd like to change a layer's symbology. I know it's possible to define a layer's symbology through the script parameters in the toolbox by setting the output layer as derived and also defining a lyrx file either as a property ofthe parameter or in the script it self. But in my case, the layer that I want to change symbology for is already added in the ArcGIS Pro project and is being used in a task. Therefore it's not possible for me to remove and add the layer as a derived parameter, since the task-resource for that layer will be broken.
I've tried using arcpy.ApplySymbologyFromLayer, but that doesn't work. There are no errors, but the layer symbology does not change. The lyrx-file works when importing symbology manually in Pro. What's the best practice here? Does anyone know what might be causing ApplySymbologyFromLayer to not have any effect?
One option is to define the new symbology based on the CIM-class, for example CIMUniqueValueRenderer. I've almost created the symbology this way, but I'm not able to set the defaultLabel. The symbology should show only to groups, <Null> and "All other Values". Has anyone got any experience with this?
Solved! Go to Solution.
Hello @TorbjørnDalløkken2 .
Have you taken a look at the arcpy.mp module. We support a limited number of renderers currently but UniqueValue is one of them.
Here are a couple of starting points
https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/symbology-class.htm
https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/uniquevaluerenderer-class.htm
And then Python CIM Access could be used to modify properties not exposed to the API.
https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/python-cim-access.htm
Jeff - Layout and arcpy.mp teams
Seems to be a common issue. MXDs did have a refreshActiveView which I cant see for Pro.
This hack might be worth a go:
"I found that passing a layer object through the ApplySymbologyFromLayer() function, as the first arg, does not work. For whatever reason, you should pass a string through it. So basically change your line from:"
in_layer = arcpy.ApplySymbologyFromLayer_management(in_layer,in_symbology_layer,update_symbology="MAINTAIN")[0]
to:
in_layer = arcpy.ApplySymbologyFromLayer_management(str(in_layer),in_symbology_layer,update_symbology="MAINTAIN")[0]
I just tried using string instead of layer in the ApplySymbologyFromLayer() function, and the results are the same. No errors returned, and the layer symbology does not change..
Hello @TorbjørnDalløkken2 .
Have you taken a look at the arcpy.mp module. We support a limited number of renderers currently but UniqueValue is one of them.
Here are a couple of starting points
https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/symbology-class.htm
https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/uniquevaluerenderer-class.htm
And then Python CIM Access could be used to modify properties not exposed to the API.
https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/python-cim-access.htm
Jeff - Layout and arcpy.mp teams
Why do we have to perform all this coding when simply importing a saved layer file (lyr or lyrx) should work programmatically? In other words, what's the point of having the "Apply Symbology From Layer" function when it's not doing what it's supposed to be doing?
This code worked for me..
p2 = arcpy.mp.ArcGISProject(aprfilenew)
maps = p2.listMaps()
for map in maps:
print ('--------------------------------------------------------------')
print (map.name)
print ('--------------------------------------------------------------')
layers = map.listLayers()
for layer in layers:
if layer.supports('NAME') and layer.supports('LONGNAME') \
and layer.supports('DATASOURCE'):
print (layer.longName + ' ---> ' + layer.dataSource)
print(layer.longName)
newfeaturelayer = layer.longName
arcpy.MakeFeatureLayer_management(layer, newfeaturelayer)
newLayerout = newfeaturelayer + "-az" # ## rename this as needed....
arcpy.ApplySymbologyFromLayer_management(newfeaturelayer, layer)
arcpy.SaveToLayerFile_management(newfeaturelayer, path + newLayerout + '.lyrx', "ABSOLUTE")