Bug report #18078

Adding Google Maps with PyQGIS

Added by Jadwiga Trojak over 2 years ago. Updated over 2 years ago.

Status:Closed
Priority:Normal
Assignee:-
Category:PyQGIS Console
Affected QGIS version:master Regression?:No
Operating System:Windows 7 Professional 64bit Easy fix?:No
Pull Request or Patch supplied:No Resolution:invalid
Crashes QGIS or corrupts data:No Copied to github as #:25974

Description

In QGIS 2.18.16 I'm using the following code (and it works):

import requests
service_url = "https://mt1.google.com/vt/lyrs=s&x={x}&y={y}&z={z}"
service_uri = "type=xyz&zmin=0&zmax=21&url="+requests.utils.quote(service_url)
tms_layer = iface.addRasterLayer(service_uri, "Google Sat", "wms")

It doesn't work form QGIS 2.99. I described the issue on https://gis.stackexchange.com/questions/270871/adding-google-maps-with-pyqgis,
and xunilk suggested that:

It looks as an unsolved visualization issue in 2.99 QGIS version. Commands run ok at Python Console but tms_layer (effectively loaded as Google_Sat) cannot be visualized. It has extent with these (xmin, xmax, ymin, ymax) parameters -20037508.342789244 20037508.342789244 -20037508.342789255 20037508.34278924.

History

#1 Updated by Nyall Dawson over 2 years ago

  • Resolution set to invalid
  • Status changed from Open to Closed

This works for me:

import requests
service_url = "mt1.google.com/vt/lyrs=s&x={x}&y={y}&z={z}" 
service_uri = "type=xyz&zmin=0&zmax=21&url=https://"+requests.utils.quote(service_url)
tms_layer = iface.addRasterLayer(service_uri, "Google Sat", "wms")

The difference is that the 'https://' portion of the url is not passed to requests.utils.quote. Possibly something has changed in that method in Python3 which has changed this behavior.

In any case, not a QGIS issue.

Also available in: Atom PDF