コード例 #1
0
    def test_apply_edit(self):
        """
        Test applying edits to a layer
        """
        layer = QgsAnnotationLayer(
            'test',
            QgsAnnotationLayer.LayerOptions(
                QgsProject.instance().transformContext()))
        self.assertTrue(layer.isValid())

        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)))

        rc = QgsRenderContext()
        self.assertCountEqual(
            layer.itemsInBounds(QgsRectangle(1, 1, 20, 20), rc),
            [polygon_item_id, linestring_item_id, marker_item_id])

        # can't apply a move to an item which doesn't exist in the layer
        self.assertEqual(
            layer.applyEdit(
                QgsAnnotationItemEditOperationMoveNode('xxx',
                                                       QgsVertexId(0, 0, 2),
                                                       QgsPoint(14, 15),
                                                       QgsPoint(19, 15))),
            Qgis.AnnotationItemEditOperationResult.Invalid)

        # apply move to polygon
        self.assertEqual(
            layer.applyEdit(
                QgsAnnotationItemEditOperationMoveNode(polygon_item_id,
                                                       QgsVertexId(0, 0, 2),
                                                       QgsPoint(14, 15),
                                                       QgsPoint(19, 15))),
            Qgis.AnnotationItemEditOperationResult.Success)

        self.assertEqual(
            layer.item(polygon_item_id).geometry().asWkt(),
            'Polygon ((12 13, 14 13, 19 15, 12 13))')
        # ensure that spatial index was updated
        self.assertCountEqual(
            layer.itemsInBounds(QgsRectangle(18, 1, 20, 16), rc),
            [polygon_item_id])
コード例 #2
0
    def testItemsInBounds(self):
        layer = QgsAnnotationLayer('test', QgsAnnotationLayer.LayerOptions(QgsProject.instance().transformContext()))
        self.assertTrue(layer.isValid())

        item1uuid = layer.addItem(QgsAnnotationPolygonItem(
            QgsPolygon(QgsLineString([QgsPoint(12, 13), QgsPoint(14, 13), QgsPoint(14, 15), QgsPoint(12, 13)]))))
        item2uuid = layer.addItem(
            QgsAnnotationLineItem(QgsLineString([QgsPoint(11, 13), QgsPoint(12, 13), QgsPoint(12, 150)])))
        item3uuid = layer.addItem(QgsAnnotationMarkerItem(QgsPoint(120, 13)))

        rc = QgsRenderContext()
        self.assertFalse(layer.itemsInBounds(QgsRectangle(-10, -10, -9, 9), rc))
        self.assertCountEqual(layer.itemsInBounds(QgsRectangle(12, 13, 14, 15), rc), [item1uuid, item2uuid])
        self.assertCountEqual(layer.itemsInBounds(QgsRectangle(12, 130, 14, 150), rc), [item2uuid])
        self.assertCountEqual(layer.itemsInBounds(QgsRectangle(110, 0, 120, 20), rc), [item3uuid])