How do you use ArcGIS API for python to specify a layer based on title

1024
4
Jump to solution
03-19-2021 04:14 AM
Labels (2)
Damian
by
New Contributor III

Dear ESRI community, 

How do you use ArcGIS API for python to specify a layer in a web map based on the layers title? All of the samples I've seen just use the layer at index 0. I can't find any samples that search for a particular layer.  For example:

 

my_webmap = gis.content.search(query="title:Trees",item_type="Web Map")
my_item = my_content[0]
wm = WebMap(my_item)
Taxon_layer = wm.layers[0] #I want to select the layer where "Title" = "Taxon"
Taxon_layer
===============================================
Out:
{
  "id": "Markup_X_3074",
  "layerType": "ArcGISFeatureLayer",
  "url": "https://services5.arcgis.com/wkEdAXzuNvKdAtLV/arcgis/rest/services/Markup_X/FeatureServer/0",
  "visibility": true,
  "opacity": 1,
  "title": "Taxon",
  "itemId": "b022f56ae0c347019ca2c9aae60c450c"
}

 

Ultimately, I want to iterate through several web maps and modify a particular layer in each web map . The title is always the same but the index number will change each time. I know how to iterate, but I don't know how to select a layer based on title.

Any assistance greatly appreciated

Regards, Damian

 

1 Solution

Accepted Solutions
jcarlson
MVP Esteemed Contributor

There is a method get_layer() on the WebMap class, according to the API docs. Testing it, however, I can't get it to return anything.

There is also the layers property, which returns all the layers in a map as a dict. Iterating over that isn't too hard.

web_map = arcgis.mapping.WebMap(gis.content.get('webmap-itemID'))

for l in web_map.layers:
    if l.title == 'Layer title you want':
        # do your layer edits here

 

- Josh Carlson
Kendall County GIS

View solution in original post

4 Replies
jcarlson
MVP Esteemed Contributor

There is a method get_layer() on the WebMap class, according to the API docs. Testing it, however, I can't get it to return anything.

There is also the layers property, which returns all the layers in a map as a dict. Iterating over that isn't too hard.

web_map = arcgis.mapping.WebMap(gis.content.get('webmap-itemID'))

for l in web_map.layers:
    if l.title == 'Layer title you want':
        # do your layer edits here

 

- Josh Carlson
Kendall County GIS
Damian
by
New Contributor III

Great, thank you Josh, that did the trick. I wonder if I could impose on you for one more question please (if you know the answer).?

Not only am I trying to specify a particular layer within a webmap (achieved above), I am also trying to specify a particular field within that layer. Example below:

 

my_webmap = gis.content.search(query="title:Trees",item_type="Web Map")
my_item = my_webmap[0]
my_itemID = my_item.id

wm = WebMap(gis.content.get(my_itemID))
# Thank you Josh...
for l in wm.layers:
    if l.title=="Gum Trees":
        l.popupInfo.fieldInfos[0].visible = True  
        #I want to turn on the visibility for a field called "Species", 
        #not just the first field in my layer.

 

So I want to run the command "l.popupInfo.fieldInfos[0].visible = True", for a field called "Species", not the first field at index '0' in my layer. As before, I want to iterate several layers and the index number may vary each time.

Regards, Damian

AdamGaudet
New Contributor III

It's a few years after the fact, but did you ever solve for this issue? I might need to do something similar for another operation.

0 Kudos
AdamGaudet
New Contributor III

Thanks all for posting here. I spent a couple of days trying to do this exact same thing and was pulling my hair out. But now all issues are resolved. *Zen*

0 Kudos