示例#1
0
    def testReplaceItem(self):
        layer = QgsAnnotationLayer(
            'test',
            QgsAnnotationLayer.LayerOptions(
                QgsProject.instance().transformContext()))

        polygon_item_id = layer.addItem(
            QgsAnnotationPolygonItem(
                QgsPolygon(
                    QgsLineString([
                        QgsPoint(12, 13),
                        QgsPoint(14, 13),
                        QgsPoint(14, 15),
                        QgsPoint(12, 13)
                    ]))))
        linestring_item_id = layer.addItem(
            QgsAnnotationLineItem(
                QgsLineString(
                    [QgsPoint(11, 13),
                     QgsPoint(12, 13),
                     QgsPoint(12, 15)])))
        marker_item_id = layer.addItem(
            QgsAnnotationMarkerItem(QgsPoint(12, 13)))

        self.assertEqual(
            layer.item(polygon_item_id).geometry().asWkt(),
            'Polygon ((12 13, 14 13, 14 15, 12 13))')
        self.assertEqual(
            layer.item(linestring_item_id).geometry().asWkt(),
            'LineString (11 13, 12 13, 12 15)')
        self.assertEqual(
            layer.item(marker_item_id).geometry().asWkt(), 'POINT(12 13)')

        layer.replaceItem(
            linestring_item_id,
            QgsAnnotationLineItem(
                QgsLineString(
                    [QgsPoint(21, 13),
                     QgsPoint(22, 13),
                     QgsPoint(22, 15)])))
        self.assertEqual(
            layer.item(polygon_item_id).geometry().asWkt(),
            'Polygon ((12 13, 14 13, 14 15, 12 13))')
        self.assertEqual(
            layer.item(linestring_item_id).geometry().asWkt(),
            'LineString (21 13, 22 13, 22 15)')
        self.assertEqual(
            layer.item(marker_item_id).geometry().asWkt(), 'POINT(12 13)')
示例#2
0
    def test_rendered_item_results_remove_outdated(self):
        """
        Test that outdated results are removed from rendered item result caches
        """
        canvas = QgsMapCanvas()
        canvas.setDestinationCrs(QgsCoordinateReferenceSystem('EPSG:4326'))
        canvas.setFrameStyle(0)
        canvas.resize(600, 400)
        canvas.setCachingEnabled(True)
        self.assertEqual(canvas.width(), 600)
        self.assertEqual(canvas.height(), 400)

        layer = QgsAnnotationLayer(
            'test',
            QgsAnnotationLayer.LayerOptions(
                QgsProject.instance().transformContext()))
        self.assertTrue(layer.isValid())
        layer2 = QgsAnnotationLayer(
            'test',
            QgsAnnotationLayer.LayerOptions(
                QgsProject.instance().transformContext()))
        self.assertTrue(layer2.isValid())

        item = QgsAnnotationPolygonItem(
            QgsPolygon(
                QgsLineString([
                    QgsPoint(11.5, 13),
                    QgsPoint(12, 13),
                    QgsPoint(12, 13.5),
                    QgsPoint(11.5, 13)
                ])))
        item.setSymbol(
            QgsFillSymbol.createSimple({
                'color': '200,100,100',
                'outline_color': 'black',
                'outline_width': '2'
            }))
        item.setZIndex(1)
        i1_id = layer.addItem(item)

        item = QgsAnnotationLineItem(
            QgsLineString(
                [QgsPoint(11, 13),
                 QgsPoint(12, 13),
                 QgsPoint(12, 15)]))
        item.setZIndex(2)
        i2_id = layer.addItem(item)

        item = QgsAnnotationMarkerItem(QgsPoint(12, 13))
        item.setZIndex(3)
        i3_id = layer2.addItem(item)

        layer.setCrs(QgsCoordinateReferenceSystem('EPSG:4326'))
        layer2.setCrs(QgsCoordinateReferenceSystem('EPSG:4326'))

        canvas.setLayers([layer, layer2])
        canvas.setExtent(QgsRectangle(10, 10, 18, 18))
        canvas.show()

        # need to wait until first redraw can occur (note that we first need to wait till drawing starts!)
        while not canvas.isDrawing():
            app.processEvents()
        canvas.waitWhileRendering()

        results = canvas.renderedItemResults()
        self.assertCountEqual([i.itemId() for i in results.renderedItems()],
                              [i1_id, i2_id, i3_id])

        # now try modifying an annotation in the layer -- it will redraw, and we don't want to reuse any previously
        # cached rendered item results for this layer!

        item = QgsAnnotationPolygonItem(
            QgsPolygon(
                QgsLineString([
                    QgsPoint(11.5, 13),
                    QgsPoint(12.5, 13),
                    QgsPoint(12.5, 13.5),
                    QgsPoint(11.5, 13)
                ])))
        item.setZIndex(1)
        layer.replaceItem(i1_id, item)
        while not canvas.isDrawing():
            app.processEvents()
        canvas.waitWhileRendering()

        item = QgsAnnotationMarkerItem(QgsPoint(17, 18))
        item.setZIndex(3)
        layer2.replaceItem(i3_id, item)
        while not canvas.isDrawing():
            app.processEvents()
        canvas.waitWhileRendering()

        results = canvas.renderedItemResults()
        items_in_bounds = results.renderedAnnotationItemsInBounds(
            QgsRectangle(10, 10, 15, 15))
        self.assertCountEqual([i.itemId() for i in items_in_bounds],
                              [i1_id, i2_id])

        items_in_bounds = results.renderedAnnotationItemsInBounds(
            QgsRectangle(15, 15, 20, 20))
        self.assertCountEqual([i.itemId() for i in items_in_bounds], [i3_id])