Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QgsMapCanvas::setDestinationCrs causes invalid map canvas extent #51833

Closed
1 of 2 tasks
jakimowb opened this issue Feb 13, 2023 · 0 comments · Fixed by #51841
Closed
1 of 2 tasks

QgsMapCanvas::setDestinationCrs causes invalid map canvas extent #51833

jakimowb opened this issue Feb 13, 2023 · 0 comments · Fixed by #51841
Assignees
Labels
Bug Either a bug report, or a bug fix. Let's hope for the latter!

Comments

@jakimowb
Copy link
Contributor

What is the bug or the crash?

A change of the QgsMapCanvas CRS causes a transformation of the canvas extent into coordinate values of the new CRS.
When this recalculation is finishedm the extentsChanged signal is emitted.

At the time of this signal, the CRS is still the old one. Slots that are connected to this signal which require the CRS information as well, may therefore use inconsistent data, as the canvas CRS does not fit to the values of the extent coordinates.

Steps to reproduce the issue

The following test changes a map canvas CRS from EPSG:32633 to EPGS:4326.
The recalculated extent is validated against the boundaries of the map canvas CRS. The extent coordinates should be inside the CRS boundaries, but this fails because the canvas CRS is still set on EPSG:32633, while the coordinate values are already in EPSG:4326.

Output:

Set CRS to <QgsCoordinateReferenceSystem: EPSG:4326>
 WGS 84 / UTM zone 33N : 13.2958240245312247,52.4763378599360735 : 13.3992241580016476,52.5538069254515321
Traceback (most recent call last):
  File "F:\Repositories\enmap-box\tests\src\issues\test_qgis_issue_mapcrs.py", line 33, in onExtentsChanged
    self.assertTrue(bounds.contains(extent))

Code:

from qgis.core import QgsCoordinateReferenceSystem, QgsRectangle, QgsCoordinateTransform
from qgis.gui import QgsMapCanvas
from qgis.testing import TestCase, start_app

app = start_app()


class MapCRSTest(TestCase):

    def test_mapCRS(self):
        crsLatLon = QgsCoordinateReferenceSystem('EPSG:4326')

        canvas = QgsMapCanvas()
        canvas.setDestinationCrs(QgsCoordinateReferenceSystem('EPSG:32633'))
        extent = QgsRectangle(384418.42293523817,
                              5817479.542521443,
                              391319.6968762298,
                              5821746.336751187)
        canvas.setExtent(extent)

        def onExtentsChanged(*args):
            crs = canvas.mapSettings().destinationCrs()
            extent = canvas.extent()
            print(f' {crs.description()} : {extent.toString()}')

            # convert boundary coordinate of the MapCanvas CRS from LatLon to MapCanvas CRS values
            trans = QgsCoordinateTransform()
            trans.setSourceCrs(crsLatLon)
            trans.setDestinationCrs(crs)
            bounds = trans.transform(crs.bounds())

            # the MapCanvas extent should still be within valid bound coordinates
            self.assertTrue(bounds.contains(extent))

        canvas.extentsChanged.connect(onExtentsChanged)
        print(f'Set CRS to {crsLatLon}')
        canvas.setDestinationCrs(crsLatLon)

Versions

QGIS 3.29 master c065313595 (windows 11)

Supported QGIS version

  • I'm running a supported QGIS version according to the roadmap.

New profile

  • I tried with a new QGIS profile

Additional context

No response

@jakimowb jakimowb added the Bug Either a bug report, or a bug fix. Let's hope for the latter! label Feb 13, 2023
@nyalldawson nyalldawson self-assigned this Feb 14, 2023
nyalldawson added a commit to nyalldawson/QGIS that referenced this issue Feb 14, 2023
to extentsChanged when canvas CRS is changed

Fixes qgis#51833
nyalldawson added a commit that referenced this issue Feb 15, 2023
to extentsChanged when canvas CRS is changed

Fixes #51833
qgis-bot pushed a commit that referenced this issue Feb 15, 2023
to extentsChanged when canvas CRS is changed

Fixes #51833
nyalldawson added a commit that referenced this issue Feb 17, 2023
to extentsChanged when canvas CRS is changed

Fixes #51833
benoitdm-oslandia pushed a commit to benoitdm-oslandia/QGIS that referenced this issue Feb 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Either a bug report, or a bug fix. Let's hope for the latter!
Projects
None yet
2 participants