replace a FeatureLayer in WebMap

663
2
Jump to solution
08-10-2022 04:22 PM
john_cartwright_noaa
New Contributor III

Hello All,

I'm trying to replace a FeatureLayer in an WebMap and retain all the layerDefinition, popupInfo, title, etc.

At first I just tried replacing the layer's url property with the new feature service URL but that didn't work.

Then I tried the remove_layer/add_layer as described in the Samples. Although the Feature Layer was updated with this approach, none of the popupInfo or layerDefintion information was. (code snippet below).

How can I transfer the attributes from the old layer to the new? Going through the UI to re-create the popup, symbology, etc. is not practical.

Thanks!

title = layer_to_be_replaced.title
layerDefinition = layer_to_be_replaced.layerDefinition
popupInfo = layer_to_be_replaced.popupInfo
webmap.remove_layer(layer_to_be_replaced)
webmap.add_layer(replacement_layer, options = {
    'title': title,
    'popupInfo': popupInfo,
    'showLegend': True,
    'layerDefinition': layerDefinition
})
webmap.update()
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
Brian_Wilson
Occasional Contributor III

Have you also figured out that doing remove and add moves the layer to the end of the layer list where it is almost certainly in the wrong place?

The map file is just a JSON file. It's possible to edit them with a JSON file editor, it is also possible to use a web based editor like ArcGIS Online Assistant. (There is a newer thing but have not tried it yet.)

These methods are fraught with the usual perils of missing commas and mismatched braces that have plagued programmers throughout time. When you do it right, it's easy and fast.

1. Make backup of original file

2. Edit, save, or possibly if you have a working map, copy, paste, save.

3. Clear cache in browser, reload, test.

4. Restore backup. (Hopefully you don't have to do this step!)

It seemed like a big pain but the alternatives (copying popups from one map to another) are more awful.

Here's one that happened here recently -- if you delete a custom basemap then every map that used the basemap is broken and cannot be loaded. Every app based on those maps is now broken and cannot load. Changing one line in the JSON files fixes it.

Learning how to do this is worth it.

 

View solution in original post

2 Replies
Brian_Wilson
Occasional Contributor III

Have you also figured out that doing remove and add moves the layer to the end of the layer list where it is almost certainly in the wrong place?

The map file is just a JSON file. It's possible to edit them with a JSON file editor, it is also possible to use a web based editor like ArcGIS Online Assistant. (There is a newer thing but have not tried it yet.)

These methods are fraught with the usual perils of missing commas and mismatched braces that have plagued programmers throughout time. When you do it right, it's easy and fast.

1. Make backup of original file

2. Edit, save, or possibly if you have a working map, copy, paste, save.

3. Clear cache in browser, reload, test.

4. Restore backup. (Hopefully you don't have to do this step!)

It seemed like a big pain but the alternatives (copying popups from one map to another) are more awful.

Here's one that happened here recently -- if you delete a custom basemap then every map that used the basemap is broken and cannot be loaded. Every app based on those maps is now broken and cannot load. Changing one line in the JSON files fixes it.

Learning how to do this is worth it.

 

john_cartwright_noaa
New Contributor III

Thanks for your reply Brian. I'm comfortable editing JSON but didn't know how to get the edited JSON back into the existing webmap. I didn't know the ArcGIS Online Assistant would do things like that.

I was hoping for a scriptable solution but the Assistant appears to work great as a console-based approach.

Thanks for the suggestion!

0 Kudos