5

I would like to display the value of a raster (at least one band) as a tooltip next to the mouse in Qgis.

The process would be :

  1. I select the raster layer
  2. when I move the cursor over the map (or maybe each time I click with the mouse),I can see the value next to the cursor.

I know the plugin ValueTool which is very useful for debug purposes but not so easy to use in order to analyse the data. It is often difficult to point at the right place with the mouse and at the same time to read the value in the plugin.

  • To enable your desired behavior with Value Tool plugin you need to select 'Options' tab at plugin and mark its 'Enable' box. – xunilk Jun 25 '17 at 14:22
  • Did you give my answer a try? – Germán Carrillo Jul 1 '17 at 1:27
  • 1
    @Germán Carrillo I tried it out. +1 – xunilk Jul 1 '17 at 15:06
7

One way of doing it is creating your own Map Tool and setting a QTimer (as QGIS does for MapTips) on mouse move events to show the tooltip. The following code illustrates it (works if you run it in the QGIS Python Console).

from qgis.core import QgsRasterLayer, QgsRaster
from qgis.gui import QgsMapToolEmitPoint
from PyQt4.QtCore import QTimer
from PyQt4.QtGui import QToolTip

class TooltipRasterMapTool(QgsMapToolEmitPoint):
    def __init__(self, canvas):
        self.canvas = canvas
        QgsMapToolEmitPoint.__init__(self, self.canvas)
        self.timerMapTips = QTimer( self.canvas )
        self.timerMapTips.timeout.connect( self.showMapTip )

    def canvasPressEvent(self, e):
        pass

    def canvasReleaseEvent(self, e):
        pass

    def canvasMoveEvent(self, e):
        if self.canvas.underMouse(): # Only if mouse is over the map
            QToolTip.hideText()
            self.timerMapTips.start( 700 ) # time in milliseconds

    def deactivate(self):
        self.timerMapTips.stop()

    def showMapTip( self ):
        self.timerMapTips.stop()
        if self.canvas.underMouse():
            rLayer = iface.activeLayer()
            if type(rLayer) is QgsRasterLayer:
                ident = rLayer.dataProvider().identify( self.toMapCoordinates(self.canvas.mouseLastXY()), QgsRaster.IdentifyFormatValue )
                if ident.isValid():
                    text = ", ".join(['{0:g}'.format(r) for r in ident.results().values() if r is not None] )
                else:
                    text = "Non valid value"
                QToolTip.showText( self.canvas.mapToGlobal( self.canvas.mouseLastXY() ), text, self.canvas )


tooltipRaster = TooltipRasterMapTool( iface.mapCanvas() )
iface.mapCanvas().setMapTool( tooltipRaster ) # Use your Map Tool!

Test plugin

Since a plugin would be much more convenient, I've created this basic test plugin ready to use, there you find installation instructions as well.

enter image description here

  • 1
    Thanks a lot. It is exactly what I need. Best regards. JB – Jacques Beilin Jun 29 '18 at 9:35
3

To enable your desired behavior with Value Tool plugin you need to select 'Options' tab at plugin and mark its 'Enable' box.

enter image description here

Afterward (change newly to 'Table' tab), when you click at the right place with the mouse, values and coordinates will remain static until next mouse click. I hope that it helps.

enter image description here

Editing Note:

To print results (pyperclip python module can be used to print to clipboard), I slightly adapted code in excellent Germán Carrillo's answer to do that.

from qgis.core import QgsRasterLayer, QgsRaster
from qgis.gui import QgsMapToolEmitPoint
from PyQt4.QtCore import QTimer
from PyQt4.QtGui import QToolTip

class TooltipRasterMapTool(QgsMapToolEmitPoint):
    def __init__(self, canvas):
        self.canvas = canvas
        QgsMapToolEmitPoint.__init__(self, self.canvas)
        self.timerMapTips = QTimer( self.canvas )
        self.timerMapTips.timeout.connect( self.showMapTip )

    def canvasPressEvent(self, e):
        point = self.toMapCoordinates(self.canvas.mouseLastXY())
        print '({:.4f}, {:.4f})'.format(point[0], point[1])
        rLayer = iface.activeLayer()
        if type(rLayer) is QgsRasterLayer:
            ident = rLayer.dataProvider().identify( point, QgsRaster.IdentifyFormatValue )
            if ident.isValid():
                text = ", ".join(['{0:g}'.format(r) for r in ident.results().values() if r is not None] )
            else:
                text = "Non valid value"

        print text


    def canvasReleaseEvent(self, e):
        pass

    def canvasMoveEvent(self, e):
        if self.canvas.underMouse(): # Only if mouse is over the map
            QToolTip.hideText()
            self.timerMapTips.start( 700 ) # time in milliseconds

    def deactivate(self):
        self.timerMapTips.stop()

    def showMapTip( self ):
        self.timerMapTips.stop()
        if self.canvas.underMouse():
            rLayer = iface.activeLayer()
            if type(rLayer) is QgsRasterLayer:
                ident = rLayer.dataProvider().identify( self.toMapCoordinates(self.canvas.mouseLastXY()), QgsRaster.IdentifyFormatValue )
                if ident.isValid():
                    text = ", ".join(['{0:g}'.format(r) for r in ident.results().values() if r is not None] )
                else:
                    text = "Non valid value"
                QToolTip.showText( self.canvas.mapToGlobal( self.canvas.mouseLastXY() ), text, self.canvas )


tooltipRaster = TooltipRasterMapTool( iface.mapCanvas() )
iface.mapCanvas().setMapTool( tooltipRaster ) # Use your Map Tool!

I tried it out with a RGB tif raster; as it can be observed at next image:

enter image description here

2

This is a workaround using a MapTips on vector layer:

  1. Create a Vector layer (points/grids) from your Raster layer.
  2. Make the Vector layer transparent.
  3. Build Map Tips on the Vector layer

In detail:

(1) Start Processing Toolbox | SAGA | Vector <-> raster | Raster values to points

  • Assign your Raster layer to the Grids
  • Set Type to [1] cells which creates gridded vector cell at each raster cell
  • [Run] this tool which returns a Shapes vector layer . Its attribute table has ID, X, Y and RasterValue (named after your Raster layer).

(2) Open Layer Properties of this Shapes layer. Open Style tab and set Transparency to 100%, so that this Shapes layer becomes invisible.

(3) Go to Display tab and select RasterValue field to Field drop-down menu. [OK] to close the window.

(4) Click on the Map Tips icon of the Attributes toolbar to activate it.

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.