3

My ArcGIS Pro 2.8 project has 8 maps (e.g. Tot Pop, 11-14, 0-18, etc...), and they each have these three layers: Elementary, Secondary and Unified. What I've attempted is to iterate the maps. Then iterate each layer. Then get the layer by lyr.name(). From this point on, I'm stuck.

GOAL: I want to subsequently overwrite that layers' weblayer by using a counter that's tied to the list. The items in the list are the names of existing hosted feature services. For example, in the Tot Pop map, if the layer's name is Elementary grab Elementary Tot Pop from the list and use it to find the weblayer with the same name. Then overwrite it. I've left out the 'find and overwrite' part of the script for simplification.

fs_names = ['Elementary Tot Pop', 'Secondary Tot Pop', 'Unified Tot Pop',
           'Elementary 11 to 14', 'Secondary 11 to 14', 'Unified 11 to 14',
           'Elementary 0 to 18', 'Secondary 0 to 18', 'Unified 0 to 18'...]

for prj in aprx.listMaps():
    print(f'--- Map: {prj.name} ---')
    for lyr in prj.listLayers():
        print(f'lyr: {lyr}')
        n = 0
        if lyr.name('Elementary'):
            overwrite0 = fs_names[n]
            print(overwrite0)
            #find and overwrite the weblayer here...
        elif lyr.name('Secondary'):
            overwrite1 = fs_names[n]
            print(overwrite1)
            #find and overwrite the weblayer here...
        elif lyr.name('Unified'):
            overwrite2 = fs_names[n]
            print(overwrite2)
            #find and overwrite the weblayer here...
            
        n += 1

What it prints out:

--- Map: Tot Pop ---
lyr: Elementary
Elementary Tot Pop
lyr: Secondary
Elementary Tot Pop
lyr: Unified
Elementary Tot Pop
--- Map: 11-14 ---
lyr: Elementary
Elementary Tot Pop
lyr: Secondary
Elementary Tot Pop
lyr: Unified
Elementary Tot Pop
--- Map: 0-18 ---
lyr: Elementary
Elementary Tot Pop
lyr: Secondary
Elementary Tot Pop
lyr: Unified
Elementary Tot Pop

What I want to see:

--- Map: Tot Pop ---
lyr: Elementary
Elementary Tot Pop
lyr: Secondary
Secondary Tot Pop
lyr: Unified
Unified Tot Pop
--- Map: 11-14 ---
lyr: Elementary
Elementary 11 to 14
lyr: Secondary
Secondary 11 to 14
lyr: Unified
Unified 11 to 14
--- Map: 0-18 ---
lyr: Elementary
Elementary 0 to 18
lyr: Secondary
Secondary 0 to 18
lyr: Unified
Unified 0 to 18

This help doc does what I want one layer at a time.

4

You're resetting your n=0 every loop, so it's always going to be the first value in the list. Move n=0 out of the for loop (put it above for lyr in prj...)

for prj in aprx.listMaps():
    print(f'--- Map: {prj.name} ---')
    n=0
    for lyr in prj.listLayers():
        print(f'lyr: {lyr}')
        if lyr.name('Elementary'):
            overwrite0 = fs_names[n]
            print(overwrite0)
            #find and overwrite the weblayer here...
        elif lyr.name('Secondary'):
            overwrite1 = fs_names[n]
            print(overwrite1)
            #find and overwrite the weblayer here...
        elif lyr.name('Unified'):
            overwrite2 = fs_names[n]
            print(overwrite2)
            #find and overwrite the weblayer here...
            
        n += 1
1
  • I had to move n = 0 out of every loop, actually, to get what I want! So, it's above for prj in aprx.listMaps(). But, you had the right idea.
    – Pfalbaum
    Oct 25 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.