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
from __future__ import absolute_import
from qgis.core import *
from qgis.gui import *
import qgis
from PyQt4.QtCore import *
from PyQt4.QtGui import *

Benjamin Jakimow
committed
from PyQt4.QtXml import *
from PyQt4.QtXmlPatterns import *

Benjamin Jakimow
committed
import xml.etree
from qgis.core import *
from timeseriesviewer import SETTINGS

benjamin.jakimow@geo.hu-berlin.de
committed
from timeseriesviewer.main import SpatialExtent, SpatialPoint

benjamin.jakimow@geo.hu-berlin.de
committed
class CursorLocationMapTool(QgsMapToolEmitPoint):

benjamin.jakimow@geo.hu-berlin.de
committed
sigLocationRequest = pyqtSignal(SpatialPoint)

benjamin.jakimow@geo.hu-berlin.de
committed
def __init__(self, canvas, showCrosshair=True):
self.mShowCrosshair = showCrosshair
self.canvas = canvas
QgsMapToolEmitPoint.__init__(self, self.canvas)
self.marker = QgsVertexMarker(self.canvas)
self.rubberband = QgsRubberBand(self.canvas, QGis.Polygon)

benjamin.jakimow@geo.hu-berlin.de
committed
color = QColor('red')

benjamin.jakimow@geo.hu-berlin.de
committed
self.mButtons = [Qt.LeftButton]

benjamin.jakimow@geo.hu-berlin.de
committed
self.rubberband.setLineStyle(Qt.SolidLine)
self.rubberband.setColor(color)
self.rubberband.setWidth(2)

benjamin.jakimow@geo.hu-berlin.de
committed
self.marker.setPenWidth(3)
self.marker.setIconSize(5)
self.marker.setIconType(QgsVertexMarker.ICON_CROSS) # or ICON_CROSS, ICON_X

benjamin.jakimow@geo.hu-berlin.de
committed
def setMouseButtons(self, listOfButtons):
assert isinstance(listOfButtons)
self.mButtons = listOfButtons

benjamin.jakimow@geo.hu-berlin.de
committed
assert isinstance(e, QgsMapMouseEvent)
if e.button() in self.mButtons:
geoPoint = self.toMapCoordinates(e.pos())
self.marker.setCenter(geoPoint)

benjamin.jakimow@geo.hu-berlin.de
committed
def setStyle(self, color=None, brushStyle=None, fillColor=None, lineStyle=None):
if color:
self.rubberband.setColor(color)
if brushStyle:
self.rubberband.setBrushStyle(brushStyle)
if fillColor:
self.rubberband.setFillColor(fillColor)
if lineStyle:
self.rubberband.setLineStyle(lineStyle)

benjamin.jakimow@geo.hu-berlin.de
committed
if e.button() in self.mButtons:
pixelPoint = e.pixelPoint()
crs = self.canvas.mapSettings().destinationCrs()
self.marker.hide()
geoPoint = self.toMapCoordinates(pixelPoint)
if self.mShowCrosshair:
#show a temporary crosshair
ext = SpatialExtent.fromMapCanvas(self.canvas)
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.rubberband.addGeometry(geom, None)
self.rubberband.show()
#remove crosshair after 0.25 sec
QTimer.singleShot(250, self.hideRubberband)
self.sigLocationRequest.emit(SpatialPoint(crs, geoPoint))

benjamin.jakimow@geo.hu-berlin.de
committed
def hideRubberband(self):
self.rubberband.reset()
class PointLayersMapTool(CursorLocationMapTool):
def __init__(self, canvas):
super(PointLayersMapTool, self).__init__(self, canvas)
self.layerType = QgsMapToolIdentify.AllLayers
self.identifyMode = QgsMapToolIdentify.LayerSelection
QgsMapToolIdentify.__init__(self, canvas)
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
class SpatialExtentMapTool(QgsMapToolEmitPoint):
from timeseriesviewer.main import SpatialExtent
sigSpatialExtentSelected = pyqtSignal(SpatialExtent)
def __init__(self, canvas):
self.canvas = canvas
QgsMapToolEmitPoint.__init__(self, self.canvas)
self.rubberBand = QgsRubberBand(self.canvas, QGis.Polygon)
self.setStyle(Qt.red, 1)
self.reset()
def setStyle(self, color, width):
self.rubberBand.setColor(color)
self.rubberBand.setWidth(width)
def reset(self):
self.startPoint = self.endPoint = None
self.isEmittingPoint = False
self.rubberBand.reset(QGis.Polygon)
def canvasPressEvent(self, e):
self.startPoint = self.toMapCoordinates(e.pos())
self.endPoint = self.startPoint
self.isEmittingPoint = True
self.showRect(self.startPoint, self.endPoint)
def canvasReleaseEvent(self, e):
self.isEmittingPoint = False
crs = self.canvas.mapSettings().destinationCrs()
rect = self.rectangle()
self.reset()
if crs is not None and rect is not None:
extent = SpatialExtent(crs, rect)
self.rectangleDrawed.emit(extent)
def canvasMoveEvent(self, e):
if not self.isEmittingPoint:
return
self.endPoint = self.toMapCoordinates(e.pos())
self.showRect(self.startPoint, self.endPoint)
def showRect(self, startPoint, endPoint):
self.rubberBand.reset(QGis.Polygon)
if startPoint.x() == endPoint.x() or startPoint.y() == endPoint.y():
return
point1 = QgsPoint(startPoint.x(), startPoint.y())
point2 = QgsPoint(startPoint.x(), endPoint.y())
point3 = QgsPoint(endPoint.x(), endPoint.y())
point4 = QgsPoint(endPoint.x(), startPoint.y())
self.rubberBand.addPoint(point1, False)
self.rubberBand.addPoint(point2, False)
self.rubberBand.addPoint(point3, False)
self.rubberBand.addPoint(point4, True) # true to update canvas
self.rubberBand.show()
def rectangle(self):
if self.startPoint is None or self.endPoint is None:
return None
elif self.startPoint.x() == self.endPoint.x() or self.startPoint.y() == self.endPoint.y():
return None
#def deactivate(self):
# super(RectangleMapTool, self).deactivate()
#self.deactivated.emit()
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
class RectangleMapTool(QgsMapToolEmitPoint):
rectangleDrawed = pyqtSignal(QgsRectangle, object)
def __init__(self, canvas):
self.canvas = canvas
QgsMapToolEmitPoint.__init__(self, self.canvas)
self.rubberBand = QgsRubberBand(self.canvas, QGis.Polygon)
self.rubberBand.setColor(Qt.red)
self.rubberBand.setWidth(1)
self.reset()
def reset(self):
self.startPoint = self.endPoint = None
self.isEmittingPoint = False
self.rubberBand.reset(QGis.Polygon)
def canvasPressEvent(self, e):
self.startPoint = self.toMapCoordinates(e.pos())
self.endPoint = self.startPoint
self.isEmittingPoint = True
self.showRect(self.startPoint, self.endPoint)
def canvasReleaseEvent(self, e):
self.isEmittingPoint = False
wkt = self.canvas.mapSettings().destinationCrs().toWkt()
self.rectangleDrawed.emit(r, wkt)
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
def canvasMoveEvent(self, e):
if not self.isEmittingPoint:
return
self.endPoint = self.toMapCoordinates(e.pos())
self.showRect(self.startPoint, self.endPoint)
def showRect(self, startPoint, endPoint):
self.rubberBand.reset(QGis.Polygon)
if startPoint.x() == endPoint.x() or startPoint.y() == endPoint.y():
return
point1 = QgsPoint(startPoint.x(), startPoint.y())
point2 = QgsPoint(startPoint.x(), endPoint.y())
point3 = QgsPoint(endPoint.x(), endPoint.y())
point4 = QgsPoint(endPoint.x(), startPoint.y())
self.rubberBand.addPoint(point1, False)
self.rubberBand.addPoint(point2, False)
self.rubberBand.addPoint(point3, False)
self.rubberBand.addPoint(point4, True) # true to update canvas
self.rubberBand.show()
def rectangle(self):
if self.startPoint is None or self.endPoint is None:
return None
elif self.startPoint.x() == self.endPoint.x() or self.startPoint.y() == self.endPoint.y():
return None
return QgsRectangle(self.startPoint, self.endPoint)
#def deactivate(self):
# super(RectangleMapTool, self).deactivate()
#self.deactivated.emit()

Benjamin Jakimow
committed

benjamin.jakimow@geo.hu-berlin.de
committed
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
if __name__ == '__main__':
import site, sys
#add site-packages to sys.path as done by enmapboxplugin.py
from timeseriesviewer import sandbox
qgsApp = sandbox.initQgisEnvironment()
import example.Images
lyr1 = QgsRasterLayer(example.Images.Img_2012_05_09_LE72270652012130EDC00_BOA)
lyr2 = QgsRasterLayer(example.Images.Img_2012_05_09_LE72270652012130EDC00_BOA)
lyr3 = QgsRasterLayer(example.Images.Img_2012_05_09_LE72270652012130EDC00_BOA)
QgsMapLayerRegistry.instance().addMapLayers([lyr1, lyr2, lyr3])
w = QWidget()
l = QHBoxLayout()
canvas1 = QgsMapCanvas()
canvas1.setWindowTitle('Canvas1')
canvas1.setLayerSet([QgsMapCanvasLayer(lyr1)])
canvas1.setExtent(lyr1.extent())
mt = CursorLocationMapTool(canvas1)
canvas1.setMapTool(mt)
canvas2 = QgsMapCanvas()
canvas2.setWindowTitle('Canvas2')
canvas2.setLayerSet([QgsMapCanvasLayer(lyr2)])
canvas2.setExtent(lyr2.extent())
canvas3 = QgsMapCanvas()
canvas3.setWindowTitle('Canvas3')
#canvas3.setLayerSet([QgsMapCanvasLayer(lyr3)])
#canvas3.setExtent(lyr3.extent())
l.addWidget(canvas1)
l.addWidget(canvas2)
l.addWidget(canvas3)
w.setLayout(l)
w.show()
qgsApp.exec_()
qgsApp.exitQgis()