7

In QGIS 2.18, I had a python script that added annotations. I can't seem to get the annotations to add in 3.0:

for feature in iter:
            # retrieve every feature with its geometry and attributes
            # fetch geometry
            geom = feature.geometry()
            # show some information about the feature
            if geom.type() ==  0: #QgsWkbTypes.Point:
                print("In add annotation...")
                point = geom.asPoint()
                utmPoint = utm.from_latlon(point.y(), point.x())
                # fetch attributes
                attrs = feature.attributes()
                # attrs is a list. It contains all the attribute values of this feature
                waypointText = attrs[0]
                textItem = QgsTextAnnotation (iface.mapCanvas())  
                crs = waypoint_layer.crs();
                document = QtGui.QTextDocument()
                document.setHtml("<strong>" + str(waypointText) + "</strong>")
                symbol = QgsMarkerSymbol()
                symbol.setSize(0);
                textItem.setDocument(document)
                textItem.setMapPosition(QgsPointXY(float(utmPoint[0]), float(utmPoint[1])))
                textItem.setMapLayer(waypoint_layer);
           #     textItem.setFrameSize(QSizeF(document.size().width() + 5,document.size().height() + 2))
           #     textItem.setFrameBorderWidth(2)
           #     textItem.setFrameColor(QColor("#e31a1c"))
           #     textItem.setFrameBackgroundColor(QColor("#d7d7d7"))
                 textItem.setMarkerSymbol(symbol)
           #     textItem.update()

Commented out textItem lines no longer work in 3.0, but even without setting frame specific features, the annotation doesn't show up.

Any examples of programatically adding an annotation to a map with QGIS 3.0?

1
  • Any update on this question? – Blinxen May 2 '19 at 11:31
5

Add sample code for add annotations using QGIS 3.0 , using point layer

layer = iface. activeLayer()
iter = layer.getFeatures()
canvas = iface.mapCanvas()

for feature in iter:
    # retrieve every feature with its geometry and attributes
    # fetch geometry
    geom = feature.geometry()
    # show some information about the feature
    if geom.type() ==  0: #QgsWkbTypes.Point:
        print("In add annotation...")
    point = geom.asPoint()
    # fetch attributes
    attrs = feature.attributes()
    # attrs is a list. It contains all the attribute values of this feature
    waypointText = attrs[0]
    html = "<strong>" + str(waypointText) + "</strong>"
    a = QgsTextAnnotation()
    c = QTextDocument()

    c.setHtml(html)
    a.setDocument(c)

    a.setFrameSize(QSizeF(100, 50))
    a.setMapLayer(layer)
    a.setFrameOffsetFromReferencePoint(QtCore.QPointF(30, 30))
    a.setMapPosition(point)
    a.setMapPositionCrs(QgsCoordinateReferenceSystem(layer.crs()))

    i = QgsMapCanvasAnnotationItem(a, canvas)

And the result is

enter image description here Tested using QGIS 3.6.2

2
  • 1
    It's also good to use QgsAnnotationManager to manage the annotations. QgsProject.instance().annotationManager().addAnnotation(a) QgsProject.instance().annotationManager().removeAnnotation(a) – Zac May 8 '20 at 1:24
  • How can I please prevent points from turning red? I have two different layers where I add annotations, but after that all points turn red. – mee Sep 25 '20 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.