Saving a web map
Starting with the Python API version 1.3, you can save the map widget as a web map in your GIS
. This process persists the basemaps, layers added (with or without your custom symbology, including smart mapping), pop-ups, extent, and graphics drawn (with or without custom symbols) as layers in your web map
.
To save the map, call the save()
method. This method creates and returns a new Web Map Item
object. As parameters, you can specify all valid Item properties as shown below:
from arcgis.gis import GIS
#gis = GIS("your_online_profile")
gis=GIS()
world_countries_item = gis.content.get('ac80670eb213440ea5899bbf92a04998')
world_countries_layer = world_countries_item.layers[0]
world_countries_layer
<FeatureLayer url:"https://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/World_Countries/FeatureServer/0">
map1 = gis.map("USA")
map1
map1.add_layer(world_countries_layer)
webmap_properties = {'title':'World Countries 2022-01-06 16:33pm',
'snippet': 'Jupyter notebook widget saved as a web map',
'tags':['automation', 'python']}
webmap_item = map1.save(webmap_properties, thumbnail=r".\webmap_thumbnail.PNG")
print(webmap_item)
<Item title:"World Countries 2022-01-06 16:33pm" type:Web Map owner:arcgis_python>
Updating a web map
Similarly, if you created the WebMap
object from an existing web map item, you can call the update()
method to update it.
Note, the save()
method always creates a new item with an updated web map definition, so this is a great way to create a copy of an existing web map. For instance, the cell below calls the save()
method and creates a new web map item with the new set of layers, without disturbing the original web map item.
We can search for web maps just like any other item:
webmap_search = gis.content.search("COVID AND UK", item_type="Web Map", outside_org=True)
covid_map_item = webmap_search[0]
You can also create a WebMap object from an existing web map item by passing the web map item as the parameter to the constructor:
from arcgis.mapping import WebMap
covid_map = WebMap(covid_map_item)
for layer in covid_map.layers:
print(layer.title)
Coronavirus COVID-19 Cases V2 - Deaths Coronavirus COVID-19 Cases V2 - Cases Coronavirus COVID-19 Cases V2 - Cases_country Coronavirus COVID-19 Cases V2 - Cases_state
You can update a web map by adding or removing layers. For instance, the cell below removes one of the layers from the web map and adds a different layer.
# remove the first - unclassed layer from the map
covid_map.remove_layer(covid_map.layers[1])
covid_map.add_layer(world_countries_layer, options={'title':'World Countries'})
True
for layer in covid_map.layers:
print(layer.title)
Coronavirus COVID-19 Cases V2 - Deaths Coronavirus COVID-19 Cases V2 - Cases_country Coronavirus COVID-19 Cases V2 - Cases_state World Countries
Next, let's add a layer to the existing map1
and update the web map item:
from arcgis.features import FeatureLayer
## add layer via
# map1.add_layer(covid_map.layers[6])
## or alternatively
map1.add_layer(FeatureLayer("https://services.arcgis.com/qHLhLQrcvEnxjtPr/arcgis/rest/services/UK_COVID_19_Data/FeatureServer/1"), )
for layer in map1.layers:
print(layer)
<FeatureLayer url:"https://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/World_Countries/FeatureServer/0"> <FeatureLayer url:"https://services.arcgis.com/qHLhLQrcvEnxjtPr/arcgis/rest/services/UK_COVID_19_Data/FeatureServer/1">
from arcgis.features import FeatureLayer
## add layer via
# map1.add_layer(covid_map.layers[6])
## or alternatively
map1.add_layer(FeatureLayer("https://services.arcgis.com/qHLhLQrcvEnxjtPr/arcgis/rest/services/UK_COVID_19_Data/FeatureServer/1"), )
for layer in map1.layers:
print(layer)
<FeatureLayer url:"https://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/World_Countries/FeatureServer/0"> <FeatureLayer url:"https://services.arcgis.com/qHLhLQrcvEnxjtPr/arcgis/rest/services/UK_COVID_19_Data/FeatureServer/1">
Using the save
method will create a new web map item, while using the update
method will make updates to the previous web map item:
update(mode=None, item_properties=None, thumbnail=None, metadata=None)
The update
method uses the following parameters:
--------------- --------------------------------------------------------------------
item_properties Optional dictionary. See table below for the keys and values.
--------------- --------------------------------------------------------------------
mode Optional string. Whether to save this map instance as a 2D WebMap,
or a 3D WebScene. Possible strings: ``2D``, ``webmap``, ``3D``, or
``webscene``.
--------------- --------------------------------------------------------------------
thumbnail Optional string. Either a path or URL to a thumbnail image.
--------------- --------------------------------------------------------------------
metadata Optional string. Either a path or URL to the metadata.
--------------- --------------------------------------------------------------------
The update
method will update the Web Map/Web Scene item that was used to create the Map Widget
object. In addition, you are also able to update the item's other properties, thumbnail, and metadata.
webmap_item_properties = {'title':'COVID-19 Cases in UK (Updated 2022-01-06)',
'snippet':'Map created using Python API showing COVID cases in UK',
'tags':['automation', 'world health', 'python']}
new_map_item = map1.update(item_properties = webmap_item_properties,
mode = "2D", thumbnail='./webmap_thumbnail.png')
print(new_map_item)
True
# We can see from the output that the title is being updated:
print(webmap_item)
<Item title:"COVID-19 Cases in UK (Updated 2022-01-06)" type:Web Map owner:arcgis_python>
Creating static maps via take_screenshot
Since version 1.5.2, the ArcGIS API for Python supports the take_screenshot()
method, which will create a screenshot of the current widget view. Note that the method only works in a Jupyter Notebook environment.
take_screenshot(output_in_cell=True, set_as_preview=True, file_path='')
The method takes the following arguments:
output_in_cell
: Optional boolean that defaults to True. This will display the screenshot in the output area of the cell where this function is called.set_as_preview
: Optional boolean that defaults to True. This will set the screenshot as the static image preview in the cell where the map widget is being displayed. Use this flag if you want the generated HTML previews of your notebook to have a map image visible.file_path
: Optional String that defaults to “”. To output the screenshot to a .png file on disk, set this String to a writeable location file path (Ex. file_path = “/path/to/documents/my_screenshot.png”).
In all notebook outputs, each image will be encoded to a base64 data URI and wrapped in an HTML tag. This means that the data for the image lives inside the notebook file itself, allowing for easy sharing of notebooks and generated HTML previews of notebooks.
map1.take_screenshot()
Export live map in a separate HTML file
Another new feature in 1.5.0 is the ability to export the current widget as a stand-alone HTML file. This added functionality provides a new workflow for creating standalone HTML maps that can be shared with anyone. All they need is a web browser!
The cell below will write the current widget to the file specified by a string path. Run the cell, find the file on your disk, and open it in your favorite web browser.
import os
file_dir = os.path.join(os.getcwd(), 'home')
if not os.path.isdir(file_dir):
os.mkdir(file_dir)
file_path = os.path.join(file_dir, 'myWorldMap.html')
map1.export_to_html(file_path)
print("html saved as " + file_path)
html saved as C:\Users\Username\Documents\home\myWorldMap.html
Embedding a live map
The embed
method embeds the current state of the map into the underlying notebook as an interactive HTML/JS/CSS element.
embed(output_in_cell=True, set_as_preview=True)
Its arguments include:
output_in_cell
: Optional boolean that defaults toTrue
. This will display the embedded HTML interactive map in the output area of the cell where this function is called.set_as_preview
: Optional boolean that defaults toTrue
. This will display the embedded HTML interactive map in the cell where the map widget is being displayed. Use this flag if you want the generated HTML previews of your notebook to have an interactive map displayed.
In all notebook outputs, each embedded HTML element will contain the entire map state and all relevant HTML wrapped in an <iframe>
element. This means that the data for the embedded HTML element lives inside the notebook file itself, allowing for easy sharing of notebooks and generated HTML previews of notebooks.
map2 = GIS().map("")
map2
census_item = gis.content.get("85d0ca4ea1ca4b9abf0c51b9bd34de2e")
census_flayer = census_item.layers[0]
map2.add_layer(census_flayer)
map2.embed(output_in_cell=True, set_as_preview=True)
Conclusion
In Part 5 of this guide series, we observed how to consume web maps and web scenes and how to update them. During this process, the examples demonstrated how to modify the map widget using code, how to make copy of an existing item into your account, how to look for basemaps, and how to update layer properties within a web map or web scene. Next in Part 6, we will explore the utility functions to be used with map widgets.