2

I can load WMS layers by coding on QGIS Python console, those are the codelines:

# Google Satelite URL wms 
url = 'type=xyz&url=http%3A//mt1.google.com/vt/lyrs%3Dy%26x%3D%7Bx%7D%26y%3D%7By%7D%26z%3D%7Bz%7D'
# Constructing layer thoughout wms url
layer = QgsRasterLayer(url, 'Google Satelite', 'wms')
# Add this layer to the current project
QgsProject.instance().addMapLayer(layer)

I use QGIS 3.16 in a Ubuntu Linux VM. When I type these line of code on QGIS console this is what I get:

enter image description here

The problem occur when I try execute the same lines in a standalone script (using PyCharm IDE) and after load WMS layer I save a copy of this project, those are the lines i run in PyCharm standalone script:

from qgis.core import QgsApplication, QgsProject
import requests

# QGIS instalation folder path
QgsApplication.setPrefixPath('/usr/qgis', True)

# QGIS reference
qgs = QgsApplication([], True)

# Initiating QGIS
qgs.initQgis()

# Creating a variable to my qgis project
project = QgsProject.instance()

# Read a specific project
### DOWNLOAD https://drive.google.com/file/d/1Pqk5W3y47Mt4rjlN1QzGoarBGGl6B3og/view?usp=sharing
project.read('path/to/qgisproject/with/0001_tester/layout/0001.qgz')

# google satelite image WMS url
# class googleWMS:
url = "http://mt1.google.com/vt/lyrs=y&x={x}&y={y}&z={z}"
uri = "type=xyz&url="+requests.utils.quote(url)

# Create a layer from WMS url
tms_layer = QgsRasterLayer(uri, "Google Satelite", 'wms')

# Add this layer into project
QgsProject.instance().addMapLayer(tms_layer)

# Save the project in another location
QgsProject.instance().write('path/to/qgisproject.qgz')

The script can load the layer into qgis project and save the file, but the WMS layer is broken... No image is loaded to workspace.

enter image description here

5
  • Not able to reproduce. I run your code in a Python script I execute in PyQGIS Python console. Then, in recent project, I click on the newly created project ant it works.
    – ThomasG77
    Commented Aug 25, 2021 at 0:04
  • Yes, on qgis python console those lines work just fine... My issue is when i try use a standalone script, in pycharm Commented Aug 25, 2021 at 10:30
  • Sorry but your screenshot was misleading me
    – ThomasG77
    Commented Aug 25, 2021 at 10:35
  • I really sorry, it is because i did run the same lines in a standalone script, using pycharm IDE, and in this case i have the right side of my screenshoot as result. I am gonna edit this question in order to make it more clear. Commented Aug 25, 2021 at 10:39
  • what is the result if you print that line (in your script standalone)? tms_layer.isValid() if is False the url params is bad or the wms provider dont load corrrect after you load the providers in this line: qgs.initQgis() you need to print the providers with: print(QgsProviderRegistry.instance().providerList()) Check if the wms provider is there Commented Sep 9, 2021 at 18:52

1 Answer 1

0

As mentioned in the comments, if you run QgsProviderRegistry.instance().providerList() in the PyCharm version, you will be able to notice that the provider wms is not loaded. This happens because the specified path is not correct and the Standalone version running under PyCharm does not have access to wms.

To fix this, just run in QGIS Python Console:

QgsApplication.showSettings()

Then an associated path called QGIS_PREFIX_PATH will appear. Finally, just replace in the PyCharm code the qgis_prefix for the path indicated above.

qgs = QgsApplication([], True)
qgis_prefix = "YOUR PATH"
qgs.setPrefixPath(qgis_prefix, True)
qgs.initQgis()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.