Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 2, 2020
1 parent 291dbe8 commit 30a3582
Showing 1 changed file with 38 additions and 5 deletions.
43 changes: 38 additions & 5 deletions tests/src/python/test_qgsmapclippingutils.py
Expand Up @@ -52,13 +52,10 @@ def testClippingRegionsForLayer(self):
self.assertEqual(regions[0].geometry().asWkt(1), 'Polygon ((0 0, 1 0, 1 1, 0 1, 0 0))')

def testCalculateFeatureRequestGeometry(self):
layer = QgsVectorLayer("Point?field=fldtxt:string&field=fldint:integer",
"addfeat", "memory")
layer2 = QgsVectorLayer("Point?field=fldtxt:string&field=fldint:integer",
"addfeat", "memory")

region = QgsMapClippingRegion(QgsGeometry.fromWkt('Polygon((0 0, 1 0, 1 1, 0 1, 0 0))'))
region.setFeatureClip(QgsMapClippingRegion.FeatureClippingType.Intersects)
region2 = QgsMapClippingRegion(QgsGeometry.fromWkt('Polygon((0 0, 0.1 0, 0.1 2, 0 2, 0 0))'))
region2.setFeatureClip(QgsMapClippingRegion.FeatureClippingType.Intersect)

rc = QgsRenderContext()

Expand All @@ -79,6 +76,42 @@ def testCalculateFeatureRequestGeometry(self):
self.assertTrue(should_clip)
self.assertEqual(geom.asWkt(0), 'Polygon ((11132 0, 0 0, 0 111325, 11132 111325, 11132 0))')

def testCalculateFeatureIntersectionGeometry(self):
region = QgsMapClippingRegion(QgsGeometry.fromWkt('Polygon((0 0, 1 0, 1 1, 0 1, 0 0))'))
region.setFeatureClip(QgsMapClippingRegion.FeatureClippingType.Intersect)
region2 = QgsMapClippingRegion(QgsGeometry.fromWkt('Polygon((0 0, 0.1 0, 0.1 2, 0 2, 0 0))'))
region2.setFeatureClip(QgsMapClippingRegion.FeatureClippingType.Intersects)
region3 = QgsMapClippingRegion(QgsGeometry.fromWkt('Polygon((0 0, 0.1 0, 0.1 2, 0 2, 0 0))'))
region3.setFeatureClip(QgsMapClippingRegion.FeatureClippingType.Intersect)

rc = QgsRenderContext()

geom, should_clip = QgsMapClippingUtils.calculateFeatureIntersectionGeometry([], rc)
self.assertFalse(should_clip)
self.assertTrue(geom.isNull())

geom, should_clip = QgsMapClippingUtils.calculateFeatureIntersectionGeometry([region], rc)
self.assertTrue(should_clip)
self.assertEqual(geom.asWkt(1), 'Polygon ((0 0, 1 0, 1 1, 0 1, 0 0))')

# region2 is a Intersects type clipping region, should not apply here
geom, should_clip = QgsMapClippingUtils.calculateFeatureIntersectionGeometry([region2], rc)
self.assertFalse(should_clip)
self.assertTrue(geom.isNull())

geom, should_clip = QgsMapClippingUtils.calculateFeatureIntersectionGeometry([region, region2], rc)
self.assertTrue(should_clip)
self.assertEqual(geom.asWkt(1), 'Polygon ((0 0, 1 0, 1 1, 0 1, 0 0))')

geom, should_clip = QgsMapClippingUtils.calculateFeatureIntersectionGeometry([region, region2, region3], rc)
self.assertTrue(should_clip)
self.assertEqual(geom.asWkt(1), 'Polygon ((0.1 0, 0 0, 0 1, 0.1 1, 0.1 0))')

rc.setCoordinateTransform(QgsCoordinateTransform(QgsCoordinateReferenceSystem('EPSG:3857'), QgsCoordinateReferenceSystem('EPSG:4326'), QgsProject.instance()))
geom, should_clip = QgsMapClippingUtils.calculateFeatureIntersectionGeometry([region, region3], rc)
self.assertTrue(should_clip)
self.assertEqual(geom.asWkt(0), 'Polygon ((11132 0, 0 0, 0 111325, 11132 111325, 11132 0))')


if __name__ == '__main__':
unittest.main()

0 comments on commit 30a3582

Please sign in to comment.