(I have since found a solution and added it as an answer)
I am currently creating my first Plugin for QGIS using the 'Plugin Builder'Plugin, QtDesigner and Python. At this point it's just supposed to create a raster layer from WMS for the extent of a chosen layer and a 1km zone around it.
The wms_url I get via the print command works both in the QGIS WMS menu and the browser (example), the layer created using it in the next line isn't valid however and I can't figure out why.
- Imports etc: (The last 3 lines are from the top answer of this question as it seems to be the same issue, it didn't solve anything for me though.)
from os import system
from qgis.PyQt.QtCore import QSettings, QTranslator, QCoreApplication
from qgis.PyQt.QtGui import *
from qgis.PyQt.QtWidgets import QAction, QFileDialog
from qgis.core import *
import processing
from .resources import *
from .flight_planner_dialog import UASFlightPlannerDialog
import os.path
qgis_prefix="C:\\Program Files\\QGIS 3.26.3\\apps\\qgis"
QgsApplication.setPrefixPath(qgis_prefix, True)
QgsApplication.initQgis()
(I skipped some stuff generated by the Plugin Builder (and I think QtDesigner) here)
- Context for the variables in the WMS URL (shouldn't be the issue, but just in case):
flightpath = self.dlg.mMapLayerComboBox.currentLayer()
destCrs = QgsCoordinateReferenceSystem(25832)
flightpathCrs = QgsCoordinateReferenceSystem(flightpath.crs())
ext = flightpath.extent()
if destCrs != flightpathCrs:
tform = QgsCoordinateTransform(flightpathCrs, destCrs, QgsProject.instance())
ext = tform.transform(ext)
bboxwest = round(ext.xMinimum())-1000
bboxsouth = round(ext.yMinimum())-1000
bboxeast = round(ext.xMaximum())+1000
bboxnorth = round(ext.yMaximum())+1000
height = bboxnorth-bboxsouth
width = bboxeast-bboxwest
- The part that doesn't work:
wms_url = 'url=https://uas-betrieb.de/geoservices/dipul/wms?service=WMS&version=1.3.0&request=GetMap&layers=dipul:wohngrundstuecke&bbox='+str(bboxwest)+','+str(bboxsouth)+','+str(bboxeast)+','+str(bboxnorth)+'&width='+str(width)+'&height='+str(height)+'&srs=EPSG:25832&transparent=true&format=image/png&styles='
print (wms_url)
rgeozones = QgsRasterLayer(wms_url, 'rgeozones', 'wms')
print(rgeozones.isValid())
url=from the beginning of thewms_urlstring. – Matt Nov 10 '22 at 17:49transparentareTRUEorFALSEi.e. case sensitive, the default value is false, sotransparent=truemay give a non-transparent image – nmtoken Nov 10 '22 at 17:53CRSnotSRSto define the coordinate reference system – nmtoken Nov 10 '22 at 17:56url=just adds a error message for me. Translated to english it would be something like: "Property request failed: The protocol "" is unknown" From what I could find online,url=is for some reason needed for WMS, but not for WFS.@nmtoken thank you as well, I applied your suggestions, however it didn't solve the problem.
– JannisM Nov 11 '22 at 08:51https://uas-betrieb.de/geoservices/dipul/wmsaccording to the GetCapabilities response, there is no layer calleddipul:wohngrundstueckethere is a layer calledwohngrundstueckeThat layer doesn't support the CRS that you are requesting, you can use only one ofEPSG:4326,EPSG:3857,EPSG:7789,CRS:84. – nmtoken Nov 11 '22 at 11:56https://uas-betrieb.de/geoservices/dipul/ows?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetMap&BBOX=653740.1014785182197,5993688.218827254139,1673555.295759583823,7371373.09475582093&CRS=EPSG:3857&WIDTH=486&HEIGHT=657&LAYERS=wohngrundstuecke&STYLES=&FORMAT=image/png&DPI=96&MAP_RESOLUTION=96&FORMAT_OPTIONS=dpi:96&TRANSPARENT=TRUEyour width and height calculations would result in an image that was order of magnitudes too big, and I would guess never returned by the server – nmtoken Nov 11 '22 at 12:08As for your suggestions: According to the how-to part of their website, you are supposed to add
– JannisM Nov 14 '22 at 08:26dipul:in front of the layer names, though I have found it working both ways. I wondered about the CRS before, but again, the server seems to handle it just fine as it gives out the correct area when requested via a web browser. Cutting down on width and height doesn't change anything either.dipul:and I failed to give the information necessary to figure out the other problem. – JannisM Nov 16 '22 at 15:19key=value¬&key=valuewhere=valueis optional, so an empty styles parameter would bestyles¬&styles=– nmtoken Nov 16 '22 at 15:37