Select to view content in your preferred language

What do the elements of ".listMaps" mean ?

789
5
01-11-2024 12:15 AM
Rema
by
New Contributor II

I'm new to Python. Could someone help me ?

The following text was taken from the ArcGIS Pro help page.
What do the contents of the () and [] in the second line of the code mean ?

 

This sample demonstrates how to reference a layer's label classes using ArcGIS Pro.

p = arcpy.mp.ArcGISProject("CURRENT")
m = p.listMaps("Yosemite National Park")[0]
for lyr in m.listLayers():
    if lyr.supports("SHOWLABELS"):
       lblClasses = lyr.listLabelClasses()

 

0 Kudos
5 Replies
DanPatterson
MVP Esteemed Contributor

list all maps that contain the name

"Yosemite National Park")

and since you may have more than one and a list of maps is returned, just return the first

[0]

(python is 0-based, so 0 is the index of the first element in a list)

 


... sort of retired...
EdM
by
New Contributor II

Hello Rema

The code basically returns a Map in your "Current" ArcGIS Pro (the ArcGIS Pro Project which you are working with) project called "Yosemite National Park".

The second line of code does the following:

- p is a variable which holds a reference to the ArcGIS Pro Project.

- listMaps() is a method on the current project object stored in p.

- listMaps() has a single argument which is a wildcard. This allows you to find all maps based upon a name. Normally the wildcard is followed by an *. In this case there is no asterisk which means you want to find those maps which are explicitly called "Yosemite National Park". 

- listMaps() returns a Python List object of ArcGIS Pro Map objects (the help for the method tells you this).

- Lists are made up of items and are zero-indexed, so the first item in the list is at index position 0. You access items in the list using the [] notation; as a 0 is in the [] brackets you are accessing the 1st Map in the list.

 

So the line of code says..... "get me ALL the maps in the current project called "Yosemite National Park" and store in a list , and then get me the 1st Map in the list and store it in a variable called 'm'.

 

I hope that makes sense!?

Rema
by
New Contributor II

Thanks for your reply, EdM.
I got what the arguments means now. But, if the map which I found by ".listMaps()" had several layers, how can I know each layer's index?

0 Kudos
MErikReedAugusta
Occasional Contributor III

There are several ways you could determine the index, but the first question is: "What purpose will that index solve?"

If you just want the index of every layer, as you pass it in the loop:

 

p = arcpy.mp.ArcGISProject("CURRENT")
m = p.listMaps("Yosemite National Park")[0]
layer_index = 0
for lyr in m.listLayers():
    print(layer_index)
    if lyr.supports("SHOWLABELS"):
       lblClasses = lyr.listLabelClasses()
    layer_index += 1

 

Line 3 is the counter we're going to use to track what index we're on as we loop.  Line 5 could easily be replaced with whatever code you needed the index for.

Once we're done with that layer in the loop, though, we need to make sure to keep our counter updated, which is what Line 8 does.  I updated it at the end of the loop because we're talking about an index, and in Python indexes are almost always 0-based, like the list that Dan was talking about up above.  You can update counters anywhere you like, but if you update them too early, you'll start getting 1-based indexes, and some things might break, unless they're expecting that.

------

But for most things, you probably don't actually need the index.  Take a look at the original code:

 

p = arcpy.mp.ArcGISProject("CURRENT")
m = p.listMaps("Yosemite National Park")[0]
for lyr in m.listLayers():
    if lyr.supports("SHOWLABELS"):
       lblClasses = lyr.listLabelClasses()

 

 

 Lines 4 and 5 do a thing on the layer as it passes it in the loop, without needing to know what index it is.  When you do a for loop on that list of layers, each round of the loop is the layer, so you can probably do whatever you need to do with that variable, "lyr".

EdM
by
New Contributor II

Hi Rema

Have a look at @MErikReedAugusta reply. It is a good answer to your question.

Have fun! - ed

0 Kudos