forked from anitagraser/TimeManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timelayermanager.py
299 lines (256 loc) · 12.7 KB
/
timelayermanager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/usr/bin/python
# -*- coding: UTF-8 -*-
from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from timelayer import NotATimeAttributeError
class TimeLayerManager(QObject):
"""Class manages all layers that can be queried temporally and provides navigation in time. Parenthesized sections are not implemented yet. All functions, besides the get functions, trigger a redraw."""
timeRestrictionsRefreshed = pyqtSignal(datetime)#object)
projectTimeExtentsChanged = pyqtSignal(object)#tuple)
lastLayerRemoved = pyqtSignal()
toggledManagement = pyqtSignal(object)
def __init__(self,iface):
QObject.__init__(self)
self.iface = iface
self.timeLayerList = []
self.projectTimeExtents = ()
self.timeFrameType = 'days' # default
self.timeFrameSize = 1 # some default value
self.currentTimePosition = None
self.timeManagementEnabled = True
self.isFirstRun=True
def isEnabled(self):
"""return true if the manager is enabled"""
return self.timeManagementEnabled
def getManagedLayers(self):
"""get the list of qgsMapLayers managed by the timeManager"""
layerList = []
for timeLayer in self.timeLayerList:
layerList.append(timeLayer.layer)
return layerList
def getCurrentTimePosition(self):
"""returns the manager's currentTimePosition"""
return self.currentTimePosition
def getTimeFrameType(self):
"""returns the type of the time frame, e.g. minutes, hours, days"""
return self.timeFrameType
def getTimeFrameSize(self):
"""returns the size of the time frame"""
return self.timeFrameSize
def getFrameCount(self):
"""returns the number of frames that can be generated using the current settings"""
if len(self.getManagedLayers()) == 0:
return 0
try:
td1 = self.projectTimeExtents[1]-self.projectTimeExtents[0]
except TypeError: # hope this fixes #17 which I still cannot reproduce
return 0
td2 = self.timeFrame()
# this is how you can devide two timedeltas (not supported by default):
us1 = td1.microseconds + 1000000 * (td1.seconds + 86400 * td1.days)
us2 = td2.microseconds + 1000000 * (td2.seconds + 86400 * td2.days)
if us2 == 0:
return 1000 # this is just a stupid default, TODO!
return us1 / us2
def hasLayers(self):
"""returns true if the manager has at least one layer registered"""
if len(self.timeLayerList) > 0:
return True
else:
return False
def hasActiveLayers(self):
"""returns true if the manager has at least one layer registered"""
if len(self.timeLayerList) > 0:
for layer in self.timeLayerList:
if layer.isEnabled():
return True
return False
def clearTimeLayerList(self):
"""clear the timeLayerList"""
for timeLayer in self.timeLayerList:
timeLayer.deleteTimeRestriction()
self.timeLayerList=[]
def timeFrame(self):
"""returns the current time frame as datetime.timedelta object"""
if self.timeFrameType == 'months':
return relativedelta(months=self.timeFrameSize) # months are not supported by timedelta
elif self.timeFrameType == 'weeks':
return timedelta(weeks=self.timeFrameSize)
elif self.timeFrameType == 'days':
return timedelta(days=self.timeFrameSize)
elif self.timeFrameType == 'hours':
return timedelta(hours=self.timeFrameSize)
elif self.timeFrameType == 'minutes':
return timedelta(minutes=self.timeFrameSize)
elif self.timeFrameType == 'seconds':
return timedelta(seconds=self.timeFrameSize)
# current code only supports down to seconds!
elif self.timeFrameType == 'milliseconds':
return timedelta(milliseconds=self.timeFrameSize)
elif self.timeFrameType == 'microseconds':
return timedelta(microseconds=self.timeFrameSize)
def refresh(self):
"""Applies or removes the temporal constraints for all managed (and enabled) layers"""
if not self.hasLayers():
return
if self.timeManagementEnabled:
for timeLayer in self.timeLayerList:
#try:
timeLayer.setTimeRestriction(self.currentTimePosition,self.timeFrame())
#except AttributeError: # if timeLayer is of NoneType
# pass
else:
for timeLayer in self.timeLayerList:
if not timeLayer.hasTimeRestriction():
return
timeLayer.deleteTimeRestriction()
#self.emit(SIGNAL('timeRestrictionsRefreshed(PyQt_PyObject)'),self.currentTimePosition)
self.timeRestrictionsRefreshed.emit(self.currentTimePosition)
def registerTimeLayer( self, timeLayer ):
"""Register a new layer for management and update the project's temporal extent"""
self.timeLayerList.append( timeLayer )
if len( self.timeLayerList ) == 1:
# update projectTimeExtents to first layer's timeExtents
self.setProjectTimeExtents(timeLayer.getTimeExtents())
# Set current time to the earliest time record
if self.isFirstRun:
self.setCurrentTimePosition(self.projectTimeExtents[0])
self.isFirstRun = False
else:
self.updateProjectTimeExtents()
self.refresh()
def removeTimeLayer(self,layerId):
"""remove the timeLayer with the given layerId"""
for i in range(0,len(self.timeLayerList)):
if self.timeLayerList[i].getLayerId() == layerId:
self.timeLayerList.pop(i)
# if the last layer was removed:
if not self.hasLayers():
self.setProjectTimeExtents((None,None))
#self.emit(SIGNAL('lastLayerRemoved()'),)
self.lastLayerRemoved.emit()
self.refresh()
def updateProjectTimeExtents(self):
"""Loop through all timeLayers and make sure that the projectTimeExtents cover all layers"""
for timeLayer in self.timeLayerList:
try:
extents = timeLayer.getTimeExtents()
except NotATimeAttributeError:
continue # TODO: we should probably do something useful here
if extents[0] < self.projectTimeExtents[0]:
self.projectTimeExtents = (extents[0],self.projectTimeExtents[1])
if extents[1] > self.projectTimeExtents[1]:
self.projectTimeExtents = (self.projectTimeExtents[0],extents[1])
def setProjectTimeExtents(self,timeExtents):
"""set projectTimeExtents to given time extent and emit signal 'projectTimeExtentsChanged(list)'"""
self.projectTimeExtents = timeExtents
#QMessageBox.information(self.iface.mainWindow(),'Debug Output','setProjectTimeExtents\nTime extents: '+str(timeExtents)+'\nType: '+str(type(timeExtents)))
try:
#self.emit(SIGNAL('projectTimeExtentsChanged(PyQt_PyObject)'),timeExtents)
self.projectTimeExtentsChanged.emit(timeExtents)
except TypeError: # if timeExtent is not specified
pass
def getProjectTimeExtents( self ):
"""Get the overall temporal extent of all managable (managed?) layers"""
return self.projectTimeExtents
def getTimeLayerList( self ):
"""Get the list of managed layers"""
return self.timeLayerList
def setTimeFrameType(self, frameType):
"""Defines the type of the time frame, accepts all values usable by timedelta objects:
days, seconds, microseconds, milliseconds, minutes, hours, weeks"""
#QMessageBox.information(self.iface.mainWindow(),'Debug Output','Time frame type: '+str(frameType))
self.timeFrameType = frameType
self.refresh()
def setTimeFrameSize( self, frameSize ):
"""Defines the size of the time frame"""
#QMessageBox.information(self.iface.mainWindow(),'Debug Output','Time frame size: '+str(frameSize))
self.timeFrameSize = frameSize
self.refresh()
def setCurrentTimePosition( self, timePosition ):
"""Defines the currently selected point in time, which is at the beginning of the time-frame."""
# TODO: Test
if type(timePosition) == QDateTime:
# convert QDateTime to datetime
timePosition = datetime.strptime( str(timePosition.toString('yyyy-MM-dd hh:mm:ss.zzz')) ,"%Y-%m-%d %H:%M:%S.%f")
elif type(timePosition) == int or type(timePosition) == float:
timePosition = datetime.fromordinal(int(timePosition))
self.currentTimePosition = timePosition
#self.emit(SIGNAL('timeRestrictionsRefreshed(PyQt_PyObject)'),self.currentTimePosition)
self.timeRestrictionsRefreshed.emit(self.currentTimePosition)
if self.isEnabled():
self.refresh()
def stepForward(self):
"""Shifts query forward in time by one time frame"""
if self.currentTimePosition != None:
self.currentTimePosition += self.timeFrame()
#self.emit(SIGNAL('timeRestrictionsRefreshed(PyQt_PyObject)'),self.currentTimePosition)
self.timeRestrictionsRefreshed.emit(self.currentTimePosition)
if self.isEnabled():
self.refresh()
def stepBackward(self):
"""Shifts query back in time by one time frame"""
if self.currentTimePosition != None:
self.currentTimePosition -= self.timeFrame()
#self.emit(SIGNAL('timeRestrictionsRefreshed(PyQt_PyObject)'),self.currentTimePosition)
self.timeRestrictionsRefreshed.emit(self.currentTimePosition)
if self.isEnabled():
self.refresh()
def toggleTimeManagement(self):
"""toggle time management on/off"""
if self.timeManagementEnabled:
self.deactivateTimeManagement()
else:
self.activateTimeManagement()
#self.emit(SIGNAL('toggledManagement(PyQt_PyObject)'),self.timeManagementEnabled)
self.toggledManagement.emit(self.timeManagementEnabled)
def activateTimeManagement(self):
"""Enable all temporal constraints on managed (and configured) layers. (Original subsets should still be active.)"""
self.timeManagementEnabled = True
self.refresh()
def deactivateTimeManagement(self):
"""Disable all temporal constraints (and restore original subsets)"""
self.timeManagementEnabled = False
self.refresh()
def getSaveString(self):
"""create a save string that can be put into project file"""
tdfmt = "%Y-%m-%d %H:%M:%S.%f"
saveString = ''
saveListLayers = []
if len(self.projectTimeExtents) > 0:
try: # test if projectTimeExtens are populated with datetimes
datetime.strftime(self.projectTimeExtents[0], tdfmt)
except TypeError: # if Nonetypes:
return (saveString,saveListLayers)
saveString = datetime.strftime(self.projectTimeExtents[0], tdfmt) + ';'
saveString += datetime.strftime(self.projectTimeExtents[1], tdfmt) + ';'
saveString += datetime.strftime(self.currentTimePosition, tdfmt) + ';'
for timeLayer in self.timeLayerList:
saveListLayers.append(timeLayer.getSaveString())
return (saveString,saveListLayers)
def restoreFromSaveString(self, saveString):
"""restore settings from loaded project file"""
tdfmt = "%Y-%m-%d %H:%M:%S.%f"
if saveString:
self.isFirstRun = False
saveString = str(saveString).split(';')
try:
timeExtents = (datetime.strptime(saveString[0], tdfmt),
datetime.strptime(saveString[1], tdfmt))
except ValueError:
try:
# Try converting without the fractional seconds for
# backward compatibility.
tdfmt = "%Y-%m-%d %H:%M:%S"
timeExtents = (datetime.strptime(saveString[0], tdfmt),
datetime.strptime(saveString[1], tdfmt))
except ValueError:
# avoid error message for projects without
# time-managed layers
return
self.projectTimeExtents = timeExtents
self.setCurrentTimePosition(datetime.strptime(saveString[2], tdfmt))
return saveString[3]