In my QGIS python plugin I want to add a background map (if the user haven't added one), hence I think I missing something in the url for the Tiles/wms service.. (the function returns invalid layer
) Any suggestions?
sources = [layer.source() for layer in QgsProject.instance().mapLayers().values()]
print(sources)
source_found = False
for source in sources:
if 'xyz&url' in source:
source_found = True
print('found')
if not source_found:
print('adding')
urlWithParams = 'type=xyz&url=http://a.tile.openstreetmap.org/%7Bz%7D/%7Bx%7D/%7By%7D.png&zmax=19&zmin=0&crs=EPSG3857'
rlayer = QgsVectorLayer(urlWithParams, 'OpenStreetMap', 'wms')
if rlayer.isValid():
QgsProject.instance().addMapLayer(rlayer)
else:
print('invalid layer')