Newer
Older
# -*- coding: utf-8 -*-
"""
/***************************************************************************
HUB TimeSeriesViewer
-------------------
begin : 2015-08-20
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
import os, logging
logger = logging.getLogger(__name__)
from qgis.core import *
from qgis.gui import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from timeseriesviewer import SETTINGS
from timeseriesviewer.utils import *

benjamin.jakimow@geo.hu-berlin.de
committed
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
class MapTools(object):
"""
Static class to support handling of nQgsMapTools.
"""
def __init__(self):
raise Exception('This class is not for any instantiation')
ZoomIn = 'ZOOM_IN'
ZoomOut = 'ZOOM_OUT'
ZoomFull = 'ZOOM_FULL'
Pan = 'PAN'
ZoomPixelScale = 'ZOOM_PIXEL_SCALE'
CursorLocation = 'CURSOR_LOCATION'
SpectralProfile = 'SPECTRAL_PROFILE'
TemporalProfile = 'TEMPORAL_PROFILE'
MoveToCenter = 'MOVE_CENTER'
@staticmethod
def copy(mapTool):
assert isinstance(mapTool, QgsMapTool)
s = ""
@staticmethod
def create(mapToolKey, canvas, *args, **kwds):
assert mapToolKey in MapTools.mapToolKeys()
assert isinstance(canvas, QgsMapCanvas)
if mapToolKey == MapTools.ZoomIn:
return QgsMapToolZoom(canvas, False)
if mapToolKey == MapTools.ZoomOut:
return QgsMapToolZoom(canvas, True)
if mapToolKey == MapTools.Pan:
return QgsMapToolPan(canvas)
if mapToolKey == MapTools.ZoomPixelScale:
return PixelScaleExtentMapTool(canvas)
if mapToolKey == MapTools.ZoomFull:
return FullExtentMapTool(canvas)
if mapToolKey == MapTools.CursorLocation:
return CursorLocationMapTool(canvas, *args, **kwds)
if mapToolKey == MapTools.MoveToCenter:
tool = CursorLocationMapTool(canvas, *args, **kwds)
tool.sigLocationRequest.connect(canvas.setCenter)
return tool
if mapToolKey == MapTools.SpectralProfile:
return SpectralProfileMapTool(canvas, *args, **kwds)
if mapToolKey == MapTools.TemporalProfile:
return TemporalProfileMapTool(canvas, *args, **kwds)
raise Exception('Unknown mapToolKey {}'.format(mapToolKey))
@staticmethod
def mapToolKeys():
return [MapTools.__dict__[k] for k in MapTools.__dict__.keys() if not k.startswith('_')]
class CursorLocationMapTool(QgsMapToolEmitPoint):
sigLocationRequest = pyqtSignal([SpatialPoint],[SpatialPoint, QgsMapCanvas])
def __init__(self, canvas, showCrosshair=True, purpose=None):
self.mShowCrosshair = showCrosshair
self.mCanvas = canvas
self.mPurpose = purpose
QgsMapToolEmitPoint.__init__(self, self.mCanvas)
self.mMarker = QgsVertexMarker(self.mCanvas)
self.mRubberband = QgsRubberBand(self.mCanvas, QGis.Polygon)
color = QColor('red')
self.mRubberband.setLineStyle(Qt.SolidLine)
self.mRubberband.setColor(color)
self.mRubberband.setWidth(2)
self.mMarker.setColor(color)
self.mMarker.setPenWidth(3)
self.mMarker.setIconSize(5)
self.mMarker.setIconType(QgsVertexMarker.ICON_CROSS) # or ICON_CROSS, ICON_X
def canvasPressEvent(self, e):
geoPoint = self.toMapCoordinates(e.pos())
self.mMarker.setCenter(geoPoint)
def setStyle(self, color=None, brushStyle=None, fillColor=None, lineStyle=None):
if color:
self.mRubberband.setColor(color)
if brushStyle:
self.mRubberband.setBrushStyle(brushStyle)
if fillColor:
self.mRubberband.setFillColor(fillColor)
if lineStyle:
self.mRubberband.setLineStyle(lineStyle)
def canvasReleaseEvent(self, e):
pixelPoint = e.pixelPoint()
crs = self.mCanvas.mapSettings().destinationCrs()
self.mMarker.hide()
geoPoint = self.toMapCoordinates(pixelPoint)
if self.mShowCrosshair:
#show a temporary crosshair
ext = SpatialExtent.fromMapCanvas(self.mCanvas)
cen = geoPoint
geom = QgsGeometry()
geom.addPart([QgsPoint(ext.upperLeftPt().x(),cen.y()), QgsPoint(ext.lowerRightPt().x(), cen.y())],
QGis.Line)
geom.addPart([QgsPoint(cen.x(), ext.upperLeftPt().y()), QgsPoint(cen.x(), ext.lowerRightPt().y())],
QGis.Line)
self.mRubberband.addGeometry(geom, None)
self.mRubberband.show()
#remove crosshair after 0.25 sec
QTimer.singleShot(250, self.hideRubberband)
pt = SpatialPoint(crs, geoPoint)
self.sigLocationRequest[SpatialPoint].emit(pt)
self.sigLocationRequest[SpatialPoint, QgsMapCanvas].emit(pt, self.canvas())
def hideRubberband(self):
self.mRubberband.reset()
class SpectralProfileMapTool(CursorLocationMapTool):
def __init__(self, *args, **kwds):
super(SpectralProfileMapTool, self).__init__(*args, **kwds)
class TemporalProfileMapTool(CursorLocationMapTool):
def __init__(self, *args, **kwds):
super(TemporalProfileMapTool, self).__init__(*args, **kwds)
class FullExtentMapTool(QgsMapTool):
def __init__(self, canvas):
super(FullExtentMapTool, self).__init__(canvas)
self.canvas = canvas
def canvasReleaseEvent(self, mouseEvent):
self.canvas.zoomToFullExtent()
def flags(self):
return QgsMapTool.Transient
class PixelScaleExtentMapTool(QgsMapTool):
def __init__(self, canvas):
super(PixelScaleExtentMapTool, self).__init__(canvas)
self.canvas = canvas
def flags(self):
return QgsMapTool.Transient
def canvasReleaseEvent(self, mouseEvent):
layers = self.canvas.layers()
unitsPxX = []
unitsPxY = []
for lyr in self.canvas.layers():
if isinstance(lyr, QgsRasterLayer):
Loading
Loading full blame...