Calling a map using it name (Python)

1625
4
12-16-2020 04:11 AM
AmitCohen
New Contributor

Hi,

I have a map name (strings).

I want to know how to access the listLayer() of a specific map by the "string" name of the map.

 

 

x = "Map1"
for i in x.listLayers():
    print("Layer: " + i.name)

 

 

not really working because I don't know how to "call" the specific map.

 

Thanks!

 

Tags (3)
0 Kudos
4 Replies
JohannesBierer
Occasional Contributor III

Maybe this example could help you:

https://pro.arcgis.com/de/pro-app/arcpy/mapping/layer-class.htm

import arcpy
aprx = arcpy.mp.ArcGISProject(r"C:\Projects\YosemiteNP\Yosemite.aprx")
m = aprx.listMaps("Yosemite National Park")[0]
for lyr in m.listLayers():
    if lyr.supports("DEFINITIONQUERY"):
        lyr.definitionQuery = ""
    if lyr.supports("SHOWLABELS"):
        lyr.showLabels = False
aprx.save()
del aprx
DavidPike
MVP Frequent Contributor

Sorry @JohannesBierer ,I didn't notice your reply.

0 Kudos
JohannesBierer
Occasional Contributor III

no problem, your solution is easier to follow 🙂

0 Kudos
DavidPike
MVP Frequent Contributor

 

aprx = arcpy.mp.ArcGISProject(r"C:\Projects\YosemiteNP\Yosemite.aprx")
m = aprx.listMaps("Yosemite National Park")[0]

 

from the code sample on the layer class page Layer—ArcGIS Pro | Documentation

 

import arcpy

aprx = arcpy.mp.ArcGISProject(r"C:\myproject.aprx")
my_map = aprx.listMaps("Your Map")[0]

for lyr in my_map.listLayers():
    print("Layer: " + lyr.name)