Ejemplo n.º 1
0
    def testProjectMainAnnotationLayer(self):
        p = QgsProject()
        self.assertIsNotNone(p.mainAnnotationLayer())

        # add some items to project annotation layer
        polygon_item_id = p.mainAnnotationLayer().addItem(QgsAnnotationPolygonItem(
            QgsPolygon(QgsLineString([QgsPoint(12, 13), QgsPoint(14, 13), QgsPoint(14, 15), QgsPoint(12, 13)]))))
        linestring_item_id = p.mainAnnotationLayer().addItem(
            QgsAnnotationLineItem(QgsLineString([QgsPoint(11, 13), QgsPoint(12, 13), QgsPoint(12, 15)])))
        marker_item_id = p.mainAnnotationLayer().addItem(QgsAnnotationMarkerItem(QgsPoint(12, 13)))

        # save project to xml
        tmpDir = QTemporaryDir()
        tmpFile = "{}/project.qgs".format(tmpDir.path())
        self.assertTrue(p.write(tmpFile))

        # test that annotation layer is cleared with project
        p.clear()
        self.assertEqual(len(p.mainAnnotationLayer().items()), 0)

        # check that main annotation layer is restored on project read
        p2 = QgsProject()
        self.assertTrue(p2.read(tmpFile))
        self.assertEqual(len(p2.mainAnnotationLayer().items()), 3)
        self.assertIsInstance(p2.mainAnnotationLayer().items()[polygon_item_id], QgsAnnotationPolygonItem)
        self.assertIsInstance(p2.mainAnnotationLayer().items()[linestring_item_id], QgsAnnotationLineItem)
        self.assertIsInstance(p2.mainAnnotationLayer().items()[marker_item_id], QgsAnnotationMarkerItem)
Ejemplo n.º 2
0
    def testMainAnnotationLayer(self):
        """
        Make sure main annotation layer is rendered in maps above all other layers
        """
        p = QgsProject()

        vl = QgsVectorLayer("Polygon?crs=epsg:4326&field=fldtxt:string",
                            "layer", "memory")
        sym3 = QgsFillSymbol.createSimple({'color': '#b200b2'})
        vl.renderer().setSymbol(sym3)

        p.addMapLayer(vl)
        layout = QgsLayout(p)
        layout.initializeDefaults()
        p.setCrs(QgsCoordinateReferenceSystem('EPSG:4326'))
        map = QgsLayoutItemMap(layout)
        map.attemptSetSceneRect(QRectF(10, 10, 180, 180))
        map.setFrameEnabled(True)
        map.setFrameStrokeWidth(
            QgsLayoutMeasurement(2, QgsUnitTypes.LayoutMillimeters))
        map.setBackgroundEnabled(True)
        map.setBackgroundColor(QColor(200, 255, 200))
        map.zoomToExtent(QgsRectangle(10, 30, 20, 35))
        map.setLayers([vl])
        layout.addLayoutItem(map)

        # add polygon to layer
        f = QgsFeature()
        f.setGeometry(QgsGeometry.fromRect(QgsRectangle(5, 25, 25, 45)))
        self.assertTrue(vl.dataProvider().addFeatures([f]))

        # no annotation yet...
        checker = QgsLayoutChecker('composermap_annotation_empty', layout)
        checker.setControlPathPrefix("composer_map")
        result, message = checker.testLayout()
        TestQgsLayoutMap.report += checker.report()
        self.assertTrue(result, message)

        annotation_layer = p.mainAnnotationLayer()
        annotation_layer.setCrs(QgsCoordinateReferenceSystem(4326))
        annotation_geom = QgsGeometry.fromRect(QgsRectangle(12, 30, 18, 33))
        annotation = QgsAnnotationPolygonItem(
            annotation_geom.constGet().clone())
        sym3 = QgsFillSymbol.createSimple({
            'color': '#ff0000',
            'outline_style': 'no'
        })
        annotation.setSymbol(sym3)
        annotation_layer.addItem(annotation)

        # annotation must be drawn above map layers
        checker = QgsLayoutChecker('composermap_annotation_item', layout)
        checker.setControlPathPrefix("composer_map")
        result, message = checker.testLayout()
        TestQgsLayoutMap.report += checker.report()
        self.assertTrue(result, message)
Ejemplo n.º 3
0
    def testMainAnnotationLayerCrs(self):
        p = QgsProject()
        self.assertEqual(p.crs(), p.mainAnnotationLayer().crs())

        # main annotation layer should follow project crs
        p.setCrs(QgsCoordinateReferenceSystem('EPSG:3111'))
        self.assertEqual(p.crs(), p.mainAnnotationLayer().crs())

        p.setCrs(QgsCoordinateReferenceSystem('EPSG:4326'))
        self.assertEqual(p.crs(), p.mainAnnotationLayer().crs())

        # add an item, should lock in the crs for the main annotation layer
        p.mainAnnotationLayer().addItem(
            QgsAnnotationPolygonItem(
                QgsPolygon(
                    QgsLineString([
                        QgsPoint(12, 13),
                        QgsPoint(14, 13),
                        QgsPoint(14, 15),
                        QgsPoint(12, 13)
                    ]))))

        p.setCrs(QgsCoordinateReferenceSystem('EPSG:3857'))
        self.assertEqual(p.mainAnnotationLayer().crs().authid(), 'EPSG:4326')