-
Notifications
You must be signed in to change notification settings - Fork 0
/
captureCoordinate.py
75 lines (66 loc) · 3.15 KB
/
captureCoordinate.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
"""
/***************************************************************************
* *
* 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 qgis.PyQt.QtCore import Qt, pyqtSignal
from qgis.PyQt.QtGui import QColor
from qgis.PyQt.QtCore import pyqtSlot
from qgis.core import QgsCoordinateTransform, QgsPointXY, QgsProject, QgsSettings
from qgis.gui import QgsMapToolEmitPoint, QgsVertexMarker
from .util import epsg4326
class CaptureCoordinate(QgsMapToolEmitPoint):
'''Class to interact with the map canvas to capture the coordinate
when the mouse button is pressed.'''
capturePoint = pyqtSignal(QgsPointXY)
captureStopped = pyqtSignal()
def __init__(self, canvas):
QgsMapToolEmitPoint.__init__(self, canvas)
self.canvas = canvas
self.vertex = None
def activate(self):
'''When activated set the cursor to a crosshair.'''
self.canvas.setCursor(Qt.CrossCursor)
self.snapcolor = QgsSettings().value( "/qgis/digitizing/snap_color" , QColor( Qt.magenta ) )
def deactivate(self):
self.removeVertexMarker()
self.captureStopped.emit()
def snappoint(self, qpoint):
match = self.canvas.snappingUtils().snapToMap(qpoint)
if match.isValid():
if self.vertex is None:
self.vertex = QgsVertexMarker(self.canvas)
self.vertex.setIconSize(12)
self.vertex.setPenWidth(2)
self.vertex.setColor(self.snapcolor)
self.vertex.setIconType(QgsVertexMarker.ICON_BOX)
self.vertex.setCenter(match.point())
return (match.point()) # Returns QgsPointXY
else:
self.removeVertexMarker()
return self.toMapCoordinates(qpoint) # QPoint input, returns QgsPointXY
def canvasMoveEvent(self, event):
'''Capture the coordinate as the user moves the mouse over
the canvas.'''
self.snappoint(event.originalPixelPoint()) # input is QPoint
def canvasReleaseEvent(self, event):
'''Capture the coordinate when the mouse button has been released,
format it, and copy it to the clipboard. pt is QgsPointXY'''
pt = self.snappoint(event.originalPixelPoint())
self.removeVertexMarker()
try:
canvasCRS = self.canvas.mapSettings().destinationCrs()
transform = QgsCoordinateTransform(canvasCRS, epsg4326, QgsProject.instance())
pt4326 = transform.transform(pt.x(), pt.y())
self.capturePoint.emit(pt4326)
except Exception as e:
pass
def removeVertexMarker(self):
if self.vertex is not None:
self.canvas.scene().removeItem(self.vertex)
self.vertex = None