Newer
Older
# -*- coding: utf-8 -*-
"""
/***************************************************************************
HUB TimeSeriesViewer
A QGIS based time series viewer
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. *
* *
***************************************************************************/
"""
import os, sys, re, fnmatch, collections, copy, traceback, six
import logging
logger = logging.getLogger(__name__)

Benjamin Jakimow
committed
from timeseriesviewer import jp, mkdir, DIR_SITE_PACKAGES, file_search
from timeseriesviewer.timeseries import *
#I don't know why, but this is required to run this in QGIS
#todo: still required?
path = os.path.abspath(jp(sys.exec_prefix, '../../bin/pythonw.exe'))
if os.path.exists(path):
multiprocessing.set_executable(path)
sys.argv = [ None ]
#ensure that required non-standard modules are available
import pyqtgraph as pg
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
class TsvMimeDataUtils(QObject):
def __init__(self, mimeData):
assert isinstance(mimeData, QMimeData)
super(TsvMimeDataUtils, self).__init__()
self.mimeData = mimeData
self.xmlDoc = QDomDocument()
if self.mimeData.hasText():
self.xmlDoc.setContent(self.mimeData.text())
self.xmlRoot = self.xmlDoc.documentElement()
pass
def hasRasterStyle(self):
if self.xmlRoot.tagName() == 'qgis':
elem = self.xmlRoot.elementsByTagName('rasterrenderer')
return elem.count() != 0
return False
def rasterStyle(self, qgisDataType):
elem = self.xmlRoot.elementsByTagName('rasterrenderer').item(0).toElement()
type = str(elem.attribute('type'))
from qgis.core import QGis, QgsContrastEnhancement
def bandSettings(colorName):
band = int(elem.attribute(colorName + 'Band'))
ceNode = elem.elementsByTagName(colorName + 'ContrastEnhancement').item(0)
vMin = float(ceNode.firstChildElement('minValue').firstChild().nodeValue())
vMax = float(ceNode.firstChildElement('maxValue').firstChild().nodeValue())
ceName = ceNode.firstChildElement('algorithm').firstChild().nodeValue()
ceAlg = QgsContrastEnhancement.contrastEnhancementAlgorithmFromString(ceName)
ce = QgsContrastEnhancement(qgisDataType)
ce.setContrastEnhancementAlgorithm(ceAlg)
ce.setMinimumValue(vMin)
ce.setMaximumValue(vMax)
return band, ce
style = None
if type == 'multibandcolor':
A = int(elem.attribute('alphaBand'))
O = int(elem.attribute('opacity'))
R, ceR = bandSettings('red')
G, ceG = bandSettings('green')
B, ceB = bandSettings('blue')
style = QgsMultiBandColorRenderer(None, R, G, B)
style.setRedContrastEnhancement(ceR)
style.setGreenContrastEnhancement(ceG)
style.setBlueContrastEnhancement(ceB)
elif type == 'singlebandgrey':
pass
return style
class QgisTsvBridge(QObject):
"""
Class to control interactions between TSV and a running QGIS instance
"""
_instance = None
@staticmethod
def instance():
return QgisTsvBridge._instance
def __init__(self, iface, TSV):
super(QgisTsvBridge, self).__init__()
assert QgisTsvBridge._instance is None
assert isinstance(TSV, TimeSeriesViewer)
assert isinstance(iface, QgisInterface)
self.iface = iface
self.TSV = TSV
self.ui = self.TSV.ui
self.SpatTempVis = self
self.syncBlocked = False
from timeseriesviewer.ui.widgets import TimeSeriesViewerUI
assert isinstance(self.ui, TimeSeriesViewerUI)
self.cbQgsVectorLayer = self.ui.dockRendering.cbQgsVectorLayer
self.gbQgsVectorLayer = self.ui.dockRendering.gbQgsVectorLayer
self.cbQgsVectorLayer.setEnabled(True)
self.gbQgsVectorLayer.setEnabled(True)
self.qgsMapCanvas = self.iface.mapCanvas()
assert isinstance(self.qgsMapCanvas, QgsMapCanvas)
self.qgsMapCanvas.extentsChanged.connect(self.syncTsvWithQgs)
self.qgsMapCanvas.destinationCrsChanged.connect(self.syncTsvWithQgs)
assert isinstance(self.cbQgsVectorLayer, QgsMapLayerComboBox)
assert isinstance(self.gbQgsVectorLayer, QgsCollapsibleGroupBox)
self.TSV.spatialTemporalVis.sigSpatialExtentChanged.connect(self.syncQgsWithTsv)
self.gbQgsVectorLayer.clicked.connect(self.onQgsVectorLayerChanged)
self.cbQgsVectorLayer.layerChanged.connect(self.onQgsVectorLayerChanged)
self.onQgsVectorLayerChanged(None)
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
def syncTsvWithQgs(self, *args):
if self.syncBlocked:
return
syncState = self.ui.dockNavigation.qgsSyncState()
if any(syncState.values()):
self.syncBlocked = True
self.syncBlocked = True
QTimer.singleShot(500, lambda: self.unblock())
tsvExt = self.TSV.spatialTemporalVis.extent
qgsExt = SpatialExtent.fromMapCanvas(self.qgsMapCanvas)
newExtent = self.newExtent(tsvExt, syncState, qgsExt)
self.TSV.spatialTemporalVis.setSpatialExtent(newExtent)
self.syncBlocked = False
pass
def syncQgsWithTsv(self, spatialExtent):
if self.syncBlocked:
return
syncState = self.ui.dockNavigation.qgsSyncState()
if any(syncState.values()):
self.syncBlocked = True
QTimer.singleShot(500, lambda: self.unblock())
tsvExt = self.TSV.spatialTemporalVis.extent
qgsExt = SpatialExtent.fromMapCanvas(self.qgsMapCanvas)
newExtent = self.newExtent(qgsExt, syncState, tsvExt)
self.qgsMapCanvas.setDestinationCrs(newExtent.crs())
self.qgsMapCanvas.setExtent(newExtent)
self.syncBlocked = False
QTimer.singleShot(1000, lambda : self.unblock())
def unblock(self):
self.syncBlocked = False
def newExtent(self, oldExtent, syncState, newExtent):
Loading
Loading full blame...