Source code: How to add basemaps in ArcGIS Pro: the gallery, XYZ tiles, WMTS and your own default

Runnable Python scripts, requirements and console output for the tutorial “How to add basemaps in ArcGIS Pro: the gallery, XYZ tiles, WMTS and your own…

Last updated

These are the exact scripts used to produce every figure and number in How to add basemaps in ArcGIS Pro: the gallery, XYZ tiles, WMTS and your own default. Download them, install the requirements into a fresh virtual environment and run them from this folder.

Written and tested by

Run it
python -m venv .venv
.venv\Scripts\activate # macOS/Linux: source .venv/bin/activate
pip install -r requirements.txt
python arcgis_pro_basemaps.py

arcgis_pro_basemaps.py

arcgis_pro_basemaps.py
"""Add basemaps to an ArcGIS Pro map with arcpy: a gallery basemap and an XYZ tile URL.
Run it from the ArcGIS Pro Python Command Prompt with the path of a project:
propy arcgis_pro_basemaps.py C:\\GIS\\Basemaps.aprx
It adds two maps, exports a PNG of Edinburgh from each to an output folder next to
this script, and saves the result as Basemaps_with_tiles.aprx beside the original,
which it leaves unchanged. In the Python window inside Pro, run the same lines with
arcpy.mp.ArcGISProject("CURRENT") and skip the export and save.
Tested with ArcGIS Pro 3.4.0 on 25 November 2026.
"""
import os
import sys
import arcpy
PROJECT = sys.argv[1] if len(sys.argv) > 1 else "CURRENT"
OUT_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "output")
# Edinburgh, in Web Mercator (EPSG:3857), the projection of the tiles.
EDINBURGH = (-362000.0, 7541000.0, -348000.0, 7555000.0)
# Pro accepts the XYZ template in either form. {level}/{col}/{row} is the one Esri documents.
OSM_TILES = "https://tile.openstreetmap.org/{level}/{col}/{row}.png"
OSM_TILES_ZXY = "https://tile.openstreetmap.org/{z}/{x}/{y}.png"
def export_map(aprx, m, name):
"""Put the map in a layout frame over Edinburgh and export it, to prove the tiles draw."""
layout = aprx.createLayout(6, 6, "INCH", f"{name} layout")
frame = layout.createMapFrame(arcpy.Extent(0, 0, 6, 6), m, f"{name} frame")
xmin, ymin, xmax, ymax = EDINBURGH
frame.camera.setExtent(arcpy.Extent(xmin, ymin, xmax, ymax, spatial_reference=arcpy.SpatialReference(3857)))
png = os.path.join(OUT_DIR, f"{name}.png")
layout.exportToPNG(png, resolution=96)
return png
def main():
aprx = arcpy.mp.ArcGISProject(PROJECT)
# 1. A basemap from the gallery, by the name shown under its thumbnail.
gallery = aprx.createMap("Gallery basemap", "MAP")
gallery.addBasemap("Imagery Hybrid")
# 2. An XYZ tile service, added from its URL template. Pro makes it a tiled service layer.
xyz = aprx.createMap("OpenStreetMap XYZ", "MAP")
for lyr in xyz.listLayers():
xyz.removeLayer(lyr)
osm = xyz.addDataFromPath(OSM_TILES)
osm.name = "OpenStreetMap"
# The same service from the {z}/{x}/{y} template, as QGIS users write it.
zxy = xyz.addDataFromPath(OSM_TILES_ZXY)
zxy.name = "OpenStreetMap (z/x/y template)"
zxy.visible = False
for m in (gallery, xyz):
print(m.name, "->", [(l.name, l.isWebLayer) for l in m.listLayers()])
if PROJECT != "CURRENT":
os.makedirs(OUT_DIR, exist_ok=True)
for m, name in ((gallery, "imagery-hybrid-edinburgh"), (xyz, "openstreetmap-xyz-edinburgh")):
png = export_map(aprx, m, name)
print("exported", os.path.basename(png), os.path.getsize(png), "bytes")
copy = os.path.join(os.path.dirname(aprx.filePath), "Basemaps_with_tiles.aprx")
aprx.saveACopy(copy)
print("saved", os.path.basename(copy))
if __name__ == "__main__":
main()