Select to view content in your preferred language

Apply MatchLayerSymbologyToAStyle() to multiple layers and styles simultaneously if the style name differs from the code describing the style...

1563
0
10-07-2023 11:29 AM
Labels (2)
DanielBuchar1
New Contributor

Hi everyone,

Apply arcpy.management.MatchLayerSymbologyToAStyle() to multiple layers and styles simultaneously if the style name differs from the code describing the style in the attribute table.

I have a problem with writing a script that automatically assigns the appropriate style to the corresponding layer. The issue I'm facing is that when a particular style matches, like in the code below, the automatic assignment replaces the previous one. As a result, the assignment is not correct. I'm sharing this because in the layer itself, I have a column called "KOD_KARTO," which contains only the code, for example, "OTKT." Meanwhile, in the style's name, I have 'LUNA (OTKT),' and in the same layer, in the "KOD_KARTO" column, I only have the code "OTKC," and in the style, I have 'TEST (OTKC).' Therefore, the script assigns the style 'TEST' first, and then it replaces it again with 'LUNA,' and these are the only ones that remain.

The question is, how can I assign all styles or write an expression to ensure that all styles are assigned to the layer without overwriting each other? This is happening because the stylex file has incorrectly assigned style names, and the database is in the client's environment where I can't change the database structure.

# Nazwa warstwy, którą chcesz zmienić
nazwa_warstwy = "x"

# Zmień styl warstwy na "TEST"
arcpy.management.MatchLayerSymbologyToAStyle(nazwa_warstwy, " 'TEST'+ ' ' + '('+$feature.KOD_KARTO+ ')'", s)

# Zmień styl tej samej warstwy na "LUNA"
arcpy.management.MatchLayerSymbologyToAStyle(nazwa_warstwy, " 'LUNA'+ ' ' + '('+$feature.KOD_KARTO+ ')'", s)

 

Here's the full script with a partial list (name: b) attached because it's quite lengthy.

import arcpy

#warstwa = 'x'

s = r"C:\esri\aprx\MapaZasadnicza2021\Mapa_Zasadnicza_2021_1000 - Copy.stylx"

b = [
    'Bariera ochronna - symbol (OTKB_S)',
    'Brama - symbol (OTOB_S)',
    'Ciepłownicze urządzenie techniczne (SUCU)', 
    'opis_kan ()',
    'opis_tel ()',
    'opis_wod ()',
    'opis_zielony ()',
    'Projektowana obudowa ochronna przewodu ciepłowniczego (SUCP)',
    'Projektowana obudowa ochronna przewodu elektroenergetycznego (SUEP)',
    'Projektowana obudowa ochronna przewodu gazowego (SUGP)',
    'TEST (OTKC)',
    'LUNA (OTKT)',
    'opis_zielony2 ()'
]


aprx = arcpy.mp.ArcGISProject("CURRENT")  # Otwiera bieżący projekt ArcGIS Pro
mapa = aprx.listMaps('1_test_Mapa')[0]  # Załóżmy, że chcesz działać na pierwszej mapie projektu



# ################### EDYCJA ##############

max_layers_to_process = 1  # Określ maksymalną liczbę warstw do przetworzenia

# #################################

layers = mapa.listLayers()  # Pobierz listę warstw na wybranej mapie

for i, warstwa in enumerate(layers, start=1):
    
    if i > max_layers_to_process:
        print('\n')
        print('END')
        break  # Przerwij pętlę po osiągnięciu maksymalnej liczby warstw do przetworzenia
    
    print(f"Dostosowywanie warstwy {i}: {warstwa.name}")
    print('\n')
    
    warstwa = 'x' 
    
    for item in b:
        if '(' in item:
            item_without_prefix = item[:item.find('(')].strip()
            print(f'Pierwsza część: {item_without_prefix}')
            # Tutaj możesz umieścić kod, który ma być wykonany, gdy nawiasy są obecne
            arcpy.management.MatchLayerSymbologyToAStyle(warstwa, " '{}' + ' '+ '(' + $feature.KOD_KARTO + ')' ".format(item_without_prefix), s)

    #         else:
    #             last_space_index = item.rfind(' ')
    #             if last_space_index != -1:
    #                 item_without_suffix = item[:last_space_index]
    #                 print(f'Druga część: {item_without_suffix}')
    #                 arcpy.management.MatchLayerSymbologyToAStyle(warstwa, " '{}' + ' '+  $feature.KOD_KARTO ".format(item_without_suffix), s)

    #             # Tutaj możesz umieścić kod, który ma być wykonany, gdy brak nawiasów, ale jest sufix do ostatniej spacji
    #             else:
    #                 print(f'Inna sytuacja: {item}')
    #                 # Tutaj możesz umieścić kod, który ma być wykonany w innych przypadkach

 

Here, there are more issues because when I iterate through the list named 'b', if I encounter 'TEST (OTKC)', the program detects it and symbolizes it, then it overwrites the change for 'LUNA (OTKT)', and finally, it removes everything because there is 'opis_zielony ()'. Please help me figure out how to let the program know that these styles have already been assigned or how to write an expression that will take both 'TEST (OTKC)' and 'LUNA (OTKT)' as items to assign symbols from the list while treating the other elements as non-matching

Additionally, adding flags at the end doesn't work either because iterating through a dictionary is slow, and there are many records. Here, I've shown only two matching elements as an example, but sometimes a layer can have 6 different codes in the 'KOD_KARTO' field, and there may be around 60,000 records to process. There are approximately 10 maps and around 60 layers.

Searching for unique records doesn't help either because the MatchLayerSymbologyToAStyle tool needs to iterate through the records. There might be something I'm not aware of, which is why I posted on the forum.

Class.pngStylex.png

I would greatly appreciate your assistance.

Best regards,
Daniel

Tags (3)
0 Replies