Wednesday, 25 November 2015

python - How to add a dynamic timeline to the QGIS canvas?


I am using the plugin Timemanager which works really great but there is one thing I'd like to improve.



Instead of displaying the date and time on each frame, I would like to display a timeline stretching from *date_start* to *date_end* which shows the current frame time within this timespan.


I understand this possibility is not (yet?) offered by the plugin and therefore I am looking for an amazing idea on how to implement this. A core Qgis functionality that could be a workaround? An already existing plugin that I have missed? A small python script?


Obviously, in order for this to work nicely with the timemanager plugin, this timeline should be displayed in the map canvas, not in the composer.


A solution for Qgis 2.0 would be nice but I could also live with something for 1.8.


If someone has an idea or (even better) has already implemented something similar, please raise your hand!



Answer



I'm not aware of an already existing solution. But if you want to get started on your own, you can add your own graphics items onto the map canvas.


Normally, such things would derive from QgsMapCanvasItem but in your case, as you probably don't want to place your item in map coordinates, but rather in screen coordinates, you should derive it from QGraphicsItem and assign that item to the mapCanvas.scene().


The following script should get you started. Just expand the paint method to your needs.


from qgis.PyQt.QtWidgets import QGraphicsItem

from qgis.PyQt.QtCore import QRectF

class TimelineItem(QGraphicsItem):
def __init__(self, mapCanvas):
QGraphicsItem.__init__(self, None, mapCanvas.scene())

def boundingRect( self ):
penWidth = 1
return QRectF(10 - penWidth / 2, 10 - penWidth / 2, 200 + penWidth, 20 + penWidth)


def paint(self, painter, option, widget):
painter.drawRoundedRect(10, 10, 200, 20, 5, 5)

timelineItem = TimelineItem(iface.mapCanvas())

To get access to the timemanager information you can use code like this:


import TimeManager
ctrl = TimeManager.timemanager.control

ctrl.timeLayerManager.timeRestrictionsRefreshed(yourhandlermethodhere)

ctrl.timeLayerManager.getProjectTimeExtents()

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...