-
Notifications
You must be signed in to change notification settings - Fork 4
/
mutantwidget.py
1152 lines (994 loc) · 45.7 KB
/
mutantwidget.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""mutant - MUlti Temporal ANalysis Tool
begin : 2014/06/16
copyright : (c) 2014- by Werner Macho
email : [email protected]
based on valuetool
copyright : (C) 2008-2010 by G. Picard
__author__ = '[email protected]'
__date__ = '2014/06/16'
__copyright__ = 'Copyright 2014, Werner Macho'
.. 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.
"""
import fnmatch # Import filtering for Layer names
import datetime # for dealing with Multi-temporal data
import time
import csv
import operator
import os
from distutils.version import StrictVersion
from .time_tracker import TimeTracker
from .applyfilter import ApplyFilter
from qgis.PyQt.QtCore import (
QSettings,
Qt,
QSize,
QEvent,
QFileInfo
)
from qgis.PyQt.QtWidgets import (
QFileDialog,
QSizePolicy,
QWidget,
QMenu,
QApplication,
QTableWidgetItem,
QToolButton,
QActionGroup,
QAction,
QLabel
)
from qgis.PyQt.QtGui import (
QBrush,
QPen,
QIcon
)
from qgis.core import (
Qgis,
QgsMapLayer,
QgsProject,
QgsRasterDataProvider,
QgsCsException,
QgsPoint,
QgsCoordinateTransform,
QgsRaster,
QgsRasterBandStats,
QgsRectangle
)
from PyQt5.uic import loadUiType
FORM_CLASS, _ = loadUiType(os.path.join(os.path.dirname(__file__), 'ui_mutant.ui'))
import logging
# change the level back to logging.WARNING(the default) before releasing
logging.basicConfig(level=logging.DEBUG)
# TODO: Get better debugging
debug = 0
has_qwt = True
try:
from PyQt5.Qwt import QwtPlot, QwtPlotCurve, QwtScaleDiv, QwtSymbol
except ImportError:
has_qwt = False
has_mpl = True
try:
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import matplotlib.dates as dates
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
from .cust.mpl_cust import MplSettings
has_mpl = StrictVersion(matplotlib.__version__) >= StrictVersion('1.0.0')
except ImportError:
has_mpl = False
has_pyqtgraph = True
try:
import pyqtgraph as pg
from .cust.pqg_cust import DateTimeViewBox, DateTimeAxis
if pg.__version__ == "": # assuming debian package with no version
has_pyqtgraph = True
else:
has_pyqtgraph = StrictVersion(pg.__version__) >= StrictVersion('0.9.8')
except ImportError:
has_pyqtgraph = False
pg = False
class MutantWidget(QWidget, FORM_CLASS):
def __init__(self, iface):
self.hasqwt = has_qwt
self.hasmpl = has_mpl
self.haspqg = has_pyqtgraph
self.layerMap = dict()
self.statsChecked = False
self.ymin = 100
self.ymax = 200
self.isActive = False
self.mt_enabled = False
# Statistics (>=1.9)
self.statsSampleSize = 2500000
self.stats = {} # stats per layer
self.layersSelected = []
self.layerBands = dict()
self.iface = iface
self.canvas = self.iface.mapCanvas()
# self.legend = self.iface.legendInterface()
self.legend = QgsProject.instance().layerTreeRoot()
self.logger = logging.getLogger('.'.join((__name__,
self.__class__.__name__)))
QWidget.__init__(self)
self.setupUi(self)
self.tabWidget.setEnabled(False)
self.plotOnMove.setChecked(QSettings().value('plugins/mutant/mouseClick', False, type=bool))
self.window_s = 99
self.power = 3
self.perc_win = 25
self.perc = 75
#self.window_size.setEnabled(True)
#self.power_eq.setEnabled(True)
#self.perc_win_val.setEnabled(True)
#self.percentil.setEnabled(True)
#self.window_size.setText(str(self.window_s))
#self.power_eq.setText(str(self.power))
#self.perc_win_val.setText(str(self.perc_win))
#self.percentil.setText(str(self.perc))
self.leYMin.setText(str(self.ymin))
self.leYMax.setText(str(self.ymax))
self.tracker = TimeTracker(self, self.canvas)
self.filter = ApplyFilter(self, self.canvas)
if has_mpl:
self.mpl_cust = MplSettings(self, self.canvas)
# self.setupUi_plot()
# don't setup plot until Graph(1) tab is clicked - workaround for bug
# #7450
# qgis will still crash in some cases, but at least the tool can be
# used in Table mode
self.qwtPlot = None
self.mplPlot = None
self.mplLine = None
self.plotLibSelector.currentIndexChanged .connect(self.change_plot)
self.tabWidget.currentChanged .connect(self.tabWidgetChanged)
self.layerSelection.currentIndexChanged .connect(self.update_layers)
self.bandSelection.currentIndexChanged .connect(self.update_layers)
self.selectionTable.cellChanged .connect(self.layerSelected)
self.enableMTAnalysesCheckBox.toggled .connect(self.on_mt_analysis_toggled)
self.selectionStringLineEdit.textChanged .connect(self.update_layers)
self.yAutoCheckBox.toggled .connect(self.y_auto_toggle)
self.toggleMutant.toggled .connect(self.catch_errors)
self.exportPushButton.clicked .connect(self.export_values)
# TODO Get Export from graph values
# self.exportPushButton_2.clicked.connect(self.xxxx)
self.registry = QgsProject.instance()
self.registry.layersAdded .connect(self.catch_errors)
self.registry.layersRemoved .connect(self.catch_errors)
self.setupUi_plot()
def catch_errors(self):
if self.toggleMutant.isChecked():
layers = self.activeRasterLayers()
if len(layers) == 0:
if self.canvas.layerCount() > 0:
text = self.tr("Mutant: No valid layers to display - "
"add Rasterlayers")
self.pop_messagebar(text)
self.changeActive(False)
else:
text = self.tr("Mutant: No valid layers to display")
self.pop_messagebar(text)
self.changeActive(False)
self.values = []
return
else:
return
else:
return
def y_auto_toggle(self, state):
# User has toggled automatic (default) y min/max values
if state == 1:
self.leYMin.setEnabled(False)
self.leYMax.setEnabled(False)
self.leYMin.setText(str(self.ymin))
self.leYMax.setText(str(self.ymax))
else:
self.leYMin.setEnabled(True)
self.leYMax.setEnabled(True)
def pop_messagebar(self, text, d_time=5):
if d_time == 0:
self.iface.messageBar().pushWidget(self.iface.messageBar().createMessage(text), Qgis.Warning)
else:
self.iface.messageBar().pushWidget(self.iface.messageBar().createMessage(text), Qgis.Warning, d_time)
def setupUi_plot(self):
# plot
self.plotLibSelector.setVisible(False)
self.enableStatistics.setVisible(False)
# stats by default because estimated are fast
self.enableStatistics.setChecked(True)
plot_count = 0
self.mplLine = None # make sure to invalidate when layers change
if self.hasqwt: # Page 2 - qwt
self.plotLibSelector.addItem('Qwt')
plot_count += 1
# Setup Qwt Plot Area in Widget
self.qwtPlot = QwtPlot(self.stackedWidget)
self.qwtPlot.setAutoFillBackground(False)
self.qwtPlot.setObjectName("qwtPlot")
#self.curve = QwtPlotCurve()
#self.curve.setSymbol(
# QwtSymbol(QwtSymbol.Ellipse,
# QBrush(Qt.white),
# QPen(Qt.red, 2),
# QSize(9, 9)))
#self.curve.attach(self.qwtPlot)
# Size Policy ???
sizePolicy = QSizePolicy(QSizePolicy.Expanding,
QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.qwtPlot.sizePolicy().hasHeightForWidth())
self.qwtPlot.setSizePolicy(sizePolicy)
# Size Policy ???
self.qwtPlot.updateGeometry()
self.stackedWidget.addWidget(self.qwtPlot)
self.qwt_widgetnumber = self.stackedWidget.indexOf(self.qwtPlot)
if self.hasmpl: # Page 3 - setup matplotlib
self.plotLibSelector.addItem('matplotlib')
# If matplotlib is the only one
self.toggleInterpolation.setEnabled(True)
self.toggleInterpolation.setVisible(True)
plot_count += 1
self.mplBackground = None
# http://www.scipy.org/Cookbook/Matplotlib/Animations
self.mplFig = plt.Figure(facecolor='w',
edgecolor='g',
linewidth=0.0)
self.mpl_subplot = self.mplFig.add_subplot(111)
self.pltCanvas = FigureCanvasQTAgg(self.mplFig)
self.pltCanvas.setParent(self.stackedWidget)
self.pltCanvas.setAutoFillBackground(False)
self.pltCanvas.setObjectName("mplPlot")
self.mplPlot = self.pltCanvas
self.mplPlot.updateGeometry()
self.stackedWidget.addWidget(self.mplPlot)
self.mpl_widgetnumber = self.stackedWidget.indexOf(self.mplPlot)
if self.haspqg: # Page 3 - setup PyQtGraph
self.plotLibSelector.addItem('PyQtGraph')
plot_count += 1
# Setup PyQtGraph stuff
pg.setConfigOption('background', 'w')
pg.setConfigOption('foreground', 'k')
self.pqg_axis = DateTimeAxis(orientation='bottom')
self.pqg_plot_widget = pg.PlotWidget(parent=self.stackedWidget,
axisItems={'bottom':
self.pqg_axis})
self.pqg_plot_item = self.pqg_plot_widget.getPlotItem()
self.pqg_plot_widget.updateGeometry()
self.stackedWidget.addWidget(self.pqg_plot_widget)
self.pqg_widgetnumber = self.stackedWidget.indexOf(
self.pqg_plot_widget)
# on zoom change do:
self.pqg_plot_item.sigXRangeChanged .connect(self.refresh_ticks)
if plot_count > 1:
self.plotLibSelector.setEnabled(True)
self.plotLibSelector.setVisible(True)
self.plotLibSelector.setCurrentIndex(0)
if self.hasqwt:
self.plotLibSelector.setCurrentIndex(self.qwt_widgetnumber)
elif self.hasmpl:
self.plotLibSelector.setCurrentIndex(self.mpl_widgetnumber)
else:
self.plotLibSelector.setCurrentIndex(self.pqg_widgetnumber)
self.change_plot()
elif plot_count == 1:
self.plotLibSelector.setCurrentIndex(0)
self.change_plot()
else: # can only be 0 if nothing else matched
message_text = "Mutant cannot find any graphiclibrary for " \
"creating plots. Please install either Qwt >= 5.0 " \
",matplotlib >= 1.0 or PyQtGraph >= 0.9.8!"
self.plot_message = QLabel(message_text)
self.plot_message.setWordWrap(True)
self.stackedWidget.addWidget(self.plot_message)
self.pop_messagebar(message_text)
def change_plot(self):
if self.stackedWidget.count() > 1:
if self.plotLibSelector.currentText() == 'Qwt':
self.stackedWidget.setCurrentIndex(self.qwt_widgetnumber)
self.toggleInterpolation.setDisabled(True)
self.toggleInterpolation.setVisible(False)
elif self.plotLibSelector.currentText() == 'matplotlib':
self.stackedWidget.setCurrentIndex(self.mpl_widgetnumber)
self.toggleInterpolation.setEnabled(True)
self.toggleInterpolation.setVisible(True)
self.mpl_subplot.clear()
elif self.plotLibSelector.currentText() == 'PyQtGraph':
self.stackedWidget.setCurrentIndex(self.pqg_widgetnumber)
self.toggleInterpolation.setDisabled(True)
self.toggleInterpolation.setVisible(False)
self.pqg_plot_widget.clear()
elif self.stackedWidget.count() == 1:
self.stackedWidget.setCurrentIndex(0)
else:
self.stackedWidget.setCurrentIndex(-1)
def keyPressEvent(self, e):
if (e.modifiers() == Qt.ControlModifier or e.modifiers() == Qt.MetaModifier) and e.key() == Qt.Key_C:
items = ''
for rec in range(self.valueTable.rowCount()):
items += '"' + self.valueTable.item(rec, 0).text() + '",' + self.valueTable.item(rec, 1).text() + "\n"
if not items == '':
clipboard = QApplication.clipboard()
clipboard.setText(items)
else:
QWidget.keyPressEvent(self, e)
def changeActive(self, active, gui=True):
if self.isActive == active:
return
self.isActive = active
if active:
self.toggleMutant.setCheckState(Qt.Checked)
self.canvas.layersChanged .connect(self.invalidatePlot)
if not self.plotOnMove.isChecked():
self.canvas.xyCoordinates .connect(self.printValue)
else:
self.toggleMutant.setCheckState(Qt.Unchecked)
self.canvas.layersChanged .disconnect(self.invalidatePlot)
try:
self.canvas.xyCoordinates.disconnect(self.printValue)
except TypeError:
pass # it was not connected so there is nothing to do here
if gui:
self.tabWidget.setEnabled(active)
if active:
self.labelStatus.setText(self.tr("Mutant is enabled!"))
# FIXME: Only on Options Tab?
if self.tabWidget.currentIndex() == 2:
self.update_layers()
else:
self.labelStatus.setText(self.tr(""))
# use this to clear plot when deactivated
# self.values=[]
# self.showValues()
def activeRasterLayers(self, index=None):
layers = []
allLayers = []
if not index:
index = self.layerSelection.currentIndex()
if index == 0: # visible layers
allLayers = self.canvas.layers()
elif index == 1 or index == 3: # All layers | by selection string
allLayers = [ltl.layer() for ltl in self.legend.findLayers()]
elif index == 2: # Manual selection
for layer in [ltl.layer() for ltl in self.legend.findLayers()]:
if layer.id() in self.layersSelected:
allLayers.append(layer)
for layer in allLayers:
if index == 3: # by selection string
# Check if the layer name matches our filter and skip it if it
# doesn't
if not self.name_matches_filter(layer.name()):
continue
if layer is not None and layer.isValid() and \
layer.type() == QgsMapLayer.RasterLayer and \
layer.dataProvider() and \
(layer.dataProvider().capabilities() & QgsRasterDataProvider.IdentifyValue):
layers.append(layer)
return layers
def activeBandsForRaster(self, layer):
activeBands = []
if self.bandSelection.currentIndex() == 1 and layer.renderer():
activeBands = layer.renderer().usesBands()
elif self.bandSelection.currentIndex() == 2:
if layer.bandCount() == 1:
activeBands = [1]
else:
activeBands = self.layerBands[layer.id()] if (layer.id() in self.layerBands) else []
else:
activeBands = list(range(1, layer.bandCount()+1))
return activeBands
def printValue(self, position):
if debug > 0:
print(position)
if not position:
return
if self.tabWidget.currentIndex() == 2:
return
if debug > 0:
print("%d active rasters, %d canvas layers" % (len(
self.activeRasterLayers()), self.canvas.layerCount()))
layers = self.activeRasterLayers()
self.labelStatus.setText(self.tr('Coordinate:') + ' (%f, %f)' % (
position.x(), position.y()))
need_extremum = (self.tabWidget.currentIndex() == 1) # if plot is shown
# count the number of required rows and remember the raster layers
nrow = 0
rasterlayers = []
layersWOStatistics = []
for layer in layers:
nrow += layer.bandCount()
rasterlayers.append(layer)
# check statistics for each band
if need_extremum:
for i in range(1, layer.bandCount()+1):
has_stats = self.get_statistics(layer, i) is not None
if not layer.id() in self.layerMap and not has_stats \
and not layer in layersWOStatistics:
layersWOStatistics.append(layer)
if layersWOStatistics and not self.statsChecked:
self.calculateStatistics(layersWOStatistics)
irow = 0
self.values = []
self.ymin = 1e38
self.ymax = -1e38
mapCanvasSrs = self.iface.mapCanvas().mapSettings().destinationCrs()
# TODO - calculate the min/max values only once,
# instead of every time!!!
# And keep them in a dict() with key=layer.id()
counter = 0
for layer in rasterlayers:
layer_name = str(layer.name())
layer_srs = layer.crs()
pos = position
# if given no position, get dummy values
if position is None:
pos = QgsPoint(0, 0)
# transform points if needed
elif not mapCanvasSrs == layer_srs:
srsTransform = QgsCoordinateTransform(mapCanvasSrs, layer_srs, QgsProject.instance())
try:
pos = srsTransform.transform(position)
except QgsCsException as err:
continue
if True:
if not layer.dataProvider():
continue
ident = None
if position is not None:
canvas = self.iface.mapCanvas()
# first test if point is within map layer extent
# maintain same behaviour as in 1.8 and print out of extent
if not layer.dataProvider().extent().contains(pos):
ident = dict()
for iband in range(1, layer.bandCount()+1):
ident[iband] = str(self.tr('out of extent'))
# we can only use context if layer is not projected
elif layer.dataProvider().crs() != \
canvas.mapSettings().destinationCrs():
ident = layer.dataProvider().identify(pos,
QgsRaster.IdentifyFormatValue).results()
else:
extent = canvas.extent()
width = round(extent.width() /
canvas.mapUnitsPerPixel())
height = round(extent.height() /
canvas.mapUnitsPerPixel())
extent = canvas.mapSettings().mapToLayerCoordinates(
layer, extent)
ident = layer.dataProvider().identify(pos,
QgsRaster.IdentifyFormatValue,
canvas.extent(),
width,
height).results()
if not len(ident) > 0:
continue
# if given no position, set values to 0
if position is None and ident is not None and iter(ident.keys()) is not None:
for key in ident.keys():
ident[key] = layer.dataProvider().noDataValue(key)
# bands displayed depends on cbxBands (all / active / selected)
activeBands = self.activeBandsForRaster(layer)
for iband in activeBands: # loop over the active bands
layer_name_with_band = layer_name
if ident is not None and len(ident) > 1:
layer_name_with_band += ' ' + layer.bandName(iband)
if not ident or iband not in ident:
bandvalue = "?"
else:
bandvalue = ident[iband]
if bandvalue is None:
bandvalue = "no data"
# different x-Axis depending on if we want to use time or
# not
if self.mt_enabled:
layer_time = self.tracker.get_time_for_layer(layer)
if layer_time is None:
continue
else:
# pyqtgraph enabled convert date to epoch
graphlib = self.plotLibSelector.currentText()
if graphlib == 'PyQtGraph':
layer_time = time.mktime(layer_time.timetuple())
# overwrite
tup = (layer_name_with_band,
layer_time,
str(bandvalue))
else:
tup = (layer_name_with_band,
counter+1,
str(bandvalue))
self.values.append(tup)
if need_extremum:
# estimated statistics
stats = self.get_statistics(layer, iband)
if stats:
self.ymin = min(self.ymin, stats.minimumValue)
self.ymax = max(self.ymax, stats.maximumValue)
counter += 1
# Update the ymin, ymax line edits if required
if self.yAutoCheckBox.isChecked():
self.leYMin.setText(str(self.ymin))
self.leYMax.setText(str(self.ymax))
self.values.sort(key=operator.itemgetter(1))
if len(self.values) == 0:
self.labelStatus.setText(self.tr("No valid bands to display"))
self.showValues(position)
def showValues(self, position):
if self.tabWidget.currentIndex() == 1:
if len(self.values) == 0:
# FIXME don't plot if there is no data to plot...
return
else:
self.plot()
else:
self.printInTable(position)
def export_values(self):
path,_ = QFileDialog.getSaveFileName(self, 'Save File', '', 'CSV(*.csv)')
if path != "":
with open(path, 'w') as stream:
writer = csv.writer(stream)
for row in range(self.valueTable.rowCount()):
rowdata = []
for column in range(self.valueTable.columnCount()):
item = self.valueTable.item(row, column)
if item is not None:
rowdata.append(item.text())
else:
rowdata.append('')
writer.writerow(rowdata)
def calculateStatistics(self, layersWOStatistics):
self.invalidatePlot(False)
self.statsChecked = True
layernames = []
for layer in layersWOStatistics:
if not layer.id() in self.layerMap:
layernames.append(layer.name())
if len(layernames) != 0:
if not self.enableStatistics.isChecked():
for layer in layersWOStatistics:
self.layerMap[layer.id()] = True
return
else:
print('ERROR, no layers to get stats for')
save_state = self.isActive
self.changeActive(False, False) # deactivate
# calculate statistics
for layer in layersWOStatistics:
if not layer.id() in self.layerMap:
self.layerMap[layer.id()] = True
for i in range(1, layer.bandCount()+1):
self.get_statistics(layer, i, True)
if save_state:
self.changeActive(True, False) # activate if necessary
# get cached statistics for layer and band or None if not calculated
def get_statistics(self, layer, bandNo, force=False):
if layer in self.stats:
if bandNo in self.stats[layer]:
return self.stats[layer][bandNo]
else:
self.stats[layer] = {}
if force or \
layer.dataProvider().hasStatistics(bandNo,
QgsRasterBandStats.Min | QgsRasterBandStats.Min,
QgsRectangle(),
self.statsSampleSize):
self.stats[layer][bandNo] = \
layer.dataProvider().bandStatistics(bandNo,
QgsRasterBandStats.Min | QgsRasterBandStats.Min,
QgsRectangle(),
self.statsSampleSize)
return self.stats[layer][bandNo]
return None
def printInTable(self, position):
self.valueTable.clearContents()
# set table widget row count
self.valueTable.setRowCount(len(self.values))
posX = position.x()
posY = position.y()
irow = 0
for layername, xval, value in self.values:
if self.valueTable.item(irow, 0) is None:
# create the item
self.valueTable.setItem(irow, 0, QTableWidgetItem())
self.valueTable.setItem(irow, 1, QTableWidgetItem())
self.valueTable.setItem(irow, 2, QTableWidgetItem())
self.valueTable.setItem(irow, 3, QTableWidgetItem())
self.valueTable.setItem(irow, 4, QTableWidgetItem())
self.valueTable.item(irow, 0).setText(layername)
self.valueTable.item(irow, 3).setText(str(posX))
self.valueTable.item(irow, 4).setText(str(posY))
if self.mt_enabled:
graphlib = self.plotLibSelector.currentText()
if graphlib == 'PyQtGraph':
date = datetime.datetime.fromtimestamp(xval)
self.valueTable.item(irow, 2).setText(str(date))
else:
self.valueTable.item(irow, 2).setText(str(xval))
# else:
# self.valueTable.item(irow, 2).setText('')
if value == 'no data' or value == 'out of extent':
self.valueTable.item(irow, 1).setText(value)
else:
value = float(value)
if self.cbxDigits.isChecked():
try:
x = str(self.spinDigits.value())
y = "{:."+x+"f}"
value = y.format(value)
except ValueError:
pass
self.valueTable.item(irow, 1).setData(Qt.EditRole, value)
irow += 1
def refresh_ticks(self):
# At this point the X extent has been changed (e.g. zoom) and we need
# to redraw ticks and associated labels
major_tick_times = []
# define label width as minimum label distance readable
label_width = 40
# First determine what visible x range we are looking at
view_min_x = self.pqg_plot_item.getViewBox().viewRange()[0][0]
view_max_x = self.pqg_plot_item.getViewBox().viewRange()[0][1]
min_date_axis = datetime.datetime.fromtimestamp(view_min_x)
view_range = view_max_x - view_min_x
# Determine the current width of the plot in px
rect_bound = self.pqg_plot_item.viewGeometry()
width = rect_bound.width()
major_label_count = (width - label_width) // label_width
major_label_spacing = view_range // (major_label_count * 3600.0 * 24
* 365.25) # In major tick units
min_tick_to_label_int = int(datetime.datetime.strftime(min_date_axis, '%Y')) + 1
major_label_spacing_delta = view_range / major_label_count
min_tick_to_label_stamp = (datetime.datetime(min_tick_to_label_int, 1, 1) -
datetime.datetime(1970, 1, 1)).total_seconds()
major_tick_times.append((int(min_tick_to_label_stamp),
str(datetime.datetime.fromtimestamp(
min_tick_to_label_stamp).strftime('%Y'))))
next_tick_to_label_int = int((datetime.datetime(
min_tick_to_label_int, 1,
1) - datetime.datetime(
1970, 1, 1)).total_seconds())
next_tick_to_label_stamp = min_tick_to_label_stamp
while True:
next_tick_to_label_int += int(major_label_spacing_delta)
next_tick_to_label_stamp += major_label_spacing_delta
if next_tick_to_label_int > int(view_max_x):
break
major_tick_times.append((next_tick_to_label_int,
str(datetime.datetime.fromtimestamp(
next_tick_to_label_stamp).strftime(
'%Y'))))
# An experiment
# ticks = [
# [(631152000.34, '1990'), (788918400, '1995')],
# [(662688000, '1991'),(694224000.46, '1992'),
# (725846400, '1993'), (757382400, '1994')]
# ]
self.pqg_axis.setTicks([major_tick_times])
def plot(self):
data_values = []
x_values = []
do_filter = self.toggleFilter.isChecked()
do_interpolation = self.toggleInterpolation.isChecked()
if self.hasqwt or self.hasmpl or self.haspqg:
for layername, xval, value in self.values:
x_values.append(xval)
try:
data_values.append(float(value))
except ValueError:
data_values.append(None)
# instead to not be plotted, be aware of min()
# calculation in plotting range = affects graphs (check
# there?
if not data_values:
data_values = [0]
#self.window_s = int(self.window_size.text())
#self.power = int(self.power_eq.text())
#self.perc_win = int(self.perc_win_val.text())
#self.perc = int(self.percentil.text())
if self.yAutoCheckBox.isChecked():
ymin = self.ymin
ymax = self.ymax
else:
# Beware the user may not have entered a number
try:
ymin = float(self.leYMin.text())
ymax = float(self.leYMax.text())
except ValueError:
message = 'Valuetool: Please enter Numbers!'
self.pop_messagebar(message)
return
# Qwt Plot
if self.hasqwt and (self.plotLibSelector.currentText() == 'Qwt'):
self.qwtPlot.setAxisMaxMinor(QwtPlot.xBottom, 0)
# self.qwtPlot.setAxisMaxMajor(QwtPlot.xBottom,0)
self.qwtPlot.setAxisScale(QwtPlot.xBottom, 1, len(self.values))
# self.qwtPlot.setAxisScale(QwtPlot.yLeft, self.ymin, self.ymax)
self.qwtPlot.setAxisScale(QwtPlot.yLeft, ymin, ymax)
try:
qwtx, qwtydata = list(zip(*[x for x in zip(x_values, data_values) if x[1] is not None]))
except ValueError:
return
#self.curve.setSamples(list(range(1, len(qwtydata)+1)), qwtydata)
self.qwtPlot.replot()
self.qwtPlot.setVisible(len(qwtydata) > 0)
# matplotlib Plot
elif self.hasmpl and (self.plotLibSelector.currentText() ==
'matplotlib'):
# don't clear to draw another row of data
self.mpl_subplot.clear()
self.mpl_cust.mpl_setup()
# If Multi-temporal Analysis enabled set xAxis away from Standard
# 1 to values to dates.
# Plot code from here
xv = np.asarray(x_values)
dv = np.asarray(data_values)
dvd = dv.astype(np.double)
datav_mask = np.isfinite(dvd)
if do_interpolation:
xaxis = xv[datav_mask]
yaxis = dvd[datav_mask]
else:
xaxis = x_values
yaxis = data_values
self.mpl_subplot.plot_date(xaxis,
yaxis,
#linestyle='-',
xdate=self.mt_enabled,
ydate=False,
marker='o',
markersize=4,
color='k',
markerfacecolor='b',
markeredgecolor='b')
if self.mt_enabled:
plt.xticks(rotation='vertical')
self.mpl_cust.mpl_date_settings(x_values, ymin, ymax)
else:
self.mpl_cust.mpl_value_settings(x_values, ymin, ymax)
if do_filter:
derived_x, derived_y = self.filter.smooth(xaxis, yaxis,window=self.window_s,polyorder=self.power,perc=self.perc,p_window=self.perc_win)
self.mpl_subplot.plot_date(derived_x,
derived_y,
linestyle='-',
xdate=self.mt_enabled,
ydate=False,
marker='x',
markersize=2,
color='r',
markerfacecolor='r',
markeredgecolor='r')
self.mplFig.canvas.draw()
# PyQtGraph Plot
elif self.haspqg and (self.plotLibSelector.currentText() == 'PyQtGraph'):
# clear on plot - don't clear if additional data should be added
self.pqg_plot_widget.clear() # clean canvas on call
# self.pqg_plot_item.clear() # clear item
# self.pqg_plot_widget.scene().removeItem(legend)
self.pqg_plot_item.setYRange(ymin, ymax)
style_normal = Qt.SolidLine
# Qt.DashLine, Qt.DotLine, Qt.DashDotLine, Qt.DashDotDotLine, Qt.CustomDashLine
style_filter = Qt.SolidLine
# filter None values out of input data
try:
pgxaxis, pgyaxis = list(zip(*[x for x in zip(x_values, data_values) if x[1] is not None]))
except ValueError:
return
self.pqg_plot_widget.plot(pgxaxis,
pgyaxis,
name='data',
symbol='o',
pen=pg.mkPen(color='k',
width=1,
style=style_normal))
if do_filter:
derived_x, derived_y = self.filter.smooth(pgxaxis, pgyaxis,window=self.window_s,polyorder=self.power,perc=self.perc,p_window=self.perc_win)
self.pqg_plot_widget.plot(derived_x,
derived_y,
name='filter',
symbol='x',
pen=pg.mkPen(color='r',
width=1,
style=style_filter))
def invalidatePlot(self, replot=True):
if self.tabWidget.currentIndex() == 2:
self.update_layers()
if not self.isActive:
return
self.statsChecked = False
if self.mplLine is not None:
del self.mplLine
self.mplLine = None
# update empty plot
if replot and self.tabWidget.currentIndex() == 1:
# self.values=[]
self.printValue(None)
def resizeEvent(self, event):
self.invalidatePlot()
def tabWidgetChanged(self):
if self.tabWidget.currentIndex() == 2:
self.update_layers()
def on_mt_analysis_toggled(self, new_state):
if new_state == 1:
self.mt_enabled = True
if self.haspqg:
self.pqg_axis.set_time_enabled(True)
if self.hasqwt and self.plotLibSelector.currentText() == 'Qwt':
message = 'Mutant: There is currently no support using Date ' \
'values while using the Qwt plotlibrary'
self.pop_messagebar(message, 0)
self.tracker.enable_selection()
self.priorityLabel.setEnabled(True)
self.extractionPriorityListWidget.setEnabled(True)
self.patternLabel.setEnabled(True)
self.patternLineEdit.setEnabled(True)
self.writeMetaDataCheckBox.setEnabled(True)
self.labelStatus.setText(self.tr("Multi-temporal analysis "
"enabled!"))
self.cutFirst.setEnabled(True)
self.dateLength.setEnabled(True)
self.sampleLineEdit.setEnabled(True)
self.sampleLabel.setEnabled(True)
self.tracker.refresh_tracker() # Only call when mt_enabled
else:
self.mt_enabled = False
if self.haspqg:
self.pqg_axis.set_time_enabled(False)
self.priorityLabel.setEnabled(False)
self.extractionPriorityListWidget.setEnabled(False)
self.patternLabel.setEnabled(False)
self.patternLineEdit.setEnabled(False)
self.writeMetaDataCheckBox.setEnabled(False)
self.labelStatus.setText(self.tr(""))
self.cutFirst.setEnabled(False)
self.dateLength.setEnabled(False)
self.sampleLineEdit.setEnabled(False)
self.sampleLabel.setEnabled(False)
self.tracker.disable_selection()
def name_matches_filter(self, name):
selection_string = self.selectionStringLineEdit.text()
return fnmatch.fnmatchcase(name, selection_string)
# update active layers in table
def update_layers(self):
if self.tabWidget.currentIndex() != 2:
return
if self.layerSelection.currentIndex() == 3: # by selection string
self.selectionStringLineEdit.setEnabled(True)