TransWikia.com

Adding a raster layer on map canvas in PyQGIS

Geographic Information Systems Asked on June 6, 2021

canvas = QgsMapCanvas()
canvas.show()
canvas.setCanvasColor(Qt.white)
canvas.enableAntiAliasing(True)
vlayer = QgsVectorLayer('testdata/airports.shp', "Airports layer", "ogr")
canvas.setExtent(vlayer.extent())
canvas.setLayers([vlayer])

tms = 'type=xyz&url=https://tile.openstreetmap.org/{z}/{x}/{y}.png&zmax=19&zmin=0'
layer = QgsRasterLayer(tms,'OSM', 'wms')
canvas.setLayers([layer ])

Above code works fine with vector layer but when I am trying to QgsRasterLayer
show cyan colour.

enter image description here

How can I add raster layer on map canvas?

One Answer

That cyan color is probably The Atlantic Ocean. Try the following way. It worked for me.

test_data = 'testdata/airports.shp'

canvas = QgsMapCanvas()
vlayer = QgsVectorLayer(test_data, "test", "ogr")
canvas.setCanvasColor(Qt.white)
canvas.enableAntiAliasing(True)

tms = 'type=xyz&url=https://tile.openstreetmap.org/{z}/{x}/{y}.png&zmax=19&zmin=0'
layer = QgsRasterLayer(tms,'OSM', 'wms')

canvas.setLayers([vlayer, layer])
canvas.setExtent(vlayer.extent())

canvas.show()

enter image description here

Correct answer by Kadir Şahbaz on June 6, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP