Skip to content
Permalink
Browse files

More tests

  • Loading branch information
nyalldawson committed Jul 2, 2020
1 parent 291dbe8 commit 30a3582a6187cae7276113baa4d0f13525bd39d4
Showing with 38 additions and 5 deletions.
  1. +38 −5 tests/src/python/test_qgsmapclippingutils.py
@@ -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()

@@ -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.
You can’t perform that action at this time.