Tuesday 25 September 2018

pyqgis - Why do QGIS 2.99 and 2.18 behave different when I use canvas.setDestinationCrs?


I started with the same setup in QGIS 2.99.0 (bc08a79) and QGIS 2.18.6 (both on Windows 10):



  • The project CRS is set to EPSG:3857

  • I added 1 layer: An XYZ tile layer with OpenStreetmap tiles: http://c.tile.openstreetmap.org/{z}/{x}/{y}.png


  • I Zoomed out to see the entire world

  • In QGIS 2.18.6 OTF is set to enabled. In version 2.99.0 OTF is always enabled.


When I run the following script in the Python Console to set the project CRS to EPSG:31370: canvas = iface.mapCanvas() target_crs = QgsCoordinateReferenceSystem() target_crs.createFromId( 31370, QgsCoordinateReferenceSystem.EpsgCrsId ) canvas.setDestinationCrs(target_crs)


I get the following correct results in QGIS 2.18.6:



  • The map is reprojected

  • The coordinates in the status bar are correct when I move the mouse pointer over the map

  • The code of the CRS in the status bar is correct: EPSG:31370 (OTF)

  • When hover that CRS code in the status bar it shows "Current CRS Belge 1972 / Belgian Lambert 72 (OTF enabled)"


  • When I click on that CRS code in the status bar, it opens the Project Properties with EPSG:31370 selected


All these results are as I expect them to be.


When I run the exact same code in QGIS 2.99.0, I get the following expected results:



  • The map is reprojected

  • The coordinates in the status bar are correct when I move the mouse pointer over the map


But I also get the following unexpected results:




  • The code of the CRS in the status bar is not correct: EPSG:3857

  • When hover that CRS code in the status bar it shows the incorrect "Current CRS WGS 84 / Pseudo Mercator"

  • When I click on that CRS code in the status bar, it opens the Project Properties with EPSG:3857 selected


When I use a global Natural Earth shapefile I get the same results.


What do I have to do to get the same correct result in both versions of QGIS? Can anyone explain what's happening in version 2.99.0?




EDIT:


Solution


I modified my code based on the answer by ndawson. Using the following code in QGIS 2.99.0: target_crs = QgsCoordinateReferenceSystem() target_crs.createFromId( 31370, QgsCoordinateReferenceSystem.EpsgCrsId ) QgsProject.instance().setCrs(target_crs)



gives the same result as the original code gives in QGIS 2.18.6. This modified code can't be used in QGIS 2.18.6.



Answer



There's many many API changes in QGIS 3.0. You're running into 2 of these:




  1. The canvas CRS should no longer be set directly, and you should instead use QgsProject.instance().setCrs(...). That will take care of setting the canvas CRS, status bar coordinates, etc.




  2. OTF projection is always on in QGIS 3. You can't switch it off, but you can set the project/canvas to a "no projection" mode by setting the project to an invalid CRS (QgsProject.instance().setCrs(QgsCoordinateReferenceSystem())). This has multiple effects, including treating all coordinates in layers as Cartesian coordinates (regardless of any layer CRS setting), and all measurements become unitless Cartesian measurements.





No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...