Select to view content in your preferred language

Add Google Maps as a basemap via Python

231
6
Jump to solution
Tuesday
AurelienRobert
New Contributor

Hello everyone,

I'm creating customs webmaps  with points and markers using Python.

I'd like to have Google Maps' satellite imagery (https://mt1.google.com/vt/lyrs=y&x={col}&y={row}&z={level}) as a basemap. In the online MapViewer it's easy to add a tile layer from an url but I don't see how to do it through Python.

The guide to use the widget (https://developers.arcgis.com/python/latest/guide/using-the-map-widget/) doesn't mention how to add a layer from an url as basemap, and I didn't find any solution in the documentation or in this questions board. 

I've also tried manually creating a layer in the My Content folder of my Arcgis Location Platform but the add tile and add layer features are different from that in the online MapViewer.

Any help appreciated.

Thank you

 

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
AurelienRobert
New Contributor

Replit found the solution: call the Google Maps satellite view as a basemap layer when creating a webmap object.

# Create web map JSON with Google satellite as basemap
sat_url = "https://mt1.google.com/vt/lyrs=y&x={col}&y={row}&z={level}"
webmap_json = {
"operationalLayers": operational_layers,
"baseMap": {
"baseMapLayers": [{
"id": "google_satellite",
"title": "Google Satellite",
"url": sat_url,
"layerType": "WebTiledLayer",
"templateUrl": sat_url.replace("{col}", "{x}").replace("{row}", "{y}").replace("{level}", "{z}"),
"visibility": True,
"opacity": 1,
"subDomains": ["mt0", "mt1", "mt2", "mt3"],
"copyright": "Google"
}],
"title": "Google Satellite"
},
"spatialReference": {
"wkid": 102100,
"latestWkid": 3857
},
"initialExtent": {
"xmin": -9548000,
"ymin": 1166000,
"xmax": -9542000,
"ymax": 1172000,
"spatialReference": {"wkid": 102100}
},
"version": "2.22"
}

# Create the web map item
create_item_url = "https://www.arcgis.com/sharing/rest/content/users/self/addItem"

item_data = {
'f': 'json',
'token': access_token,
'title': 'title1',
'type': 'Web Map',
'tags': 'test',
'snippet': 'Property map with 6 points',
'text': json.dumps(webmap_json)
}

response = requests.post(create_item_url, data=item_data)

View solution in original post

0 Kudos
6 Replies
HaydenWelch
MVP Regular Contributor

You can use the `addDataFromPath` method of a `Map`:

import arcpy

prj = arcpy.mp.ArcGISProject('CURRENT')
m = prj.listMaps('MapName')[0]

lay: arcpy.mp.Layer = m.addDataFromPath(r'https://mt1.google.com/vt/lyrs=y&x={col}&y={row}&z={level}', 'AUTOMATIC')
lay.name = 'Google Hybrid'
prj.save()
0 Kudos
AurelienRobert
New Contributor

Thank you Hayden, unfortunately Cursor tells me arcpy can only be imported when using Arcgis Pro, which I do not have as I use a simple Arcgis online account.

0 Kudos
HaydenWelch
MVP Regular Contributor

In that case you'll have to use the Arcgis API for Python. Arcpy is mostly for managing a Pro instance and wrangling tools/data.

The general flow should be the same though.

0 Kudos
TonyAlmeida
MVP Regular Contributor

This works for me, but it's not google maps.

import arcpy
import os

# Get project and map
prj = arcpy.mp.ArcGISProject('CURRENT')
m = prj.listMaps()[0]  # Gets the first map

# Path to connection file 
conn_path = os.path.join(os.environ['USERPROFILE'], 'Documents', 'ArcGIS', 'Connections', 'GoogleMaps.wmts')

try:
    # Add the connection
    lyr = m.addDataFromPath(conn_path)
    lyr.name = "Google Satellite"
    
    # Refresh view
    aprx = arcpy.mp.ArcGISProject('CURRENT')
    #aprx.save()
    print("Successfully added Esri World Imagery")
    
except Exception as e:
    print(f"Error: {str(e)}")
    # Alternative legal basemap if Google fails
    world_imagery = m.addDataFromPath(r"https://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer")
    world_imagery.name = "Esri World Imagery"

 

 

0 Kudos
AurelienRobert
New Contributor

Thank you Tony, unfortunately Cursor tells me arcpy can only be imported when using Arcgis Pro, which I do not have as I use a simple Arcgis online account.

0 Kudos
AurelienRobert
New Contributor

Replit found the solution: call the Google Maps satellite view as a basemap layer when creating a webmap object.

# Create web map JSON with Google satellite as basemap
sat_url = "https://mt1.google.com/vt/lyrs=y&x={col}&y={row}&z={level}"
webmap_json = {
"operationalLayers": operational_layers,
"baseMap": {
"baseMapLayers": [{
"id": "google_satellite",
"title": "Google Satellite",
"url": sat_url,
"layerType": "WebTiledLayer",
"templateUrl": sat_url.replace("{col}", "{x}").replace("{row}", "{y}").replace("{level}", "{z}"),
"visibility": True,
"opacity": 1,
"subDomains": ["mt0", "mt1", "mt2", "mt3"],
"copyright": "Google"
}],
"title": "Google Satellite"
},
"spatialReference": {
"wkid": 102100,
"latestWkid": 3857
},
"initialExtent": {
"xmin": -9548000,
"ymin": 1166000,
"xmax": -9542000,
"ymax": 1172000,
"spatialReference": {"wkid": 102100}
},
"version": "2.22"
}

# Create the web map item
create_item_url = "https://www.arcgis.com/sharing/rest/content/users/self/addItem"

item_data = {
'f': 'json',
'token': access_token,
'title': 'title1',
'type': 'Web Map',
'tags': 'test',
'snippet': 'Property map with 6 points',
'text': json.dumps(webmap_json)
}

response = requests.post(create_item_url, data=item_data)

0 Kudos