Select to view content in your preferred language

Trouble with script regarding matching symbology to a .stylx

295
1
02-26-2024 06:41 AM
Annie_Rosen
New Contributor

Hi!

I am new to python, but figured I could script a tool for me and my collegues to use to automate some of our work.

The first tool I am trying to create will allow the user to choose multiple layers and match them to a specific .stylex-file and the value will be set to antikv_bed. So it is an upgraded, but simpler version of the tool “Match Layer Symbology To A Style”.

After a couple of tries, I gave up and consulted ChatGPT, and it came up with the following code. It runs, without errors, but didn’t change any of the symbols.

The .stylx file works in “Match Layer Symbology To A Style”, so there is no problem.

Can someone please help me? 🙂

import arcpy

# Definiera funktionen för att matcha lagersymboliken mot en stil med hjälp av ett fält
def match_layer_symbology_to_style(input_layers):
    try:
        style_file = r"M:\GIS\ArcGIS...."
        match_field = "antikv_bed"
        for layer in input_layers:
            arcpy.management.MatchLayerSymbologyToAStyle(layer, style_file, True)
            arcpy.AddMessage(f"Symbology successfully matched to style for layer: {layer}")
    except arcpy.ExecuteError:
        arcpy.AddError(arcpy.GetMessages(2))
    except Exception as e:
        arcpy.AddError(str(e))

# Parametrar för scriptverktyget
input_layers = arcpy.Parameter(
    displayName="Input Layers",
    name="input_layers",
    datatype="GPValueTable",
    parameterType="Required",
    direction="Input")

# Definiera parametrarna för scriptverktyget
params = [input_layers]

# Kör funktionen med de angivna parametrarna
def main():
    input_layers = arcpy.GetParameter(0) # Hämta parametern som en lista av lager
    match_layer_symbology_to_style(input_layers)

if __name__ == "__main__":
    main()

0 Kudos
1 Reply
AlexanderDanielPratama
Esri Contributor

I am guessing, the reason it does not work because you use the arcpy.GetParameter. You could probably use arcpy.GetParameterAsText if your value is string. I never use arcpy.GetParameter so probably you need to "parse" or get the value inside GetParameter first because your input is GPValueTable.

Another alternative, you might create csv file and put as an input. Then, tried to parsing the value in the csv as the input of your match_layer_symbology_to_style

Hope it helps you.

Cheers