Problem: I have a point feature and reference layer that I use in multiple maps for different crews. The form associated with it in Field Maps Designer is somewhat complicated and I have to remake the form for each new map I put that point and background layer in. This is a pain and also prone to errors and inconsistencies.
I have the following code that copies over the layer I want from the operationallayers of a map that has been configured in Field Maps Designer, to a blank map. It looks correct as far as I can tell and in Field Maps Designer on the new map all of the calculations are the same. BUT the blue plus button is not present in the Field Maps App for the new map and I cannot collect the point feature like I can in the original map.
from arcgis.gis import GIS
import json
gis = GIS('home')
source_web_map_id = 'xy'
dest_web_map_id = 'xy'
# get underlying data for the webmaps as a dictionary
#Source webmap with field maps configured
src_wm_item = gis.content.get(source_web_map_id)
src_wm_data = src_wm_item.get_data()
# blank new webmap
dest_wm_item = gis.content.get(dest_web_map_id)
dest_wm_data = dest_wm_item.get_data()
for layer in src_wm_data.get("operationalLayers"):
if layer.get("title") in ['Layer 1 Name', "Layer 2 Name"]:
dest_wm_data["operationalLayers"].append(layer)
# Create the item_properties dictionary with the updated JSON
item_properties = {"text": json.dumps(dest_wm_data)}
# Update the web map item with the new JSON definition
update_result = dest_wm_item.update(item_properties=item_properties)
print(update_result)
Can anyone help me figure out what I am missing?
Solved! Go to Solution.
Short Answer: you have to manually save the map after updating it. Does anybody know how to do this programmatically? .save(item_properties) creates a new map.
I was finally able to get the blue '+' button to appear and use the point normally by manually saving the map in map viewer after running the code. I still do not see any difference in the web map definition after doing this so I don't understand why it works.
The code I ended up with:
from arcgis.gis import GIS
from arcgis.mapping import WebMap
gis = GIS('home')
source_web_map_id = 'xyz'
dest_web_map_id = 'abc'
# get source web map with field maps configured
src_wm_item = gis.content.get(source_web_map_id)
src_wm = WebMap(src_wm_item)
# get dest web map
dest_wm_item = gis.content.get(dest_web_map_id)
dest_wm = WebMap(dest_wm_item)
# append desired layers to the destination operationalLayers list
for layer in src_wm.definition['operationalLayers']:
if layer['title'] in ['layer_1', 'another_layer_I_want']:
dest_wm.definition['operationalLayers'].append(layer)
update = dest_wm_item.update({'text': dest_wm.definition})
# should be True
print(update)
Hi @Sean_Perks,
You can create a Map object first (or WebMap if using version 2.3.0 or earlier) and save it as a new WebMap item (Map.save()). So create a Map object from your source map and save it as your destination map, then you can run a remove layer over the new WebMap and only keep the layers you want. This will maintain the forms too and other settings that may be required for Field Maps
I have used your approach successfully in the past but I am traveling at the moment and don't have access. I can update her next week.
Just to note, you do not need JSON on your workflow above, get_data() returns a dictionary, you can use the manipulated dictionary {"text" : dest_wm_data}
All the best,
Glen
The approach you suggested does work and will suffice for my current purpose. I would still love to know how to do it by updating the dictionary because I can see a situation where I want to add this layer to an already established map or adjust other settings in the dictionary.
Thanks for your note about the json! Can't remember why I thought that was necessary but cleans things up.
Short Answer: you have to manually save the map after updating it. Does anybody know how to do this programmatically? .save(item_properties) creates a new map.
I was finally able to get the blue '+' button to appear and use the point normally by manually saving the map in map viewer after running the code. I still do not see any difference in the web map definition after doing this so I don't understand why it works.
The code I ended up with:
from arcgis.gis import GIS
from arcgis.mapping import WebMap
gis = GIS('home')
source_web_map_id = 'xyz'
dest_web_map_id = 'abc'
# get source web map with field maps configured
src_wm_item = gis.content.get(source_web_map_id)
src_wm = WebMap(src_wm_item)
# get dest web map
dest_wm_item = gis.content.get(dest_web_map_id)
dest_wm = WebMap(dest_wm_item)
# append desired layers to the destination operationalLayers list
for layer in src_wm.definition['operationalLayers']:
if layer['title'] in ['layer_1', 'another_layer_I_want']:
dest_wm.definition['operationalLayers'].append(layer)
update = dest_wm_item.update({'text': dest_wm.definition})
# should be True
print(update)