0

I am using the following code to check if a WMS raster is valid.

urlWithParams = "url=crs=EPSG:4326&format=image/png&layers=tasmania&styles&url=https://demo.geo-solutions.it/geoserver/ows"
rlayer = QgsRasterLayer(urlWithParams, 'some layer name', 'wms')
rlayer.isValid()

and it return True.

enter image description here

But when I use the same code in my standalone pyQGIS Application. It returns False. enter image description here I don't know why it happen. Could anyone help me? Thank very much.


Here is my whole code:

#coding=utf-8
import sys, os
import processing
from processing.core.Processing import Processing

from osgeo import gdal, osr

from qgis.core import *
from qgis.gui import *
from qgis.utils import *

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5 import *

# add below environ to access qgis / qt library  #
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = r'C:\Program Files\QGIS 3.12\apps\Qt5\plugins'
os.environ['PATH'] += r';C:\Program Files\QGIS 3.12\apps\qgis\bin;C:\Program Files\QGIS 3.12\apps\Qt5\bin'

os.environ['GDAL_DATA'] = "C:\OSGeo4W64\share\gdal"
os.environ['GDAL_DRIVER'] = "C:\OSGeo4W64\bin\gdalplugins"
os.environ['PROJ_LIB'] = "C:\Program Files\QGIS 3.12\share\proj"
os.environ['PYTHONPATH'] = "C:\OSGeo4W64\share\gdal"
sys.path.extend([r'C:\Program Files\QGIS 3.12\apps\qgis\python',r'C:\Program Files\QGIS 3.12\apps\Python37\Lib\site-packages',r'C:\OSGeo4W64\apps\Python37\lib\site-packages\pyproj\proj_dir\share\proj'])

QgsApplication.setPrefixPath(os.environ.get("QGIS_PREFIX_PATH", "C:\Program Files\QGIS 3.12\apps\qgis"), True)
qgs = QgsApplication([], True)
qgs.initQgis()

urlWithParams = "url=crs=EPSG:4326&format=image/png&layers=tasmania&styles=&url=https://demo.geo-solutions.it/geoserver/ows"    
rlayer = QgsRasterLayer(urlWithParams, 'wms layer', 'wms')
if not rlayer.isValid():
    print("Layer failed to load!")

canvas=QgsMapCanvas()
canvas.setExtent(rlayer.extent())
canvas.setLayers([rlayer])

canvas.show()

and if I use just like:

self.rlayer = QgsRasterLayer("D:\myJpg.jpg", "myJpg")

It could work. So I don't know where is the problem.Could anyone help me?


Here is my whole code:

#coding=utf-8
import sys, os
import processing
from processing.core.Processing import Processing

from osgeo import gdal, osr

from qgis.core import *
from qgis.gui import *
from qgis.utils import *

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5 import *

# add below environ to access qgis / qt library  #
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = r'C:\Program Files\QGIS 3.12\apps\Qt5\plugins'
os.environ['PATH'] += r';C:\Program Files\QGIS 3.12\apps\qgis\bin;C:\Program Files\QGIS 3.12\apps\Qt5\bin'

os.environ['GDAL_DATA'] = "C:\OSGeo4W64\share\gdal"
os.environ['GDAL_DRIVER'] = "C:\OSGeo4W64\bin\gdalplugins"
os.environ['PROJ_LIB'] = "C:\Program Files\QGIS 3.12\share\proj"
os.environ['PYTHONPATH'] = "C:\OSGeo4W64\share\gdal"
sys.path.extend([r'C:\Program Files\QGIS 3.12\apps\qgis\python',r'C:\Program Files\QGIS 3.12\apps\Python37\Lib\site-packages',r'C:\OSGeo4W64\apps\Python37\lib\site-packages\pyproj\proj_dir\share\proj'])

QgsApplication.setPrefixPath(os.environ.get("QGIS_PREFIX_PATH", "C:\Program Files\QGIS 3.12\apps\qgis"), True)
qgs = QgsApplication([], True)
qgs.initQgis()

urlWithParams = "url=crs=EPSG:4326&format=image/png&layers=tasmania&styles=&url=https://demo.geo-solutions.it/geoserver/ows"    
rlayer = QgsRasterLayer(urlWithParams, 'wms layer', 'wms')
if not rlayer.isValid():
    print("Layer failed to load!")

canvas=QgsMapCanvas()
canvas.setExtent(rlayer.extent())
canvas.setLayers([rlayer])

canvas.show()

and if I use just like:

self.rlayer = QgsRasterLayer("D:\myJpg.jpg", "myJpg")

It could work. So I don't know where is the problem.Could anyone help me?

2
  • How do you set up your standalone PyQGIS application? We need to see more code to discard issues like this. – Germán Carrillo Apr 8 '20 at 12:23
  • in url=crs=EPSG:4326..., remove url= to get something like "crs=EPSG:4326... – ThomasG77 Apr 9 '20 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.