TransWikia.com

PyQGIS: How to change CRS of OSM

Geographic Information Systems Asked by HHasemi on January 19, 2021

In a PyQGIS standalone script, I tried to change the CRS of an OpenStreetMap loaded as a Raster Layer.

I want to change the OSM from EPSG:3857 to EPSG:4326 and display it on Canvas, but the display on Canvas does not change even if I use the setCrs() method of PyQGIS.

Below is the PyQGIS standalone script.

import os
import sys

from osgeo import gdal, ogr, osr

from qgis.core import *
from qgis.gui import *
from PyQt5 import uic
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

class MainWindow(QMainWindow):
    def __init__(self, parent = None):
        super().__init__(parent)
        self.ui = uic.loadUi("ui/MainWindow.ui", self)

        self.canvas = QgsMapCanvas()
        self.canvas.setCanvasColor(Qt.white)
        self.canvas.enableAntiAliasing(True)
        self.setCentralWidget(self.canvas)

        self.showOpenStreetMap()

        self.setToolbar()

    def setToolbar(self):
        actionZoomIn = QAction("Zoom in", self)
        actionZoomIn.setCheckable(True)
        actionZoomIn.triggered.connect(self.zoomIn)
        actionZoomOut = QAction("Zoom out", self)
        actionZoomOut.setCheckable(True)
        actionZoomOut.triggered.connect(self.zoomOut)
        actionPan = QAction("Pan", self)
        actionPan.setCheckable(True)
        actionPan.triggered.connect(self.pan)

        self.toolZoomIn = QgsMapToolZoom(self.canvas, False)
        self.toolZoomIn.setAction(actionZoomIn)
        self.toolZoomOut = QgsMapToolZoom(self.canvas, True)
        self.toolZoomOut.setAction(actionZoomOut)
        self.toolPan = QgsMapToolPan(self.canvas)
        self.toolPan.setAction(actionPan)

        self.canvasToolbar = self.addToolBar("Canvas actions")
        self.canvasToolbar.addAction(actionZoomIn)
        self.canvasToolbar.addAction(actionZoomOut)
        self.canvasToolbar.addAction(actionPan)

    def normal(self):
        self.canvas.setMapTool(self.mapTool)

    def zoomIn(self):
        self.canvas.setMapTool(self.toolZoomIn)

    def zoomOut(self):
        self.canvas.setMapTool(self.toolZoomOut)

    def pan(self):
        self.canvas.setMapTool(self.toolPan)

    def showOpenStreetMap(self):
        xml = """<GDAL_WMS>
    <Service name="TMS">
        <ServerUrl>https://tile.openstreetmap.org/${z}/${x}/${y}.png</ServerUrl>
        <SRS>EPSG:4326</SRS>
    </Service>
    <DataWindow>
        <UpperLeftX>-20037508.34</UpperLeftX>
        <UpperLeftY>20037508.34</UpperLeftY>
        <LowerRightX>20037508.34</LowerRightX>
        <LowerRightY>-20037508.34</LowerRightY>
        <TileLevel>18</TileLevel>
        <TileCountX>1</TileCountX>
        <TileCountY>1</TileCountY>
        <YOrigin>top</YOrigin>
    </DataWindow>
    <Projection>EPSG:4326</Projection>
    <BlockSizeX>256</BlockSizeX>
    <BlockSizeY>256</BlockSizeY>
    <BandsCount>3</BandsCount>
    <Cache>
        <Path/>
    </Cache>
</GDAL_WMS>"""

        vfn = "/vsimem/osm.xml"
        gdal.FileFromMemBuffer(vfn, xml)
        rLayer = QgsRasterLayer(vfn, "OSM")

        if not rLayer.isValid():
            print("Failed to open OSM")

        self.canvas.setExtent(rLayer.extent())
        QgsProject.instance().addMapLayer(rLayer)
        self.canvas.setLayers([rLayer])

        # not work?
        QgsProject.instance().setCrs(QgsCoordinateReferenceSystem("EPSG:4326"))
        rLayer.setCrs(QgsCoordinateReferenceSystem("EPSG:4326"))


if __name__ == "__main__":

    prefixPath = sys.argv[1]

    QgsApplication.setPrefixPath(prefixPath, True)

    qgs = QgsApplication([], True)

    qgs.initQgis()

    w = MainWindow()
    w.show()
    sys.exit(qgs.exec_())

    qgs.exitQgis()

Below: display of this script.

enter image description here

I want to change the CRS like QGIS3.

enter image description here

Please tell me how to use the API of PyQGIS.

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