Restore an ArcGIS Online WebMap from Configuration Files using the ArcGIS API for Python

Table of Contents

The Video

Introduction

The ArcGIS API for Python is a powerful Python library that allows users to interact with and automate tasks in ArcGIS Online (or Portal). The API is excellent for programmatically creating, maintaining, updating and extracting components of ArcGIS Online such as WebMaps. In this blog post we will focus on using the ArcGIS API for Python to create a new WebMap based on JSON configuration files.

Check-out the previous blog post Backup ArcGIS Online WebMap Configurations using the ArcGIS API for Python.

Discover the power of automation and script-based operations to efficiently manage content in ArcGIS Online. Throughout this course, you will gain practical, hands-on experience with using the ArcGIS API for Python to perform a wide range of content management tasks with ease, such as creating Folders and Groups and managing content within them.

arcgis modules

The API provides access to your organisations ArcGIS Online via the GIS class in the gis module. This GIS class is the gateway to ArcGIS Online. We need the Map class to create a Map object and the save as a WebMap Item. From the Standard Python Library we import json to aid with getting the JSON from our files, and os to aid with accessing our backup files. 

				
					## provides access to ArcGIS Online
from arcgis.gis import GIS

## required to create a new WebMap item in ArcGIS Online
from arcgis.map import Map

## to read in our json files
import json

## for accessing config files
import os
				
			

Accessing ArcGIS Online

Our first port of call is to access your ArcGIS Online via the GIS class. There are a handful of ways to achieve access, if you are logged into your ArcGIS Online in ArcGIS Pro you can simply use "home", otherwise, another common way is to provide the ArcGIS Online URL, followed by your username and password.
				
					## Access AGOL
agol = GIS("home")
				
			
				
					## Access AGOL
agol = GIS(
    url = "https://your_organisation.maps.arcgis.com/",
    username = "Your_Username",
    password = "Your_Password"
)
				
			

Required inputs

We require two user inputs; the folder where the backup files reside, and the name of the new webmap to craete.

				
					## the folder that contains the json config files
archive_folder = "path/to/archive/folder"

## the name for the new webmap
new_webmap_name = "NEW_WEBMAP_NAME"
				
			

The JSON Filepaths

We have two JSON files to consider, one that contains the Item Settings and the other that contains the WebMap Definition.

				
					## the filepath to the Settings json file
json_settings_file = [f.path for f in os.scandir(archive_folder) if f.name.startswith("Settings")][0]

## the fielpath to the WebMap Definition json file
json_definition_file = [f.path for f in os.scandir(archive_folder) if f.name.startswith("Definition")][0]
				
			

Get the Item Settings

We load our item settings from the JSON file and workout the thumbnail filepath.

				
					## open the json file for the settings
with open(json_settings_file) as wm_json:
    wm_settings = json.load(wm_json)

## get the thumbnail name
thumbnail_name = wm_settings["thumbnail"].split("/")[-1]

## get teh thumbnail filepath
thumbnail_path = os.path.join(archive_folder, thumbnail_name)
				
			

Set the WebMap Item Properties

After this step, we will create a Map object and save it as a WebMap Item in ArcGIS Online. The save() operation takes a dictionary describing item properties that we see below. We will also attempt to put the WebMap in the original folder.

				
					## set the item properties dictionary
wm_item_dict = {
    "title" : new_webmap_name,
    "description" : wm_settings["description"],
    "snippet" : wm_settings["snippet"],
    "tags" : wm_settings["tags"],
    "typeKeywords" : wm_settings["typeKeywords"],
    "extent" : wm_settings["extent"],
    "spatialReference" : wm_settings["spatialReference"],
    "accessInformation" : wm_settings["accessInformation"],
    "licenseInfo" : wm_settings["licenseInfo"],
    "culture" : wm_settings["culture"],
    "access" : wm_settings["access"],
    "commentsEnabled" : wm_settings["commentsEnabled"],
    "categories" : wm_settings["categories"],
}

## get the folder
if wm_settings["owner"] == agol.users.me.username:
    folder = wm_settings["ownerFolder"]
else:
    folder = None
				
			

Create the Map Object and Save as a WebMap Item

We use the properties from the previous step to save the Map object as a WebMap Item in ArcGIS Online

				
					## create the WebMap object
webmap = Map()

## save the WebMap as an Item of Content
wm_item = webmap.save(
    item_properties=wm_item_dict,
    thumbnail = thumbnail_path,
    folder = folder
)
				
			

Update Some More Item Properties

We couldn’t handle delete protection, content status, or favorite in the previous step so we cater for that here.

				
					## Delete Protection
wm_item.protect(enable=wm_settings["protected"])

## Content Status
if "contentStatus" in wm_settings:
    wm_item.content_status = wm_settings["contentStatus"]

## Favorite
wm_item.favorite = wm_settings["favorite"]
				
			

Update/Apply the WebMap Definition

If we left it as is we would have an empty WebMap with a default basemap, we don;t want that, so here take the WebMap definition from the JSON file and apply it to the WebMap item. Our WebMap has been created/restored.

				
					## apply the definition from the json file
with open(json_definition_file) as wm_json:
    wm_def = json.load(wm_json)
    item_properties = {"text":wm_def}
    wm_item.update(item_properties=item_properties)
				
			

Geospatial Professionals, GIS Analysts, and enthusiasts will discover the power of automation and script-based operations to efficiently interact and update WebMaps in ArcGIS Online. Throughout this course, you will gain practical, hands-on experience in leveraging the ArcGIS API for Python to perform a wide range of WebMap tasks with ease.

We will dissect WebMaps for all that they are and by the end of this course you will be comfortable with manipulating the JSON, which is the behind the scenes configuration of a WebMap, to produce scripts for workflows where there is currently no API Python method, such as grouping layers.

All the code in one place

You can find the entire code workflow below with links to important components in the documentation that were used.

				
					## provides access to ArcGIS Online
from arcgis.gis import GIS

## required to create a new WebMap item in ArcGIS Online
from arcgis.map import Map

## to read in our json files
import json

## for accessing config files
import os

################################################################################
## API Reference Links:
##  https://developers.arcgis.com/python/latest/api-reference/arcgis.gis.toc.html#gis
##  https://developers.arcgis.com/python/latest/api-reference/arcgis.map.toc.html#map
##  https://developers.arcgis.com/python/latest/api-reference/arcgis.map.toc.html#arcgis.map.Map.save
##  https://developers.arcgis.com/python/latest/api-reference/arcgis.gis.toc.html#item
##  https://developers.arcgis.com/python/latest/api-reference/arcgis.gis.toc.html#arcgis.gis.Item.protect
##  https://developers.arcgis.com/python/latest/api-reference/arcgis.gis.toc.html#arcgis.gis.Item.favorite
##  https://developers.arcgis.com/python/latest/api-reference/arcgis.gis.toc.html#arcgis.gis.Item.content_status
##  https://developers.arcgis.com/python/latest/api-reference/arcgis.gis.toc.html#arcgis.gis.Item.update
##
## API Versions: 2.1.0.2, 2.2.0.1, 2.3.0, 2.4.0
##
################################################################################

################################################################################
## ACCESS ARCGIS ONLINE ########################################################

agol = GIS("home")

################################################################################
## USER INPUTS #################################################################

## the folder that contains the json config files
archive_folder = "path/to/archive/folder"

## the name for the new webmap
new_webmap_name = "NEW_WEBMAP_NAME"

################################################################################
## THE JSON FILES ##############################################################

## the filepath to the Settings json file
json_settings_file = [f.path for f in os.scandir(archive_folder) if f.name.startswith("Settings")][0]

## the fielpath to the WebMap Definition json file
json_definition_file = [f.path for f in os.scandir(archive_folder) if f.name.startswith("Definition")][0]

################################################################################
## GET THE ITEM SETTINGS #######################################################

## open the json file for the settings
with open(json_settings_file) as wm_json:
    wm_settings = json.load(wm_json)

## get the thumbnail name
thumbnail_name = wm_settings["thumbnail"].split("/")[-1]

## get teh thumbnail filepath
thumbnail_path = os.path.join(archive_folder, thumbnail_name)

################################################################################
## SET THE NEW WEBMAP ITEM PROPERTIES ##########################################

## set the item properties dictionary
wm_item_dict = {
    "title" : new_webmap_name,
    "description" : wm_settings["description"],
    "snippet" : wm_settings["snippet"],
    "tags" : wm_settings["tags"],
    "typeKeywords" : wm_settings["typeKeywords"],
    "extent" : wm_settings["extent"],
    "spatialReference" : wm_settings["spatialReference"],
    "accessInformation" : wm_settings["accessInformation"],
    "licenseInfo" : wm_settings["licenseInfo"],
    "culture" : wm_settings["culture"],
    "access" : wm_settings["access"],
    "commentsEnabled" : wm_settings["commentsEnabled"],
    "categories" : wm_settings["categories"],
}

## get the folder
if wm_settings["owner"] == agol.users.me.username:
    folder = wm_settings["ownerFolder"]
else:
    folder = None

################################################################################
## CREATE MAP OBJECT AND SAVE AS WEBMAP ITEM ###################################

## create the WebMap object
webmap = Map()

## save the WebMap as an Item of Content
wm_item = webmap.save(
    item_properties=wm_item_dict,
    thumbnail = thumbnail_path,
    folder = folder
)

################################################################################
## UPDATE THE WEBMAP ITEM OBJECT PROPERTIES ####################################

## Delete Protection
wm_item.protect(enable=wm_settings["protected"])

## Content Status
if "contentStatus" in wm_settings:
    wm_item.content_status = wm_settings["contentStatus"]

## Favorite
wm_item.favorite = wm_settings["favorite"]

################################################################################
## UPDATE THE WEBMAP DEFINITION ################################################

## apply the definition from the json file
with open(json_definition_file) as wm_json:
    wm_def = json.load(wm_json)
    item_properties = {"text":wm_def}
    wm_item.update(item_properties=item_properties)

################################################################################
print("\nSCRIPT COMPLETE")

				
			

Leave a Comment

Your email address will not be published. Required fields are marked *