-
Notifications
You must be signed in to change notification settings - Fork 4
/
time_tracker.py
304 lines (244 loc) · 10.9 KB
/
time_tracker.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
300
301
302
303
304
"""mutant - MUlti Temporal ANalysis Tool
begin : 2014/06/15
copyright : (c) 2014- by Werner Macho
email : [email protected]
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""
from builtins import object
from qgis.PyQt.QtCore import QObject, Qt, QFileInfo
from qgis.PyQt.QtGui import QPalette, QColor
from qgis.core import QgsProject, QgsMapLayer
import datetime
# work with gdal xml
try:
from osgeo import gdal
from osgeo.gdalconst import *
except ImportError:
import gdal
from gdalconst import *
class TimeTracker(object):
def __init__(self, parent, canvas):
self.layer_times = dict()
self.parent = parent
self.canvas = canvas
self.widget = self.parent.extractionPriorityListWidget
self.cut_first = self.parent.cutFirst
self.select_date = self.parent.dateLength
self.pattern_edit = self.parent.patternLineEdit
self.sample_edit = self.parent.sampleLineEdit
self.write_meta = self.parent.writeMetaPushButton
# This data structure looks like:
# {
# 'layer_id' : datetime.datetime(2014, 2, 23),
# }
self.registry = QgsProject.instance()
def enable_selection(self):
self.registry.layersAdded.connect(self.refresh_tracker)
self.registry.layersRemoved.connect(self.refresh_tracker)
self.widget.itemChanged.connect(self.refresh_tracker)
self.widget.itemChanged.connect(self.validate_date_string)
self.widget.model().rowsMoved.connect(self.refresh_tracker)
self.widget.model().rowsMoved.connect(self.validate_date_string)
self.cut_first.valueChanged.connect(self.cut_first_spinbox_changed)
self.cut_first.valueChanged.connect(self.refresh_tracker)
self.select_date.valueChanged.connect(self.date_length_spinbox_changed)
self.select_date.valueChanged.connect(self.refresh_tracker)
self.pattern_edit.textChanged.connect(self.refresh_tracker)
self.pattern_edit.textChanged.connect(self.validate_date_string)
self.sample_edit.textChanged.connect(self.refresh_tracker)
self.sample_edit.textChanged.connect(self.validate_date_string)
# self.write_meta.clicked.connect(self.write_XML) # FIXME Find function
# to only write XML
self.initiate_values()
def disable_selection(self):
self.registry.layersAdded.disconnect(self.refresh_tracker)
self.registry.layersRemoved.disconnect(self.refresh_tracker)
self.widget.itemChanged.disconnect(self.refresh_tracker)
self.widget.itemChanged.disconnect(self.validate_date_string)
self.widget.model().rowsMoved.disconnect(self.refresh_tracker)
self.widget.model().rowsMoved.disconnect(self.validate_date_string)
self.cut_first.valueChanged.disconnect(self.cut_first_spinbox_changed)
self.cut_first.valueChanged.disconnect(self.refresh_tracker)
self.select_date.valueChanged.disconnect(
self.date_length_spinbox_changed)
self.select_date.valueChanged.disconnect(self.refresh_tracker)
self.pattern_edit.textChanged.disconnect(self.refresh_tracker)
self.pattern_edit.textChanged.disconnect(self.validate_date_string)
self.sample_edit.textChanged.disconnect(self.refresh_tracker)
self.sample_edit.textChanged.disconnect(self.validate_date_string)
def refresh_tracker(self):
# initialise (or re-initialise) self.layer_times
# Empty the dictionary
self.layer_times = {}
# Loop through all raster layers in qgis and call
# track_layer, populating the dictionary
if QgsProject is None:
return
reg = QgsProject.instance()
for layer_id, layer in reg.mapLayers().items():
try:
if layer.type() == QgsMapLayer.RasterLayer:
self.track_layer(layer)
except AttributeError:
pass
def track_layer(self, layer):
"""given a layer, determine its date and write the entry to the data
structure."""
layer_id = layer.id()
# extract time from layers if there is some
self.layer_times[layer_id] = self.extract_time_from_layer(layer)
def get_time_for_layer(self, layer):
"""Retrieves a time from the internal store"""
layer_id = layer.id()
try:
layer_time = self.layer_times[layer_id]
except KeyError:
return None
return layer_time
def extract_time_from_layer(self, layer):
""" Returns the time for the given layer from either its file name,
exif data, tif header or xml file. """
extraction_methods = []
i = 0
list_widget = self.parent.extractionPriorityListWidget
write_meta = self.parent.writeMetaDataCheckBox
while i < list_widget.count():
if list_widget.item(i).text() == "XML" and \
list_widget.item(i).checkState() == Qt.Checked:
extraction_methods.append(
self.extract_time_from_persistent_metadata)
elif list_widget.item(i).text() == "Filename" and \
list_widget.item(i).checkState() == Qt.Checked:
extraction_methods.append(self.extract_time_from_filename)
elif list_widget.item(i).text() == "Exif" and \
list_widget.item(i).checkState() == Qt.Checked:
extraction_methods.append(self.extract_time_from_exif)
elif list_widget.item(i).text() == "TIFF-Header" and \
list_widget.item(i).checkState() == Qt.Checked:
extraction_methods.append(self.extract_time_from_tif)
i += 1
t = None
for extraction_method in extraction_methods:
t = extraction_method(layer)
if t is not None:
break
if write_meta.isChecked():
self.write_time_to_metadata(layer, t)
return t # Is meant to return a datetime.datetime but could actually
# return None
def extract_time_from_persistent_metadata(self, layer):
layer_path = layer.source()
# read from associated *.aux.xml file
ds = gdal.Open(layer_path, GA_ReadOnly)
date = ds.GetMetadataItem('DateTime')
if date is None:
return_date = None
else:
return_date = datetime.datetime.strptime(date, "%Y-%m-%dT%H:%M:%S")
return return_date
def cut_first_spinbox_changed(self):
self.update_sample()
current_sample_len = len(self.parent.sampleLineEdit.text())
self.parent.dateLength.setValue(current_sample_len)
def date_length_spinbox_changed(self):
self.update_sample()
def initiate_values(self):
path_to_layer = None
for layer_id, layer in self.registry.mapLayers().items():
if layer.type() == QgsMapLayer.RasterLayer:
path_to_layer = layer.source()
break
if path_to_layer is None:
return
filename = QFileInfo(path_to_layer).completeBaseName()
if self.parent.cutFirst.value() > 0 and \
self.parent.dateLength.value() > 0:
filename = self.make_strptime_safe(filename)
self.parent.dateLength.setValue(len(filename))
self.parent.sampleLineEdit.setText(filename)
def update_sample(self):
path_to_layer = None
for layer_id, layer in self.registry.mapLayers().items():
if layer.type() == QgsMapLayer.RasterLayer:
path_to_layer = layer.source()
break
if path_to_layer is None:
return
filename = QFileInfo(path_to_layer).completeBaseName()
final_name = self.make_strptime_safe(filename)
# set the value back to GUI
self.parent.sampleLineEdit.setText(final_name)
def make_strptime_safe(self, filename):
cut_front_characters = self.parent.cutFirst.value()
length_of_date = self.parent.dateLength.value()
# cut filename according to the values in GUI
cut_name = filename[cut_front_characters:]
final_name = cut_name[:length_of_date]
return final_name
def extract_time_from_filename(self, layer):
path_to_layer = layer.source()
filename = QFileInfo(path_to_layer).completeBaseName()
trimmed_filename = self.make_strptime_safe(filename)
pattern = self.parent.patternLineEdit.text()
try:
date = datetime.datetime.strptime(trimmed_filename, pattern)
except ValueError:
date = None
# return that date as a datetime.datetime
return date
def validate_date_string(self): # FIXME check for double input e.g. %H%H
list_widget = self.parent.extractionPriorityListWidget
valid = True
pattern_line = self.pattern_edit
pal = QPalette(pattern_line.palette())
pattern = self.parent.patternLineEdit.text()
sample = self.parent.sampleLineEdit.text()
today_string = ''
i = 0
from_file_name_checked = False
# orig_color = pal.background()
while i < list_widget.count():
if list_widget.item(i).text() == "Filename" and \
list_widget.item(i).checkState() == Qt.Checked:
from_file_name_checked = True
i += 1
if not from_file_name_checked:
pal.setColor(QPalette.Base, QColor('white'))
pattern_line.setPalette(pal)
return
try:
datetime.datetime.strptime(sample,
pattern)
except ValueError:
valid = False
try:
today_string = datetime.datetime.strftime(datetime.datetime.today(),
pattern)
except ValueError:
valid = False
if len(today_string) != len(sample):
valid = False
if valid:
pal.setColor(QPalette.Base, QColor(0, 255, 0, 127)) # light green
else:
pal.setColor(QPalette.Base, QColor(255, 0, 0, 127)) # light red
pattern_line.setPalette(pal)
def extract_time_from_exif(self, layer):
# FIXME add functionality here
return # datetime.datetime(2000, 3, 1)
def extract_time_from_tif(self, layer):
# FIXME add functionality here
return # datetime.datetime(2000, 12, 1)
def write_time_to_metadata(self, layer, t):
layer_path = layer.source()
# read from associated *.aux.xml file
ds = gdal.Open(layer_path, GA_Update)
if t is not None:
metadata = t.isoformat()
ds.SetMetadataItem('DateTime', metadata, '')
else:
return
ds.FlushCache()