Exemple #1
0
    def testAppliesToLayer(self):
        layer = QgsVectorLayer(
            "Point?field=fldtxt:string&field=fldint:integer", "addfeat",
            "memory")
        layer2 = QgsVectorLayer(
            "Point?field=fldtxt:string&field=fldint:integer", "addfeat",
            "memory")
        layer3 = 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))'))
        # should apply to all layers by default
        self.assertTrue(region.appliesToLayer(layer))
        self.assertTrue(region.appliesToLayer(layer2))
        self.assertTrue(region.appliesToLayer(layer3))

        region.setRestrictedLayers([layer, layer2])
        self.assertTrue(region.appliesToLayer(layer))
        self.assertTrue(region.appliesToLayer(layer2))
        self.assertTrue(region.appliesToLayer(layer3))

        region.setRestrictToLayers(True)
        self.assertTrue(region.appliesToLayer(layer))
        self.assertTrue(region.appliesToLayer(layer2))
        self.assertFalse(region.appliesToLayer(layer3))

        region.setRestrictedLayers([])
        self.assertFalse(region.appliesToLayer(layer))
        self.assertFalse(region.appliesToLayer(layer2))
        self.assertFalse(region.appliesToLayer(layer3))
    def testClippingRegionsForLayer(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))'))
        region2 = QgsMapClippingRegion(
            QgsGeometry.fromWkt('Polygon((0 0, 0.1 0, 0.1 2, 0 2, 0 0))'))
        region2.setRestrictedLayers([layer])
        region2.setRestrictToLayers(True)
        ms = QgsMapSettings()
        ms.addClippingRegion(region)
        ms.addClippingRegion(region2)
        rc = QgsRenderContext.fromMapSettings(ms)

        regions = QgsMapClippingUtils.collectClippingRegionsForLayer(rc, layer)
        self.assertEqual(len(regions), 2)
        self.assertEqual(regions[0].geometry().asWkt(1),
                         'Polygon ((0 0, 1 0, 1 1, 0 1, 0 0))')
        self.assertEqual(regions[1].geometry().asWkt(1),
                         'Polygon ((0 0, 0.1 0, 0.1 2, 0 2, 0 0))')

        regions = QgsMapClippingUtils.collectClippingRegionsForLayer(
            rc, layer2)
        self.assertEqual(len(regions), 1)
        self.assertEqual(regions[0].geometry().asWkt(1),
                         'Polygon ((0 0, 1 0, 1 1, 0 1, 0 0))')
Exemple #3
0
    def testGetSet(self):
        region = QgsMapClippingRegion(
            QgsGeometry.fromWkt('Polygon((0 0, 1 0, 1 1, 0 1, 0 0))'))
        self.assertEqual(region.geometry().asWkt(),
                         'Polygon ((0 0, 1 0, 1 1, 0 1, 0 0))')

        region.setGeometry(
            QgsGeometry.fromWkt('Polygon((10 0, 11 0, 11 1, 10 1, 10 0))'))
        self.assertEqual(region.geometry().asWkt(),
                         'Polygon ((10 0, 11 0, 11 1, 10 1, 10 0))')

        self.assertEqual(
            region.featureClip(),
            QgsMapClippingRegion.FeatureClippingType.ClipToIntersection)
        region.setFeatureClip(
            QgsMapClippingRegion.FeatureClippingType.ClipPainterOnly)
        self.assertEqual(
            region.featureClip(),
            QgsMapClippingRegion.FeatureClippingType.ClipPainterOnly)

        layer = QgsVectorLayer(
            "Point?field=fldtxt:string&field=fldint:integer", "addfeat",
            "memory")
        layer2 = QgsVectorLayer(
            "Point?field=fldtxt:string&field=fldint:integer", "addfeat",
            "memory")
        self.assertEqual(len(region.restrictedLayers()), 0)
        region.setRestrictedLayers([layer, layer2])
        self.assertCountEqual(region.restrictedLayers(), [layer, layer2])
        region.setRestrictToLayers(False)
        self.assertFalse(region.restrictToLayers())
        region.setRestrictToLayers(True)
        self.assertTrue(region.restrictToLayers())