Table of Contents
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, and updating webmaps in ArcGIS Online. In this post we will focus on the first of those – creating! We will create a simple and empty webmap in ArcGIS Online.
arcgis modules
gis
module. This GIS
class is the gateway to ArcGIS Online. We will need to import the WebMap
class to create a WebMap
object. The WebMap
class is a part of the mapping
module. This mapping
module provides components for working with 2D and 3D maps and scenes, and also includes classes for map layers such as the MapFeatureLayer
, MapImageLayer
, and VectorTileLayer
.
## import GIS which provides the gateway entry to your AGOL
from arcgis.gis import GIS
## import WebMap from the mapping module
from arcgis.mapping import WebMap
Accessing ArcGIS Online
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"
)
Create a WebMap Object
WebMap
object is as straightforward as below. It is important to note that, here, we are merely creating a WebMap
object in-memory. The WebMap has not yet been created as a WebMap content item in ArcGIS Online. We will save this object later in the workflow.
## Instantiate a WebMap object
webmap = WebMap()
Define the WebMap Properties
Next, we want to define some of our WebMap properties such as the name (title) of the WebMap, a description, a summary, some tags, the initial extent, a basemap, and sure why not a thumbnail to be fancy. The WebMap
object has a property called basemap
to aid set our basemap of choice. We set a variable to define the path to our thumbnail which is a png file, and we create a dictionary with defined key, value pairs to aid with the other properties mentioned. The basemap
property is the only one of these that is directly accessible through the WebMap
object. The other properties are set on the WebMap item object once we save our WebMap object and it is then created and available in ArcGIS Online.
## Set the Basemap
webmap.basemap = "gray-vector"
## WebMap thumbnail filepath
thumbnail = r"C:\path\to\thumbnail.png"
## WebMap Item Properties
wm_item_dict = {
"title" : "A WebMap Item Created with the API",
"snippet" : "This is a summary created with the API",
"description" : "This is a description created with the API",
"tags" : "python,injected,tags", # ["python", "injected", "tags"]
"extent" : {'xmin': -9.1, 'ymin': 53.255, 'xmax': -9.0, 'ymax': 53.3, 'spatialReference': {'wkid': 4326}}, # Galway
}
Save the WebMap Object as a Content Item
WebMap
object as WebMap content item. The WebMap
object has a method called save
to perform this action for us. Our dictionary defining the WebMap Item properties is the only required parameter, but we also want to use the thumbnail
parameter.
## Save the WebMap to AGOL as a content item
webmap.save(
item_properties = wm_item_dict,
thumbnail = thumbnail
)
Check it out in ArcGIS Online
The WebMap Content Item has been created in our home folder – this is the folder named the same as your username.
![](https://learn.finaldraftmapping.com/wp-content/uploads/2024/03/fdmga_webmap_agol_1-1024x125.png)
We can see all the components that we set; the title, description, summary, tags, thumbnail, and basemap.
![](https://learn.finaldraftmapping.com/wp-content/uploads/2024/03/fdmga_webmap_agol_2-1024x824.png)
And if we open up the WebMap we can see that it contains our gray-vector basemap as the only layer and zoomed to Galway in Ireland. We have successfully created a WebMap content item in ArcGIS Online using the ArcGIS API for Python to create a WebMap
object and saving with desired properties.
![](https://learn.finaldraftmapping.com/wp-content/uploads/2024/03/fdmga_webmap_agol_3-1024x529.png)
Unlock the full potential of ArcGIS Online by mastering the art of efficient Content Management with the ArcGIS API for Python. In this comprehensive course, you will embark on a journey to streamline your geospatial workflows, enhance data organization, and maximize the impact of your ArcGIS Online platform.
Geospatial Professionals, GIS Analysts, Data Managers, and enthusiasts will 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 in leveraging the ArcGIS API for Python to perform a wide range of content management tasks with ease.
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.
from arcgis.gis import GIS
from arcgis.mapping import WebMap
################################################################################
## API Reference Links:
## https://developers.arcgis.com/python/api-reference/arcgis.gis.toc.html#gis
## https://developers.arcgis.com/python/api-reference/arcgis.mapping.toc.html#webmap
## https://developers.arcgis.com/python/api-reference/arcgis.mapping.toc.html#arcgis.mapping.WebMap.basemap
## https://developers.arcgis.com/python/api-reference/arcgis.mapping.toc.html#arcgis.mapping.WebMap.save
##
## Description:
## Create a WebMap with a selected basemap and without any layers.
##
## API Version: 2.2.0.1
##
################################################################################
## Access AGOL
agol = GIS("home")
## Instantiate a WebMap object"""
webmap = WebMap()
## Set the Basemap
webmap.basemap = "gray-vector"
## WebMap thumbnail path
thumbnail = r"C:\path\to\thumbnail.png"
## WebMap Item Properties
wm_item_dict = {
"title" : "A WebMap Item Created with the API",
"snippet" : "This is a summary created with the API",
"description" : "This is a description created with the API",
"tags" : "python,injected,tags", # ["python", "injected", "tags"]
"extent" : {'xmin': -9.1, 'ymin': 53.255, 'xmax': -9.0, 'ymax': 53.3, 'spatialReference': {'wkid': 4326}}, # Galway
}
## Save the WebMap to AGOL as a content item
wm_item = webmap.save(item_properties=wm_item_dict, thumbnail=thumbnail)
1 thought on “Create a WebMap in ArcGIS Online with the ArcGIS API for Python”
Pingback: What BaseMaps are Available for my WebMaps in ArcGIS Online using the ArcGIS API for Python? – FDM Geospatial Academy