Match Symbology to a style?

628
2
Jump to solution
01-27-2023 10:56 AM
Davec43
Occasional Contributor

I've created a list of the layers in the map and I'm trying to then use that list to match symbology to a style. Is it possible? When I try I get an error.

import arcpy

import random
from itertools import groupby
from collections import defaultdict
aprx = arcpy.mp.ArcGISProject("CURRENT")
act_map = aprx.activeMap
map1 = aprx.listMaps("Map1")

layer = act_map.listLayers()
lyr_list = []
for position in range(0, len(layer)):
    lyr_name = layer[position].name
    lyr_list.append(lyr_name)

arcpy.management.MatchLayerSymbologyToAStyle(lyr_list, "$feature.Type", r"\\Path\ArcGIS_Styles.stylx")

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 10200, in MatchLayerSymbologyToAStyle
    raise e
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 10197, in MatchLayerSymbologyToAStyle
    retval = convertArcObjectToPythonObject(gp.MatchLayerSymbologyToAStyle_management(*gp_fixargs((in_layer, match_values, in_style), True)))
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 512, in <lambda>
    return lambda *args: val(*gp_fixargs(args, True))
RuntimeError: Object: Error in executing tool

 

0 Kudos
1 Solution

Accepted Solutions
DanielMiranda2
Occasional Contributor

MatchLayerSymbologyToAStyle takes a layer as an input parameter, but you are trying to pass a list of layers into the tool, which will not work. Your MatchLayerSymbologyToAStyle has to be part of your loop.

Additionally, lines 10-14 are not needed. listLayers() gives you everything you need, so constructing a list of layer names is extra work. I would remove lines 10-16 and use this:

 

 

layers = act_map.listLayers()
for lyr in layers:
    arcpy.management.MatchLayerSymbologyToAStyle("$feature.Type", r"\\Path\ArcGIS_Styles.stylx") 

 

 

Note, act_map is referring to your current active map in your ArcGIS Pro project. This can sometimes be annoying if you have multiple maps in a project and don't have the right one open. I usually prefer to just define the map by its name. You do that with, 

map1 = aprx.listMaps("Map1")

But never actually use this variable. If map1 is what you actually want to use, I'd replace act_map with map1.

View solution in original post

2 Replies
DanielMiranda2
Occasional Contributor

MatchLayerSymbologyToAStyle takes a layer as an input parameter, but you are trying to pass a list of layers into the tool, which will not work. Your MatchLayerSymbologyToAStyle has to be part of your loop.

Additionally, lines 10-14 are not needed. listLayers() gives you everything you need, so constructing a list of layer names is extra work. I would remove lines 10-16 and use this:

 

 

layers = act_map.listLayers()
for lyr in layers:
    arcpy.management.MatchLayerSymbologyToAStyle("$feature.Type", r"\\Path\ArcGIS_Styles.stylx") 

 

 

Note, act_map is referring to your current active map in your ArcGIS Pro project. This can sometimes be annoying if you have multiple maps in a project and don't have the right one open. I usually prefer to just define the map by its name. You do that with, 

map1 = aprx.listMaps("Map1")

But never actually use this variable. If map1 is what you actually want to use, I'd replace act_map with map1.

Davec43
Occasional Contributor

Thank you!

0 Kudos