forked from jrenken/qgis-PosiView
/
position_marker.py
285 lines (255 loc) · 10.7 KB
/
position_marker.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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
PositionMarker
A QGIS plugin
PosiView tracks multiple mobile object and vehicles and displays its position on the canvas
-------------------
begin : 2015-06-01
git sha : $Format:%H$
copyright : (C) 2015 by Jens Renken/Marum/University of Bremen
email : renken@marum.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. *
* *
***************************************************************************/
"""
from PyQt4.QtCore import QPointF, QRectF, QLineF, QPoint
from PyQt4.QtGui import QPainter, QBrush, QColor, QPen, QPolygonF
from qgis.gui import QgsMapCanvasItem, QgsVertexMarker
from qgis.core import QgsDistanceArea
from _collections import deque
class PositionMarker(QgsMapCanvasItem):
'''
MapCanvasItem for showing the MobileItem on the MapCanvas
Can have different appearences, fixed ones like corss, x or box
or a userdefined shape.
Can display also a label on the canvas
'''
def __init__(self, canvas, params={}):
'''
Constructor
:param iface: An interface instance that will be passed to this class
which provides the hook by which you can manipulate the QGIS
application at run time.
:type iface: QgsInterface
:param params: A dictionary defining all the properties of the position marker
:type params: dictionary
'''
self.canvas = canvas
self.type = params.get('type', 'BOX').upper()
self.size = int(params.get('size', 16))
self.showLabel = bool(params.get('showLabel', True))
s = (self.size - 1) / 2
self.length = float(params.get('length', 98.0))
self.width = float(params.get('width', 17.0))
self.offsetX = float(params.get('offsetX', 0.0))
self.offsetY = float(params.get('offsetY', 0.0))
self.shape = params.get('shape', ((0.0, -0.5), (0.5, -0.3), (0.5, 0.5), (-0.5, 0.50), (-0.5, -0.3)))
self.paintShape = QPolygonF([QPointF(-s, -s), QPointF(s, -s), QPointF(s, s), QPointF(-s, s)])
self.color = self.getColor(params.get('color', 'black'))
self.fillColor = self.getColor(params.get('fillColor', 'lime'))
self.penWidth = int(params.get('penWidth', 1))
spw = s + self.penWidth + 1
self.bounding = QRectF(-spw, -spw, spw * 2, spw * 2)
if self.type in ('CROSS', 'X'):
self.penWidth = 5
self.trackLen = int(params.get('trackLength', 100))
self.trackColor = self.getColor(params.get('trackColor', self.fillColor))
self.track = deque()
self.position = None
self.heading = 0
super(PositionMarker, self).__init__(canvas)
self.setZValue(int(params.get('zValue', 100)))
self.distArea = QgsDistanceArea()
self.distArea.setEllipsoid(u'WGS84')
self.distArea.setEllipsoidalMode(True)
if self.showLabel:
self.label = MarkerLabel(self.canvas, params)
self.label.setZValue(self.zValue() + 0.1)
self.updateSize()
def properties(self):
return {'type': self.type,
'size': self.size,
'length': self.length,
'width': self.width,
'offsetX': self.offsetX,
'offsetY': self.offsetY,
'shape': self.shape,
'color': self.color.rgba(),
'fillColor': self.fillColor.rgba(),
'penWidth': self.penWidth,
'trackLength': self.trackLen,
'trackColor' : self.trackColor.rgba(),
'zValue': self.zValue(),
'showLabel': self.showLabel}
def setMapPosition(self, pos):
if self.position != pos:
self.updateTrack()
self.position = pos
self.setPos(self.toCanvasCoordinates(self.position))
if self.showLabel:
self.label.setMapPosition(pos)
self.update()
def newHeading(self, heading):
if self.heading != heading:
self.heading = heading
self.setRotation(self.canvas.rotation() + self.heading)
self.update()
def resetPosition(self):
self.position = None
if self.showLabel:
self.label.resetPoition()
def updatePosition(self):
if self.position:
self.prepareGeometryChange()
self.updateSize()
self.setPos(self.toCanvasCoordinates(self.position))
self.setRotation(self.canvas.rotation() + self.heading)
def updateMapMagnification(self):
self.updatePosition()
if self.showLabel:
self.label.updatePosition()
for tp in self.track:
tp[0].updatePosition()
def updateSize(self):
if self.type != 'SHAPE':
return
s = self.canvas.mapSettings()
self.distArea.setSourceCrs(s.destinationCrs())
try:
p1 = self.toMapCoordinates(QPoint(0, 0))
p2 = self.toMapCoordinates(QPoint(0, 100))
l = self.distArea.measureLine(p1, p2)
f = 100 / l
except:
f = s.outputDpi() / 0.0254 / s.scale()
paintLength = max(self.length * f, 50)
paintWidth = paintLength * self.width / self.length
offsY = self.offsetX / self.length * paintLength
offsX = self.offsetY / self.width * paintWidth
self.paintShape.clear()
for v in self.shape:
self.paintShape << QPointF(v[0] * paintWidth - offsX, v[1] * paintLength + offsY)
self.size = max(paintLength, paintWidth)
self.bounding = self.paintShape.boundingRect()
def newTrackPoint(self, pos):
tp = QgsVertexMarker(self.canvas)
tp.setCenter(pos)
tp.setIconType(QgsVertexMarker.ICON_CROSS)
tp.setColor(self.trackColor)
tp.setZValue(self.zValue() - 0.1)
tp.setIconSize(3)
tp.setPenWidth(3)
return tp
def updateTrack(self):
if self.position and self.trackLen:
if len(self.track) >= self.trackLen:
tpr = self.track.popleft()
self.canvas.scene().removeItem(tpr[0])
del(tpr)
tp = self.newTrackPoint(self.position)
self.track.append((tp, self.position))
def setVisible(self, visible):
for tp in self.track:
tp[0].setVisible(visible)
QgsMapCanvasItem.setVisible(self, visible)
if self.showLabel:
self.label.setVisible(visible)
def deleteTrack(self):
for tp in self.track:
self.canvas.scene().removeItem(tp[0])
self.track.clear()
def setTrack(self, track):
self.track.clear()
for tp in track:
tpn = self.newTrackPoint(tp)
self.track.append((tpn, tp))
def paint(self, painter, xxx, xxx2):
if not self.position:
return
s = (self.size - 1) / 2
pen = QPen(self.color)
pen.setWidth(self.penWidth)
painter.setPen(pen)
if self.type == 'CROSS':
painter.drawLine(QLineF(-s, 0, s, 0))
painter.drawLine(QLineF(0, -s, 0, s))
elif self.type == 'X':
painter.drawLine(QLineF(-s, -s, s, s))
painter.drawLine(QLineF(-s, s, s, -s))
elif self.type == 'BOX':
brush = QBrush(self.fillColor)
painter.setBrush(brush)
painter.drawConvexPolygon(self.paintShape)
elif self.type == 'SHAPE':
painter.setRenderHint(QPainter.Antialiasing, True)
brush = QBrush(self.fillColor)
painter.setBrush(brush)
painter.drawConvexPolygon(self.paintShape)
def boundingRect(self):
return self.bounding
def getColor(self, value):
try:
return QColor.fromRgba(int(value))
except ValueError:
return QColor(value)
def removeFromCanvas(self):
self.deleteTrack()
if self.showLabel:
self.canvas.scene().removeItem(self.label)
self.canvas.scene().removeItem(self)
class MarkerLabel(QgsMapCanvasItem):
'''
Visual representation of the markers/mobiles name
'''
def __init__(self, canvas, params={}):
'''
Constructor
:param iface: An interface instance that will be passed to this class
which provides the hook by which you can manipulate the QGIS
application at run time.
:type iface: QgsInterface
:param params: A dictionary defining all the properties of the position marker
:type params: dictionary
'''
self.canvas = canvas
self.label = params.get('Name', 'Item')
self.LABEL_DISTANCE = 50
self.labelDistance = params.get('labelDistance', self.LABEL_DISTANCE)
self.labelRect = QRectF(self.canvas.fontMetrics().boundingRect(
self.label)).translated(QPointF(self.labelDistance, -self.labelDistance))
self.labelRect.setBottomLeft(QPointF(0, 0))
self.color = self.getColor(params.get('color', 'black'))
self.position = None
super(MarkerLabel, self).__init__(canvas)
def boundingRect(self):
return self.labelRect
def paint(self, painter, xxx, xxx2):
if not self.position:
return
pen = QPen(self.color)
pen.setWidth(1)
painter.setPen(pen)
painter.drawLine(QPointF(0, 0), QPointF(self.labelDistance, -self.labelDistance / 2))
painter.drawText(QPointF(self.labelDistance + 2, -self.labelDistance / 2 + 2), self.label)
def setMapPosition(self, pos):
if self.position != pos:
self.position = pos
self.setPos(self.toCanvasCoordinates(self.position))
self.update()
def resetPoition(self):
self.position = None
def updatePosition(self):
if self.position:
self.setPos(self.toCanvasCoordinates(self.position))
def getColor(self, value):
try:
return QColor.fromRgba(int(value))
except ValueError:
return QColor(value)