From 8f1a6e30482a7165d618da22eb3070604eafc6be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Bartoletti?= Date: Tue, 22 Oct 2024 13:39:53 +0200 Subject: [PATCH] fix(QgsCurve): properly handle area cache when reversing geometry --- src/core/geometry/qgscircularstring.cpp | 2 ++ src/core/geometry/qgslinestring.cpp | 2 ++ tests/src/python/test_qgslinestring.py | 16 ++++++++++++++++ 3 files changed, 20 insertions(+) diff --git a/src/core/geometry/qgscircularstring.cpp b/src/core/geometry/qgscircularstring.cpp index bdae1ac358f7..9027c0e9ac43 100644 --- a/src/core/geometry/qgscircularstring.cpp +++ b/src/core/geometry/qgscircularstring.cpp @@ -1605,6 +1605,8 @@ QgsCircularString *QgsCircularString::reversed() const { std::reverse( copy->mM.begin(), copy->mM.end() ); } + + copy->mSummedUpArea = -mSummedUpArea; return copy; } diff --git a/src/core/geometry/qgslinestring.cpp b/src/core/geometry/qgslinestring.cpp index 711314d8a5a4..419d9cd20ad6 100644 --- a/src/core/geometry/qgslinestring.cpp +++ b/src/core/geometry/qgslinestring.cpp @@ -1464,6 +1464,8 @@ QgsLineString *QgsLineString::reversed() const { std::reverse( copy->mM.begin(), copy->mM.end() ); } + + copy->mSummedUpArea = -mSummedUpArea; return copy; } diff --git a/tests/src/python/test_qgslinestring.py b/tests/src/python/test_qgslinestring.py index 1d6494f12605..3b2d68e4ba9d 100644 --- a/tests/src/python/test_qgslinestring.py +++ b/tests/src/python/test_qgslinestring.py @@ -389,6 +389,22 @@ def test_simplify_by_distance(self): self.assertEqual(p.simplifyByDistance(0).asWkt(), 'LineString (2 0, 2 2, 0 2, 0 0, 2 0)') + def test_orientation(self): + """ + test orientation. From https://github.com/qgis/QGIS/issues/58333 + """ + geom = QgsLineString() + geom.fromWkt('LineString (1 1, 2 1, 2 2, 1 2, 1 1)') + self.assertEqual(geom.sumUpArea(), 1.0) + self.assertEqual(geom.orientation(), Qgis.AngularDirection.CounterClockwise) + geom.fromWkt('LineString (1 1, 1 2, 2 2, 2 1, 1 1)') + self.assertEqual(geom.sumUpArea(), -1.0) + self.assertEqual(geom.orientation(), Qgis.AngularDirection.Clockwise) + geom = geom.reversed() + self.assertEqual(geom.asWkt(), 'LineString (1 1, 2 1, 2 2, 1 2, 1 1)') + self.assertEqual(geom.sumUpArea(), 1.0) + self.assertEqual(geom.orientation(), Qgis.AngularDirection.CounterClockwise) + if __name__ == '__main__': unittest.main()