Exemple #1
0
    def testCalculateFeatureRequestGeometry(self):
        region = QgsMapClippingRegion(QgsGeometry.fromWkt('Polygon((0 0, 1 0, 1 1, 0 1, 0 0))'))
        region.setFeatureClip(QgsMapClippingRegion.FeatureClippingType.NoClipping)
        region2 = QgsMapClippingRegion(QgsGeometry.fromWkt('Polygon((0 0, 0.1 0, 0.1 2, 0 2, 0 0))'))
        region2.setFeatureClip(QgsMapClippingRegion.FeatureClippingType.NoClipping)

        rc = QgsRenderContext()

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

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

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

        rc.setCoordinateTransform(QgsCoordinateTransform(QgsCoordinateReferenceSystem('EPSG:3857'), QgsCoordinateReferenceSystem('EPSG:4326'), QgsProject.instance()))
        geom, should_clip = QgsMapClippingUtils.calculateFeatureRequestGeometry([region, region2], rc)
        geom.normalize()
        self.assertTrue(should_clip)
        self.assertEqual(geom.asWkt(0), 'Polygon ((0 0, 0 111325, 11132 111325, 11132 0, 0 0))')
    def testLabelIntersectionGeometry(self):
        region = QgsMapClippingRegion(
            QgsGeometry.fromWkt('Polygon((0 0, 1 0, 1 1, 0 1, 0 0))'))
        region.setFeatureClip(
            QgsMapClippingRegion.FeatureClippingType.ClipToIntersection)
        region2 = QgsMapClippingRegion(
            QgsGeometry.fromWkt('Polygon((0 0, 0.1 0, 0.1 2, 0 2, 0 0))'))
        region2.setFeatureClip(
            QgsMapClippingRegion.FeatureClippingType.NoClipping)
        region3 = QgsMapClippingRegion(
            QgsGeometry.fromWkt('Polygon((0 0, 0.1 0, 0.1 2, 0 2, 0 0))'))
        region3.setFeatureClip(
            QgsMapClippingRegion.FeatureClippingType.ClipPainterOnly)

        rc = QgsRenderContext()

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

        geom, should_clip = QgsMapClippingUtils.calculateLabelIntersectionGeometry(
            [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.calculateLabelIntersectionGeometry(
            [region2], rc)
        self.assertFalse(should_clip)
        self.assertTrue(geom.isNull())

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

        # region3 is a PainterClip type clipping region, MUST be applied for labels
        geom, should_clip = QgsMapClippingUtils.calculateLabelIntersectionGeometry(
            [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.calculateLabelIntersectionGeometry(
            [region, region3], rc)
        self.assertTrue(should_clip)
        self.assertEqual(
            geom.asWkt(0),
            'Polygon ((11132 0, 0 0, 0 111325, 11132 111325, 11132 0))')