2
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?

3
  • please add the code where you create the raster layer
    – Ian Turton
    Commented Apr 28, 2021 at 8:31
  • @BenW after I modified code raster shows cyan color only Commented Apr 28, 2021 at 9:47
  • 1
    You need to delete the first canvas.setLayers() and add vlayer to the second one like this "canvas.setLayers([vlayer,layer])" You raster is cyan because I think you're in the water, your vlayer (wich is your extent) must be in Mercator (EPSG:3857) for your map to focus at the right location.
    – JULESG
    Commented Apr 28, 2021 at 12:22

1 Answer 1

4

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

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.