1

I would like to add a graphic/image on the top left corner of my view, would it be possible to make it static, meaning that wherever we zoom to or pan, the graphic will still stay put.

  • Not sure I understand. You can add a static graphic in the composer for printing e.g docs.qgis.org/1.8/en/docs/user_manual/print_composer/… . Do you want it in the main QGIS View and if yes, why? – ThomasG77 Mar 4 '16 at 3:20
  • I am afraid you can't put simply graphics into map view. You can do that only in composer. In the map view there are View - Decorations - Grid, Scale Bar, North arrow, Copyright label and that is it. Maybe if you could adjust somehow the north arrow image... But I have no idea how... – Miro Mar 4 '16 at 3:23
  • ThomasG77, I need to add a graphic that would change color depending on the location we pan to. Let's imagine it is just a circle shape at a corner of the view. – MyGIS Mar 4 '16 at 4:01
0

With PyQGIS, you can add an image to the canvas. You will find below only a small part of the code you could used as proof of the idea.

I didn't do the positioning part of the rectangle in the canvas but I've added how to manage event when your canvas changed.

To try out:

  • open a project with a world map using projection EPSG 4326.
  • change the code sample to reference the image path /tmp/test.png with your own (the image tested was for me a 100x100 red png image)
  • run the code below from Plugins > Python Console
  • zoom and pan changing the y position to see the image color changing.

    from PyQt4.QtGui import QImage, QColor, QPixmap
    from qgis.utils import iface
    
    img = QImage('/tmp/test.png')
    pixmap = QPixmap.fromImage(img)
    canvas = iface.mapCanvas()
    ref_pixmap = canvas.scene().addPixmap(pixmap)
    
    def on_extents_changed():
        center = canvas.center()
        print center.x(), center.y()
        new_pix = ref_pixmap.pixmap()
        if (center.y() >= -90 and center.y() < -45):
            new_pix.fill(QColor(255, 0, 255))
        elif center.y() >= -45 and center.y() < 0:
            new_pix.fill(QColor(0, 0, 255))
        elif center.y() >= 0 and center.y() < 45:
            new_pix.fill(QColor(255, 255, 0))
        elif center.y() >= 45 and center.y() < 90:
            new_pix.fill(QColor(0, 255, 0))
        ref_pixmap.setPixmap(new_pix)
    
    iface.mapCanvas().extentsChanged.connect(on_extents_changed)
    

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.