Newer
Older
# -*- coding: utf-8 -*-
"""
/***************************************************************************
HUB TimeSeriesViewer
-------------------
begin : 2017-08-04
git sha : $Format:%H$
copyright : (C) 2017 by HU-Berlin
email : benjamin.jakimow@geo.hu-berlin.de
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
"""
# noinspection PyPep8Naming
from __future__ import absolute_import
import os, sys, pickle, datetime
from qgis.gui import *
from qgis.core import *
from PyQt4.QtCore import *
from PyQt4.QtXml import *
from PyQt4.QtGui import *
from timeseriesviewer import jp, SETTINGS
from timeseriesviewer.timeseries import *
from timeseriesviewer.utils import SpatialExtent, SpatialPoint, px2geo
from timeseriesviewer.ui.docks import TsvDockWidgetBase, loadUI
from timeseriesviewer.plotstyling import PlotStyle, PlotStyleButton
from timeseriesviewer.pixelloader import PixelLoader, PixelLoaderTask
from timeseriesviewer.sensorvisualization import SensorListModel
import pyqtgraph as pg
from pyqtgraph import functions as fn
from pyqtgraph import AxisItem
import datetime
from osgeo import gdal, gdal_array
import numpy as np
LABEL_DN = 'DN or Index'
LABEL_TIME = 'Date'
OPENGL_AVAILABLE = False
try:
import OpenGL
OPENGL_AVAILABLE = True
except:
pass
def getTextColorWithContrast(c):
assert isinstance(c, QColor)
if c.lightness() < 0.5:
return QColor('white')
else:
return QColor('black')
def bandIndex2bandKey(i):
assert isinstance(i, int)
assert i >= 0
return 'b{}'.format(i + 1)
def bandKey2bandIndex(key):
match = PlotSettingsModel.regBandKeyExact.search(key)
assert match
idx = int(match.group()[1:]) - 1
return idx
def selectedModelIndices(tableView):
assert isinstance(tableView, QTableView)
result = {}
sm = tableView.selectionModel()
m = tableView.model()
if isinstance(sm, QItemSelectionModel) and isinstance(m, QAbstractItemModel):
for idx in sm.selectedIndexes():
assert isinstance(idx, QModelIndex)
if idx.row() not in result.keys():
result[idx.row()] = idx
return result.values()
class DateTimeAxis(pg.AxisItem):
def __init__(self, *args, **kwds):
super(DateTimeAxis, self).__init__(*args, **kwds)
self.setRange(1,3000)
self.enableAutoSIPrefix(False)
self.labelAngle = 0
def logTickStrings(self, values, scale, spacing):
s = ""
def tickStrings(self, values, scale, spacing):
strns = []
if len(values) == 0:
return []
#assert isinstance(values[0],
values = [num2date(v) if v > 0 else num2date(1) for v in values]
rng = max(values)-min(values)
ndays = rng.astype(int)
strns = []
for v in values:
if ndays == 0:
strns.append(v.astype(str))
strns.append(v.astype(str))
return strns
def tickValues(self, minVal, maxVal, size):
d = super(DateTimeAxis, self).tickValues(minVal, maxVal, size)
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
def drawPicture(self, p, axisSpec, tickSpecs, textSpecs):
p.setRenderHint(p.Antialiasing, False)
p.setRenderHint(p.TextAntialiasing, True)
## draw long line along axis
pen, p1, p2 = axisSpec
p.setPen(pen)
p.drawLine(p1, p2)
p.translate(0.5, 0) ## resolves some damn pixel ambiguity
## draw ticks
for pen, p1, p2 in tickSpecs:
p.setPen(pen)
p.drawLine(p1, p2)
## Draw all text
if self.tickFont is not None:
p.setFont(self.tickFont)
p.setPen(self.pen())
#for rect, flags, text in textSpecs:
# p.drawText(rect, flags, text)
# # p.drawRect(rect)
#see https://github.com/pyqtgraph/pyqtgraph/issues/322
for rect, flags, text in textSpecs:
p.save() # save the painter state
p.translate(rect.center()) # move coordinate system to center of text rect
p.rotate(self.labelAngle) # rotate text
p.translate(-rect.center()) # revert coordinate system
p.drawText(rect, flags, text)
p.restore() # restore the painter state
def __init__(self, *args, **kwds):
# menu creation is deferred because it is expensive and often
# the user will never see the menu anyway.
self.menu = None
def boundingRect(self):
return super(_SensorPoints,self).boundingRect()
def paint(self, p, *args):
super(_SensorPoints, self).paint(p, *args)
# On right-click, raise the context menu
def mouseClickEvent(self, ev):
if ev.button() == QtCore.Qt.RightButton:
if self.raiseContextMenu(ev):
ev.accept()
def raiseContextMenu(self, ev):
menu = self.getContextMenus()
# Let the scene add on to the end of our context menu
# (this is optional)
menu = self.scene().addParentContextMenus(self, menu, ev)
pos = ev.screenPos()
menu.popup(QtCore.QPoint(pos.x(), pos.y()))
return True
# This method will be called when this item's _children_ want to raise
# a context menu that includes their parents' menus.
def getContextMenus(self, event=None):
if self.menu is None:
self.menu = QMenu()
self.menu.setTitle(self.name + " options..")
green = QAction("Turn green", self.menu)
green.triggered.connect(self.setGreen)
self.menu.addAction(green)
self.menu.green = green
blue = QAction("Turn blue", self.menu)
blue.triggered.connect(self.setBlue)
self.menu.addAction(blue)
self.menu.green = blue
alpha = QWidgetAction(self.menu)
alphaSlider = QSlider()
alphaSlider.setOrientation(QtCore.Qt.Horizontal)
alphaSlider.setMaximum(255)
alphaSlider.setValue(255)
alphaSlider.valueChanged.connect(self.setAlpha)
alpha.setDefaultWidget(alphaSlider)
self.menu.addAction(alpha)
self.menu.alpha = alpha
self.menu.alphaSlider = alphaSlider
return self.menu
class TemporalProfilePlotDataItem(pg.PlotDataItem):
def __init__(self, plotStyle, parent=None):
assert isinstance(plotStyle, TemporalProfilePlotStyle)
super(TemporalProfilePlotDataItem, self).__init__([], [], parent=parent)

benjamin.jakimow@geo.hu-berlin.de
committed
self.menu = None
#self.setFlags(QGraphicsItem.ItemIsSelectable)

benjamin.jakimow@geo.hu-berlin.de
committed
self.setAcceptedMouseButtons(Qt.LeftButton | Qt.RightButton)
self.mPlotStyle.sigUpdated.connect(self.updateDataAndStyle)

benjamin.jakimow@geo.hu-berlin.de
committed
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
# On right-click, raise the context menu
def mouseClickEvent(self, ev):
if ev.button() == QtCore.Qt.RightButton:
if self.raiseContextMenu(ev):
ev.accept()
def raiseContextMenu(self, ev):
menu = self.getContextMenus()
# Let the scene add on to the end of our context menu
# (this is optional)
menu = self.scene().addParentContextMenus(self, menu, ev)
pos = ev.screenPos()
menu.popup(QtCore.QPoint(pos.x(), pos.y()))
return True
# This method will be called when this item's _children_ want to raise
# a context menu that includes their parents' menus.
def getContextMenus(self, event=None):
if self.menu is None:
self.menu = QMenu()
self.menu.setTitle(self.name + " options..")
green = QAction("Turn green", self.menu)
green.triggered.connect(self.setGreen)
self.menu.addAction(green)
self.menu.green = green
blue = QAction("Turn blue", self.menu)
blue.triggered.connect(self.setBlue)
self.menu.addAction(blue)
self.menu.green = blue
alpha = QWidgetAction(self.menu)
alphaSlider = QSlider()
alphaSlider.setOrientation(QtCore.Qt.Horizontal)
alphaSlider.setMaximum(255)
alphaSlider.setValue(255)
alphaSlider.valueChanged.connect(self.setAlpha)
alpha.setDefaultWidget(alphaSlider)
self.menu.addAction(alpha)
self.menu.alpha = alpha
self.menu.alphaSlider = alphaSlider
return self.menu
def updateDataAndStyle(self):
TP = self.mPlotStyle.temporalProfile()
sensor = self.mPlotStyle.sensor()
if isinstance(TP, TemporalProfile) and isinstance(sensor, SensorInstrument):
x, y = TP.dataFromExpression(self.mPlotStyle.sensor(), self.mPlotStyle.expression())
x = np.asarray(x, dtype=np.float)
y = np.asarray(y, dtype=np.float)
self.setData(x=[], y=[]) #dummy
self.updateStyle()
def updateStyle(self):
"""
Updates visibility properties
"""
if DEBUG:
print('{} updateStyle'.format(self))
from pyqtgraph.graphicsItems.ScatterPlotItem import drawSymbol
# path = drawSymbol(p, self.markerSymbol, self.markerSize, self.markerPen, self.markerBrush)
# #painter, symbol, size, pen, brush
self.setSymbol(self.mPlotStyle.markerSymbol)
self.setSymbolSize(self.mPlotStyle.markerSize)
self.setSymbolBrush(self.mPlotStyle.markerBrush)
self.setSymbolPen(self.mPlotStyle.markerPen)
self.update()
#self.setPen(fn.mkPen(self.mPlotStyle.linePen))
#self.setFillBrush(fn.mkBrush(self.mPlotStyle.mExpression))
#self.setSymbolBrush(fn.mkBrush(self.mPlotStyle.markerBrush))
# self.setFillBrush(self.mPlotStyle.)
#self.update()
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
def setClickable(self, b, width=None):
assert isinstance(b, bool)
self.curve.setClickable(b, width=width)
def setColor(self, color):
if not isinstance(color, QColor):
color = QColor(color)
self.setPen(color)
def pen(self):
return fn.mkPen(self.opts['pen'])
def color(self):
return self.pen().color()
def setLineWidth(self, width):
pen = pg.mkPen(self.opts['pen'])
assert isinstance(pen, QPen)
pen.setWidth(width)
self.setPen(pen)
class PlotSettingsWidgetDelegate(QStyledItemDelegate):
"""
def __init__(self, tableView, timeSeries, temporalProfileListModel, parent=None):
super(PlotSettingsWidgetDelegate, self).__init__(parent=parent)
self._preferedSize = QgsFieldExpressionWidget().sizeHint()
self.tableView = tableView
self.timeSeries = timeSeries
self.temporalProfileListModel = temporalProfileListModel
def setItemDelegates(self, tableView):
assert isinstance(tableView, QTableView)
model = tableView.model()
assert isinstance(model, PlotSettingsModel)
for c in [model.cnSensor, model.cnExpression, model.cnStyle, model.cnTemporalProfile]:
i = model.columNames.index(c)
tableView.setItemDelegateForColumn(i, self)
def getColumnName(self, index):
assert index.isValid()
model = index.model()
assert isinstance(model, PlotSettingsModel)
return model.columNames[index.column()]
"""
def sizeHint(self, options, index):
s = super(ExpressionDelegate, self).sizeHint(options, index)
exprString = self.tableView.model().data(index)
l = QLabel()
l.setText(exprString)
x = l.sizeHint().width() + 100
s = QSize(x, s.height())
return self._preferedSize
"""
def createEditor(self, parent, option, index):
cname = self.getColumnName(index)
model = self.tableView.model()
w = None
if index.isValid() and isinstance(model, PlotSettingsModel):
plotStyle = model.idx2plotStyle(index)
if isinstance(plotStyle, TemporalProfilePlotStyle):
if cname == model.cnExpression:
w = QgsFieldExpressionWidget(parent=parent)
#todo: w.setLayer(sv.memLyr)
w.setExpressionDialogTitle('Values')
w.setToolTip('Set an expression to specify the image band or calculate a spectral index.')
w.fieldChanged.connect(lambda : self.checkData(w, w.expression()))
elif cname == model.cnStyle:
w = PlotStyleButton(parent=parent)
w.setPlotStyle(plotStyle)
w.setToolTip('Set style.')
w.sigPlotStyleChanged.connect(lambda: self.checkData(w, w.plotStyle()))
elif cname == model.cnSensor:
w = QComboBox(parent=parent)
m = SensorListModel(self.timeSeries)
w.setModel(m)
elif cname == model.cnTemporalProfile:
w = QComboBox(parent=parent)
w.setModel(self.temporalProfileListModel)
else:
raise NotImplementedError()
return w
def checkData(self, w, expression):
if isinstance(w, QgsFieldExpressionWidget):
assert expression == w.expression()
assert w.isExpressionValid(expression) == w.isValidExpression()
if w.isValidExpression():
self.commitData.emit(w)
else:
s = ""
#print(('Delegate commit failed',w.asExpression()))
self.commitData.emit(w)
def setEditorData(self, editor, index):
cname = self.getColumnName(index)
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
model = self.tableView.model()
w = None
if index.isValid() and isinstance(model, PlotSettingsModel):
cname = self.getColumnName(index)
if cname == model.cnExpression:
lastExpr = index.model().data(index, Qt.DisplayRole)
assert isinstance(editor, QgsFieldExpressionWidget)
editor.setProperty('lastexpr', lastExpr)
editor.setField(lastExpr)
elif cname == model.cnStyle:
style = index.data()
assert isinstance(editor, PlotStyleButton)
editor.setPlotStyle(style)
elif cname == model.cnSensor:
assert isinstance(editor, QComboBox)
m = editor.model()
assert isinstance(m, SensorListModel)
sensor = index.data(role=Qt.UserRole)
if isinstance(sensor, SensorInstrument):
idx = m.sensor2idx(sensor)
editor.setCurrentIndex(idx.row())
elif cname == model.cnTemporalProfile:
assert isinstance(editor, QComboBox)
m = editor.model()
assert isinstance(m, TemporalProfileCollectionListModel)
TP = index.data(role=Qt.UserRole)
if isinstance(TP, TemporalProfile):
idx = m.tp2idx(TP)
editor.setCurrentIndex(idx)
else:
raise NotImplementedError()
def setModelData(self, w, model, index):
cname = self.getColumnName(index)
if index.isValid() and isinstance(model, PlotSettingsModel):
if cname == model.cnExpression:
assert isinstance(w, QgsFieldExpressionWidget)
expr = w.asExpression()
exprLast = model.data(index, Qt.DisplayRole)
if w.isValidExpression() and expr != exprLast:
model.setData(index, w.asExpression(), Qt.EditRole)
elif cname == model.cnStyle:
assert isinstance(w, PlotStyleButton)
model.setData(index, w.plotStyle(), Qt.EditRole)
elif cname == model.cnSensor:
assert isinstance(w, QComboBox)
sensor = w.itemData(w.currentIndex(), role=Qt.UserRole)
assert isinstance(sensor, SensorInstrument)
model.setData(index, sensor, Qt.EditRole)
elif cname == model.cnTemporalProfile:
assert isinstance(w, QComboBox)
TP = w.itemData(w.currentIndex(), role=Qt.UserRole)
assert isinstance(TP, TemporalProfile)
model.setData(index, TP, Qt.EditRole)
else:
raise NotImplementedError()
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
class SensorPixelDataMemoryLayer(QgsVectorLayer):
def __init__(self, sensor, crs=None):
assert isinstance(sensor, SensorInstrument)
if crs is None:
crs = QgsCoordinateReferenceSystem('EPSG:4862')
uri = 'Point?crs={}'.format(crs.authid())
super(SensorPixelDataMemoryLayer, self).__init__(uri, 'Pixels_sensor_' + sensor.name(), 'memory', False)
self.mSensor = sensor
#initialize fields
assert self.startEditing()
# standard field names, types, etc.
fieldDefs = [('pxid', QVariant.String, 'integer'),
('date', QVariant.String, 'char'),
('doy', QVariant.Int, 'integer'),
('geo_x', QVariant.Double, 'decimal'),
('geo_y', QVariant.Double, 'decimal'),
('px_x', QVariant.Int, 'integer'),
('px_y', QVariant.Int, 'integer'),
]
# one field for each band
for b in range(sensor.nb):
fName = 'b{}'.format(b + 1)
fieldDefs.append((fName, QVariant.Double, 'decimal'))
# initialize fields
for fieldDef in fieldDefs:
field = QgsField(fieldDef[0], fieldDef[1], fieldDef[2])
self.addAttribute(field)
self.commitChanges()
def sensor(self):
return self.mSensor
def nPixels(self):
raise NotImplementedError()
def dates(self):
raise NotImplementedError()
class TemporalProfileCollectionListModel(QAbstractListModel):
def __init__(self, temporalProfileCollection, *args, **kwds):
super(TemporalProfileCollectionListModel, self).__init__(*args, **kwds)
assert isinstance(temporalProfileCollection, TemporalProfileCollection)
self.mTPColl = temporalProfileCollection
self.mTPColl.rowsAboutToBeInserted.connect(self.rowsAboutToBeInserted)
self.mTPColl.rowsInserted.connect(self.rowsInserted.emit)
#self.mTPColl.rowsAboutToBeRemoved.connect(self.rowsAboutToBeRemoved)
self.mTPColl.rowsRemoved.connect(lambda : self.modelReset.emit())
def idx2tp(self, *args, **kwds):
return self.mTPColl.idx2tp(*args, **kwds)
def tp2idx(self, *args, **kwds):
return self.mTPColl.tp2idx(*args, **kwds)
def flags(self, index):
if index.isValid():
flags = Qt.ItemIsEnabled | Qt.ItemIsSelectable
return flags
#return item.qt_flags(index.column())
return Qt.NoItemFlags
def rowCount(self, *args, **kwds):
return self.mTPColl.rowCount(*args, **kwds)
def data(self, index, role=Qt.DisplayRole):
if role is None or not index.isValid():
return None
TP = self.mTPColl.idx2tp(index)
value = None
if isinstance(TP, TemporalProfile):
if role == Qt.DisplayRole:
value = '{}'.format(TP.name())
elif role == Qt.ToolTipRole:
value = '#{} "{}" {}'.format(TP.mID, TP.name(), TP.mCoordinate)
elif role == Qt.UserRole:
value = TP
return value
class TemporalProfileCollection(QAbstractTableModel):
A collection to store the TemporalProfile data delivered by a PixelLoader
"""
#sigSensorAdded = pyqtSignal(SensorInstrument)
#sigSensorRemoved = pyqtSignal(SensorInstrument)
#sigPixelAdded = pyqtSignal()
#sigPixelRemoved = pyqtSignal()
sigTemporalProfilesAdded = pyqtSignal(list)
sigTemporalProfilesRemoved = pyqtSignal(list)
sigMaxProfilesChanged = pyqtSignal(int)
super(TemporalProfileCollection, self).__init__()
#self.sensorPxLayers = dict()
#self.memLyrCrs = QgsCoordinateReferenceSystem('EPSG:4326')
self.mcnID = 'id'
self.mcnCoordinate = 'Coordinate'
self.mcnLoaded = 'Loading'
self.mcnName = 'Name'
self.mColumNames = [self.mcnName, self.mcnLoaded, self.mcnCoordinate]
crs = QgsCoordinateReferenceSystem('EPSG:4862')
uri = 'Point?crs={}'.format(crs.authid())
self.TS = None
self.mLocations = QgsVectorLayer(uri, 'LOCATIONS', 'memory', False)
self.mTemporalProfiles = []
self.mTPLookupSpatialPoint = {}
self.mTPLookupID = {}
self.mMaxProfiles = 10
self.nextID = 0
def __len__(self):
return len(self.mTemporalProfiles)
def __iter__(self):
return iter(self.mTemporalProfiles)
def __getitem__(self, slice):
return self.mTemporalProfiles[slice]
def __contains__(self, item):
return item in self.mTemporalProfiles
def rowCount(self, parent=None, *args, **kwargs):
return len(self.mTemporalProfiles)
def columnCount(self, QModelIndex_parent=None, *args, **kwargs):
return len(self.mColumNames)
def idx2tp(self, index):
if index.isValid() and index.row() < len(self.mTemporalProfiles) :
return self.mTemporalProfiles[index.row()]
return None
def tp2idx(self, temporalProfile):
assert isinstance(temporalProfile, TemporalProfile)
row = self.mTemporalProfiles.index(temporalProfile)
return self.createIndex(row, 0)
else:
return QModelIndex()
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
def data(self, index, role = Qt.DisplayRole):
if role is None or not index.isValid():
return None
value = None
columnName = self.mColumNames[index.column()]
TP = self.idx2tp(index)
if not isinstance(TP, TemporalProfile):
return None
#self.mColumNames = ['id','coordinate','loaded']
if role == Qt.DisplayRole:
if columnName == self.mcnID:
value = TP.mID
elif columnName == self.mcnName:
value = TP.name()
elif columnName == self.mcnCoordinate:
value = '{}'.format(TP.mCoordinate)
elif columnName == self.mcnLoaded:
nIs, nMax = TP.loadingStatus()
if nMax > 0:
value = '{}/{} ({:0.2f} %)'.format(nIs, nMax, float(nIs) / nMax * 100)
elif role == Qt.EditRole:
if columnName == self.mcnName:
value = TP.name()
elif role == Qt.ToolTipRole:
if columnName == self.mcnID:
value = 'ID Temporal Profile'
elif columnName == self.mcnName:
value = TP.name()
elif columnName == self.mcnCoordinate:
value = '{}'.format(TP.mCoordinate)
elif columnName == self.mcnLoaded:
nIs, nMax = TP.loadingStatus()
value = '{}'.format(TP.mCoordinate)
elif role == Qt.UserRole:
value = TP
return value
def flags(self, index):
if index.isValid():
flags = Qt.ItemIsEnabled | Qt.ItemIsSelectable
cName = self.mColumNames[index.column()]
if cName == self.mcnName:
flags = flags | Qt.ItemIsEditable
return flags
#return item.qt_flags(index.column())
return None
def setData(self, index, value, role=None):
if role is None or not index.isValid():
return None
cName = self.mColumNames[index.column()]
TP = self.idx2tp(index)
if isinstance(TP, TemporalProfile):
if role == Qt.EditRole and cName == self.mcnName:
if len(value) == 0: #do not accept empty strings
return False
else:
TP.setName(value)
return True
return False
def headerData(self, col, orientation, role):
if Qt is None:
return None
if role == Qt.DisplayRole:
if orientation == Qt.Horizontal:
return self.mColumNames[col]
elif orientation == Qt.Vertical:
return col
return None
def insertTemporalProfiles(self, temporalProfiles, i=None):
if isinstance(temporalProfiles, TemporalProfile):
temporalProfiles = [temporalProfiles]
assert isinstance(temporalProfiles, list)
for temporalProfile in temporalProfiles:
assert isinstance(temporalProfile, TemporalProfile)
if i is None:
i = len(self.mTemporalProfiles)
if len(temporalProfiles) > 0:
self.beginInsertRows(QModelIndex(), i, i + len(temporalProfiles) - 1)
for temporalProfile in temporalProfiles:
assert isinstance(temporalProfile, TemporalProfile)
id = self.nextID
self.nextID += 1
temporalProfile.mID = id
self.mTemporalProfiles.insert(i, temporalProfile)
self.mTPLookupID[id] = temporalProfile
self.mTPLookupSpatialPoint[temporalProfile.mCoordinate] = temporalProfile
i += 1
self.endInsertRows()
self.sigTemporalProfilesAdded.emit(temporalProfiles)
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
def temporalProfileFromGeometry(self, geometry):
if geometry in self.mTPLookupSpatialPoint.keys():
return self.mTPLookupSpatialPoint[geometry]
else:
return None
def temporalProfileFromID(self, id):
if id in self.mTPLookupID.keys():
return self.mTPLookupID[id]
else:
return None
def id(self, temporalProfile):
"""
Returns the id of an TemporalProfile
:param temporalProfile: TemporalProfile
:return: id or None, inf temporalProfile is not part of this collections
"""
for k, tp in self.mTPLookupID.items():
if tp == temporalProfile:
return k
return None
def fromID(self, id):
if self.mTPLookupID.has_key(id):
return self.mTPLookupID[id]
else:
return None
def fromSpatialPoint(self, spatialPoint):
if self.mTPLookupSpatialPoint.has_key(spatialPoint):
return self.mTPLookupSpatialPoint[spatialPoint]
else:
return None
def removeTemporalProfiles(self, temporalProfiles):
"""
Removes temporal profiles from this collection
:param temporalProfile: TemporalProfile
"""
if isinstance(temporalProfiles, TemporalProfile):
temporalProfiles = [temporalProfiles]
assert isinstance(temporalProfiles, list)
temporalProfiles = [tp for tp in temporalProfiles if isinstance(tp, TemporalProfile) and tp in self.mTemporalProfiles]
if len(temporalProfiles) > 0:
def deleteFromDict(d, value):
assert isinstance(d, dict)
if value in d.values():
key = d.keys()[d.values().index(value)]
d.pop(key)
for temporalProfile in temporalProfiles:
assert isinstance(temporalProfile, TemporalProfile)
idx = self.tp2idx(temporalProfile)
row = idx.row()
self.beginRemoveRows(QModelIndex(), row, row)
self.mTemporalProfiles.remove(temporalProfile)
deleteFromDict(self.mTPLookupID, temporalProfile)
deleteFromDict(self.mTPLookupSpatialPoint, temporalProfile)
self.sigTemporalProfilesRemoved.emit(temporalProfiles)
self.clear()
if isinstance(timeSeries, TimeSeries):
self.TS = timeSeries
#for sensor in self.TS.Sensors:
# self.addSensor(sensor)
#self.TS.sigSensorAdded.connect(self.addSensor)
#self.TS.sigSensorRemoved.connect(self.removeSensor)
def setMaxProfiles(self, n):
"""
Sets the maximum number of temporal profiles to be stored in this container.
:param n: number of profiles, must be >= 1
"""
old = self.mMaxProfiles
if old != n:
self.mMaxProfiles = n
self.prune()
self.sigMaxProfilesChanged.emit(self.mMaxProfiles)
def prune(self):
"""
Reduces the number of temporal profile to the value n defined with .setMaxProfiles(n)
:return: [list-of-removed-TemporalProfiles]
"""
def getFieldDefn(self, name, values):
if isinstance(values, np.ndarray):
# add bands
if values.dtype in [np.int8, np.int16, np.int32, np.int64,
np.uint8, np.uint16, np.uint32, np.uint64]:
fType = QVariant.Int
fTypeName = 'integer'
elif values.dtype in [np.float16, np.float32, np.float64]:
fType = QVariant.Double
fTypeName = 'decimal'
else:
raise NotImplementedError()
return QgsField(name, fType, fTypeName)
def setFeatureAttribute(self, feature, name, value):
assert isinstance(feature, QgsFeature)
assert isinstance(name, str)
i = feature.fieldNameIndex(name)
assert i >= 0, 'Field "{}" does not exist'.format(name)
field = feature.fields()[i]
if field.isNumeric():
if field.type() == QVariant.Int:
value = int(value)
elif field.type() == QVariant.Double:
value = float(value)
else:
raise NotImplementedError()
feature.setAttribute(i, value)
def sort(self, col, order):
if self.rowCount() == 0:
return
self.layoutAboutToBeChanged.emit()
colName = self.mColumNames[col]
r = order != Qt.AscendingOrder
if colName == self.mcnName:
self.items.sort(key = lambda TP:TP.name(), reverse=r)
elif colName == self.mcnCoordinate:
self.items.sort(key=lambda TP: str(TP.mCoordinate), reverse=r)
elif colName == self.mcnID:
self.items.sort(key=lambda TP: TP.mID, reverse=r)
elif colName == self.mcnLoaded:
self.items.sort(key=lambda TP: TP.loadingStatus(), reverse=r)
self.layoutChanged.emit()
def addPixelLoaderResult(self, d):
assert isinstance(d, PixelLoaderTask)
if d.success():
TP = self.temporalProfileFromID(TPid)
tsd = self.TS.getTSD(d.sourcePath)
assert isinstance(tsd, TimeSeriesDatum)
if isinstance(TP, TemporalProfile):
profileData = d.resProfiles[i]
if not isinstance(profileData, tuple):
s = ""
validValues = not isinstance(vMean, str)
for iBand, bandIndex in enumerate(d.bandIndices):
key = 'b{}'.format(bandIndex + 1)
values[key] = vMean[iBand] if validValues else None
values[key] = vStd[iBand] if validValues else None
#indicesY, indicesX = d.imagePixelIndices()
#values['px_x'] = indicesX
#values['px_y'] = indicesY
TP.updateData(tsd, values)
def clear(self):
#todo: remove TS Profiles
#self.mTemporalProfiles.clear()
#self.sensorPxLayers.clear()
pass

benjamin.jakimow@geo.hu-berlin.de
committed
sigExpressionUpdated = pyqtSignal()
def __init__(self, temporalProfile):
super(TemporalProfilePlotStyle, self).__init__()
assert isinstance(temporalProfile, TemporalProfile)
self.mSensor = None
self.mTP = temporalProfile
self.mExpression = u'"b1"'
self.mPlotItems = []
if isinstance(temporalProfile, TemporalProfile):
self.setTemporalProfile(temporalProfile)
def createPlotItem(self, plotWidget):
pdi = TemporalProfilePlotDataItem(self)
self.mPlotItems.append(pdi)
return pdi
def setTemporalProfile(self, temporalPofile):
assert isinstance(temporalPofile, TemporalProfile)
b = temporalPofile != self.mTP
self.mTP = temporalPofile