forked from NationalSecurityAgency/qgis-latlontools-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopyLatLonTool.py
233 lines (219 loc) · 11.2 KB
/
copyLatLonTool.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
from qgis.PyQt.QtCore import Qt, pyqtSignal
from qgis.PyQt.QtGui import QColor
from qgis.PyQt.QtWidgets import QApplication
from qgis.core import Qgis, QgsCoordinateTransform, QgsPointXY, QgsProject, QgsSettings
from qgis.gui import QgsMapToolEmitPoint, QgsVertexMarker
from .settings import settings
from .util import epsg4326, formatDmsString
from .utm import latLon2UtmString
from . import mgrs
from . import olc
# import traceback
class CopyLatLonTool(QgsMapToolEmitPoint):
'''Class to interact with the map canvas to capture the coordinate
when the mouse button is pressed and to display the coordinate in
in the status bar.'''
capturesig = pyqtSignal(QgsPointXY)
def __init__(self, settings, iface):
QgsMapToolEmitPoint.__init__(self, iface.mapCanvas())
self.iface = iface
self.canvas = iface.mapCanvas()
self.settings = settings
self.capture4326 = False
self.marker = None
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.removeMarker()
self.removeVertexMarker()
def formatCoord(self, pt, delimiter):
'''Format the coordinate string according to the settings from
the settings dialog.'''
if self.settings.captureProjIsWgs84(): # ProjectionTypeWgs84
# Make sure the coordinate is transformed to EPSG:4326
canvasCRS = self.canvas.mapSettings().destinationCrs()
if canvasCRS == epsg4326:
pt4326 = pt
else:
transform = QgsCoordinateTransform(canvasCRS, epsg4326, QgsProject.instance())
pt4326 = transform.transform(pt.x(), pt.y())
lat = pt4326.y()
lon = pt4326.x()
if self.settings.wgs84NumberFormat == self.settings.Wgs84TypeDMS: # DMS
msg = formatDmsString(lat, lon, True, self.settings.dmsPrecision, self.settings.coordOrder, delimiter)
elif self.settings.wgs84NumberFormat == self.settings.Wgs84TypeDDMMSS: # DDMMSS
msg = formatDmsString(lat, lon, False, self.settings.dmsPrecision, self.settings.coordOrder, delimiter)
elif self.settings.wgs84NumberFormat == self.settings.Wgs84TypeWKT: # WKT
msg = 'POINT({:.{prec}f} {:.{prec}f})'.format(pt4326.x(), pt4326.y(), prec=self.settings.decimalDigits)
elif self.settings.wgs84NumberFormat == self.settings.Wgs84TypeGeoJSON: # GeoJSON
msg = '{{"type": "Point","coordinates": [{:.{prec}f},{:.{prec}f}]}}'.format(pt4326.x(), pt4326.y(), prec=self.settings.decimalDigits)
else: # decimal degrees
if self.settings.coordOrder == self.settings.OrderYX:
msg = '{:.{prec}f}{}{:.{prec}f}'.format(pt4326.y(), delimiter, pt4326.x(), prec=self.settings.decimalDigits)
else:
msg = '{:.{prec}f}{}{:.{prec}f}'.format(pt4326.x(), delimiter, pt4326.y(), prec=self.settings.decimalDigits)
elif self.settings.captureProjIsProjectCRS():
# Projection in the project CRS
if self.settings.otherNumberFormat == 0: # Numerical
if self.settings.coordOrder == self.settings.OrderYX:
msg = '{:.{prec}f}{}{:.{prec}f}'.format(pt.y(), delimiter, pt.x(), prec=self.settings.decimalDigits)
else:
msg = '{:.{prec}f}{}{:.{prec}f}'.format(pt.x(), delimiter, pt.y(), prec=self.settings.decimalDigits)
else:
msg = 'POINT({:.{prec}f} {:.{prec}f})'.format(pt.x(), pt.y(), prec=self.settings.decimalDigits)
elif self.settings.captureProjIsCustomCRS():
# Projection is a custom CRS
canvasCRS = self.canvas.mapSettings().destinationCrs()
customCRS = self.settings.captureCustomCRS()
transform = QgsCoordinateTransform(canvasCRS, customCRS, QgsProject.instance())
pt = transform.transform(pt.x(), pt.y())
if self.settings.otherNumberFormat == 0: # Numerical
if self.settings.coordOrder == self.settings.OrderYX:
msg = '{:.{prec}f}{}{:.{prec}f}'.format(pt.y(), delimiter, pt.x(), prec=self.settings.decimalDigits)
else:
msg = '{:.{prec}f}{}{:.{prec}f}'.format(pt.x(), delimiter, pt.y(), prec=self.settings.decimalDigits)
else:
msg = 'POINT({:.{prec}f} {:.{prec}f})'.format(pt.x(), pt.y(), prec=self.settings.decimalDigits)
elif self.settings.captureProjIsMGRS():
# Make sure the coordinate is transformed to EPSG:4326
canvasCRS = self.canvas.mapSettings().destinationCrs()
if canvasCRS == epsg4326:
pt4326 = pt
else:
transform = QgsCoordinateTransform(canvasCRS, epsg4326, QgsProject.instance())
pt4326 = transform.transform(pt.x(), pt.y())
try:
msg = mgrs.toMgrs(pt4326.y(), pt4326.x())
except Exception:
msg = None
elif self.settings.captureProjIsPlusCodes():
# Make sure the coordinate is transformed to EPSG:4326
canvasCRS = self.canvas.mapSettings().destinationCrs()
if canvasCRS == epsg4326:
pt4326 = pt
else:
transform = QgsCoordinateTransform(canvasCRS, epsg4326, QgsProject.instance())
pt4326 = transform.transform(pt.x(), pt.y())
try:
msg = olc.encode(pt4326.y(), pt4326.x(), self.settings.plusCodesLength)
except Exception:
msg = None
elif self.settings.captureProjIsUTM():
# Make sure the coordinate is transformed to EPSG:4326
canvasCRS = self.canvas.mapSettings().destinationCrs()
if canvasCRS == epsg4326:
pt4326 = pt
else:
transform = QgsCoordinateTransform(canvasCRS, epsg4326, QgsProject.instance())
pt4326 = transform.transform(pt.x(), pt.y())
msg = latLon2UtmString(pt4326.y(), pt4326.x(), self.settings.dmsPrecision)
if msg == '':
msg = None
msg = '{}{}{}'.format(self.settings.capturePrefix, msg, self.settings.captureSuffix)
return msg
def canvasMoveEvent(self, event):
'''Capture the coordinate as the user moves the mouse over
the canvas. Show it in the status bar.'''
pt = self.snappoint(event.originalPixelPoint()) # input is QPoint
try:
msg = self.formatCoord(pt, ', ')
formatString = self.coordFormatString()
if msg is None:
self.iface.statusBarIface().showMessage("")
else:
self.iface.statusBarIface().showMessage("{} - {}".format(msg, formatString), 4000)
except Exception:
self.iface.statusBarIface().showMessage("")
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 coordFormatString(self):
if self.settings.captureProjIsWgs84():
if self.settings.wgs84NumberFormat == self.settings.Wgs84TypeDecimal:
if self.settings.coordOrder == self.settings.OrderYX:
s = 'Lat Lon'
else:
s = 'Lon Lat'
elif self.settings.wgs84NumberFormat == self.settings.Wgs84TypeWKT:
s = 'WKT'
elif self.settings.wgs84NumberFormat == self.settings.Wgs84TypeGeoJSON:
s = 'GeoJSON'
else:
s = 'DMS'
elif self.settings.captureProjIsProjectCRS():
crsID = self.canvas.mapSettings().destinationCrs().authid()
if self.settings.otherNumberFormat == 0: # Numerical
if self.settings.coordOrder == self.settings.OrderYX:
s = '{} - Y,X'.format(crsID)
else:
s = '{} - X,Y'.format(crsID)
else: # WKT
s = 'WKT'
elif self.settings.captureProjIsMGRS():
s = 'MGRS'
elif self.settings.captureProjIsUTM():
s = 'Standard UTM'
elif self.settings.captureProjIsPlusCodes():
s = 'Plus Codes'
elif self.settings.captureProjIsCustomCRS():
if self.settings.otherNumberFormat == 0: # Numerical
if self.settings.coordOrder == self.settings.OrderYX:
s = '{} - Y,X'.format(self.settings.captureCustomCRSID())
else:
s = '{} - X,Y'.format(self.settings.captureCustomCRSID())
else: # WKT
s = 'WKT'
else: # Should never happen
s = ''
return s
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()
if settings.captureShowLocation:
if self.marker is None:
self.marker = QgsVertexMarker(self.canvas)
self.marker.setIconSize(18)
self.marker.setPenWidth(2)
self.marker.setIconType(QgsVertexMarker.ICON_CROSS)
self.marker.setCenter(pt)
else:
self.removeMarker()
try:
if self.capture4326:
canvasCRS = self.canvas.mapSettings().destinationCrs()
transform = QgsCoordinateTransform(canvasCRS, epsg4326, QgsProject.instance())
pt4326 = transform.transform(pt.x(), pt.y())
self.capturesig.emit(pt4326)
return
msg = self.formatCoord(pt, self.settings.delimiter)
formatString = self.coordFormatString()
if msg is not None:
clipboard = QApplication.clipboard()
clipboard.setText(msg)
self.iface.messageBar().pushMessage("", "{} coordinate {} copied to the clipboard".format(formatString, msg), level=Qgis.Info, duration=4)
except Exception as e:
self.iface.messageBar().pushMessage("", "Invalid coordinate: {}".format(e), level=Qgis.Warning, duration=4)
def removeMarker(self):
if self.marker is not None:
self.canvas.scene().removeItem(self.marker)
self.marker = None
def removeVertexMarker(self):
if self.vertex is not None:
self.canvas.scene().removeItem(self.vertex)
self.vertex = None