forked from NationalSecurityAgency/qgis-latlontools-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoordinateConverter.py
421 lines (374 loc) · 16.4 KB
/
coordinateConverter.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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import os
import re
from qgis.PyQt.QtCore import QSize
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtWidgets import QDialog, QMenu, QApplication
from qgis.PyQt.QtCore import pyqtSlot
from qgis.PyQt.uic import loadUiType
from qgis.core import QgsCoordinateTransform, QgsPoint, QgsPointXY, QgsProject
from .util import epsg4326, parseDMSString, formatDmsString
# import traceback
from .settings import settings
from . import mgrs
from . import olc
from .utm import latLon2UtmString, isUtm, utmString2Crs
FORM_CLASS, _ = loadUiType(os.path.join(
os.path.dirname(__file__), 'ui/coordinateConverter.ui'))
class CoordinateConverterWidget(QDialog, FORM_CLASS):
inputProjection = 0
origPt = None
origCrs = epsg4326
def __init__(self, lltools, settingsDialog, iface, parent):
super(CoordinateConverterWidget, self).__init__(parent)
self.setupUi(self)
self.iface = iface
self.canvas = iface.mapCanvas()
self.lltools = lltools
self.settings = settingsDialog
self.clipboard = QApplication.clipboard()
# Set up a connection with the coordinate capture tool
self.lltools.mapTool.capturesig.connect(self.capturedPoint)
self.xymenu = QMenu()
icon = QIcon(os.path.dirname(__file__) + '/images/yx.png')
a = self.xymenu.addAction(icon, "Y, X (Lat, Lon) Order")
a.setData(0)
icon = QIcon(os.path.dirname(__file__) + '/images/xy.png')
a = self.xymenu.addAction(icon, "X, Y (Lon, Lat) Order")
a.setData(1)
self.xyButton.setIconSize(QSize(16, 16))
self.xyButton.setIcon(icon)
self.xyButton.setMenu(self.xymenu)
self.xyButton.triggered.connect(self.xyTriggered)
self.inputXYOrder = settings.converterCoordOrder
self.clearFormButton.setIcon(QIcon(':/images/themes/default/mIconClearText.svg'))
self.clearFormButton.clicked.connect(self.clearForm)
self.coordCaptureButton.setIcon(QIcon(os.path.dirname(__file__) + "/images/coordinate_capture.png"))
self.coordCaptureButton.clicked.connect(self.startCapture)
self.optionsButton.setIcon(QIcon(':/images/themes/default/mActionOptions.svg'))
self.optionsButton.clicked.connect(self.showSettings)
self.closeButton.clicked.connect(self.closeEvent)
icon = QIcon(os.path.dirname(__file__) + "/images/check.png")
self.wgs84CommitButton.setIcon(icon)
self.projCommitButton.setIcon(icon)
self.customCommitButton.setIcon(icon)
self.dmsCommitButton.setIcon(icon)
self.ddmmssCommitButton.setIcon(icon)
self.utmCommitButton.setIcon(icon)
self.mgrsCommitButton.setIcon(icon)
self.plusCommitButton.setIcon(icon)
self.wgs84CommitButton.clicked.connect(self.commitWgs84)
self.wgs84LineEdit.returnPressed.connect(self.commitWgs84)
self.projCommitButton.clicked.connect(self.commitProject)
self.projLineEdit.returnPressed.connect(self.commitProject)
self.customCommitButton.clicked.connect(self.commitCustom)
self.customLineEdit.returnPressed.connect(self.commitCustom)
self.dmsCommitButton.clicked.connect(self.commitDms)
self.dmsLineEdit.returnPressed.connect(self.commitDms)
self.ddmmssCommitButton.clicked.connect(self.commitDdmmss)
self.ddmmssLineEdit.returnPressed.connect(self.commitDdmmss)
self.utmCommitButton.clicked.connect(self.commitUtm)
self.utmLineEdit.returnPressed.connect(self.commitUtm)
self.mgrsCommitButton.clicked.connect(self.commitMgrs)
self.mgrsLineEdit.returnPressed.connect(self.commitMgrs)
self.plusCommitButton.clicked.connect(self.commitPlus)
self.plusLineEdit.returnPressed.connect(self.commitPlus)
icon = QIcon(':/images/themes/default/mActionEditCopy.svg')
self.wgs84CopyButton.setIcon(icon)
self.projCopyButton.setIcon(icon)
self.customCopyButton.setIcon(icon)
self.dmsCopyButton.setIcon(icon)
self.ddmmssCopyButton.setIcon(icon)
self.utmCopyButton.setIcon(icon)
self.mgrsCopyButton.setIcon(icon)
self.plusCopyButton.setIcon(icon)
self.wgs84CopyButton.clicked.connect(self.copyWgs84)
self.projCopyButton.clicked.connect(self.copyProject)
self.customCopyButton.clicked.connect(self.copyCustom)
self.dmsCopyButton.clicked.connect(self.copyDms)
self.ddmmssCopyButton.clicked.connect(self.copyDdmmss)
self.utmCopyButton.clicked.connect(self.copyUtm)
self.mgrsCopyButton.clicked.connect(self.copyMgrs)
self.plusCopyButton.clicked.connect(self.copyPlus)
self.customProjectionSelectionWidget.setCrs(epsg4326)
self.customProjectionSelectionWidget.crsChanged.connect(self.customCrsChanged)
def showEvent(self, e):
self.inputXYOrder = settings.converterCoordOrder
self.xyButton.setDefaultAction(self.xymenu.actions()[settings.converterCoordOrder])
self.updateLabel()
def closeEvent(self, e):
self.stopCapture()
self.hide()
def xyTriggered(self, action):
self.xyButton.setDefaultAction(action)
self.inputXYOrder = action.data()
if self.origPt is not None:
self.updateCoordinates(-1, self.origPt, self.origCrs)
self.updateLabel()
def showInvalid(self, id):
self.origPt = None
if id != 0:
self.wgs84LineEdit.setText('Invalid')
if id != 1:
self.projLineEdit.setText('Invalid')
if id != 2:
self.customLineEdit.setText('Invalid')
if id != 3:
self.dmsLineEdit.setText('Invalid')
if id != 4:
self.ddmmssLineEdit.setText('Invalid')
if id != 5:
self.utmLineEdit.setText('Invalid')
if id != 6:
self.mgrsLineEdit.setText('Invalid')
if id != 7:
self.plusLineEdit.setText('Invalid')
def clearForm(self):
self.origPt = None
self.wgs84LineEdit.setText('')
self.projLineEdit.setText('')
self.customLineEdit.setText('')
self.dmsLineEdit.setText('')
self.ddmmssLineEdit.setText('')
self.utmLineEdit.setText('')
self.mgrsLineEdit.setText('')
self.plusLineEdit.setText('')
def updateCoordinates(self, id, pt, crs):
self.origPt = pt
self.origCrs = crs
projCRS = self.canvas.mapSettings().destinationCrs()
customCRS = self.customProjectionSelectionWidget.crs()
if crs == epsg4326:
pt4326 = pt
else:
trans = QgsCoordinateTransform(crs, epsg4326, QgsProject.instance())
pt4326 = trans.transform(pt.x(), pt.y())
if id != 0: # WGS 84
if self.inputXYOrder == 0: # Y, X
s = '{:.{prec}f}{}{:.{prec}f}'.format(pt4326.y(), settings.converterDelimiter, pt4326.x(), prec=settings.converter4326DDPrec)
else:
s = '{:.{prec}f}{}{:.{prec}f}'.format(pt4326.x(), settings.converterDelimiter, pt4326.y(), prec=settings.converter4326DDPrec)
self.wgs84LineEdit.setText(s)
if id != 1: # Project CRS
if crs == projCRS:
newpt = pt
else:
trans = QgsCoordinateTransform(crs, projCRS, QgsProject.instance())
newpt = trans.transform(pt.x(), pt.y())
if projCRS == epsg4326:
precision = settings.converter4326DDPrec
else:
precision = settings.converterDDPrec
if self.inputXYOrder == 0: # Y, X
s = '{:.{prec}f}{}{:.{prec}f}'.format(newpt.y(), settings.converterDelimiter, newpt.x(), prec=precision)
else:
s = '{:.{prec}f}{}{:.{prec}f}'.format(newpt.x(), settings.converterDelimiter, newpt.y(), prec=precision)
self.projLineEdit.setText(s)
if id != 2: # Custom CRS
if crs == customCRS:
newpt = pt
else:
trans = QgsCoordinateTransform(crs, customCRS, QgsProject.instance())
newpt = trans.transform(pt.x(), pt.y())
if customCRS == epsg4326:
precision = settings.converter4326DDPrec
else:
precision = settings.converterDDPrec
if self.inputXYOrder == 0: # Y, X
s = '{:.{prec}f}{}{:.{prec}f}'.format(newpt.y(), settings.converterDelimiter, newpt.x(), prec=precision)
else:
s = '{:.{prec}f}{}{:.{prec}f}'.format(newpt.x(), settings.converterDelimiter, newpt.y(), prec=precision)
self.customLineEdit.setText(s)
if id != 3: # D M' S"
s = formatDmsString(pt4326.y(), pt4326.x(), True, settings.converterDmsPrec, self.inputXYOrder, settings.converterDelimiter)
self.dmsLineEdit.setText(s)
if id != 4: # DDMMSS
s = formatDmsString(pt4326.y(), pt4326.x(), False, settings.converterDmsPrec, self.inputXYOrder, settings.converterDdmmssDelimiter)
self.ddmmssLineEdit.setText(s)
if id != 5: # UTM
s = latLon2UtmString(pt4326.y(), pt4326.x(), settings.converterUtmPrec)
self.utmLineEdit.setText(s)
if id != 6: # MGRS
try:
s = mgrs.toMgrs(pt4326.y(), pt4326.x())
except Exception:
s = 'Invalid'
self.mgrsLineEdit.setText(s)
if id != 7: # Plus Codes
try:
s = olc.encode(pt4326.y(), pt4326.x(), settings.converterPlusCodeLength)
except Exception:
s = 'Invalid'
self.plusLineEdit.setText(s)
def commitWgs84(self):
text = self.wgs84LineEdit.text().strip()
try:
lat, lon = parseDMSString(text, self.inputXYOrder)
pt = QgsPoint(lon, lat)
self.updateCoordinates(0, pt, epsg4326)
except Exception:
# traceback.print_exc()
self.showInvalid(0)
def commitProject(self):
projCRS = self.canvas.mapSettings().destinationCrs()
text = self.projLineEdit.text().strip()
try:
if projCRS == epsg4326:
lat, lon = parseDMSString(text, self.inputXYOrder)
else:
coords = re.split(r'[\s,;:]+', text, 1)
if len(coords) < 2:
self.showInvalid(1)
return
if self.inputXYOrder == 0: # Lat, Lon
lat = float(coords[0])
lon = float(coords[1])
else: # Lon, Lat
lon = float(coords[0])
lat = float(coords[1])
except Exception:
self.showInvalid(1)
return
pt = QgsPoint(lon, lat)
self.updateCoordinates(1, pt, projCRS)
def commitCustom(self):
customCRS = self.customProjectionSelectionWidget.crs()
text = self.customLineEdit.text().strip()
try:
if customCRS == epsg4326:
lat, lon = parseDMSString(text, self.inputXYOrder)
else:
coords = re.split(r'[\s,;:]+', text, 1)
if len(coords) < 2:
self.showInvalid(2)
return
if self.inputXYOrder == 0: # Lat, Lon
lat = float(coords[0])
lon = float(coords[1])
else: # Lon, Lat
lon = float(coords[0])
lat = float(coords[1])
except Exception:
self.showInvalid(2)
return
pt = QgsPoint(lon, lat)
self.updateCoordinates(2, pt, customCRS)
def commitDms(self):
text = self.dmsLineEdit.text().strip()
try:
lat, lon = parseDMSString(text, self.inputXYOrder)
pt = QgsPoint(lon, lat)
self.updateCoordinates(3, pt, epsg4326)
except Exception:
self.showInvalid(3)
def commitDdmmss(self):
text = self.ddmmssLineEdit.text().strip()
try:
lat, lon = parseDMSString(text, self.inputXYOrder)
pt = QgsPoint(lon, lat)
self.updateCoordinates(4, pt, epsg4326)
except Exception:
self.showInvalid(4)
def commitUtm(self):
text = self.utmLineEdit.text().strip()
if isUtm(text):
pt = utmString2Crs(text, epsg4326)
self.updateCoordinates(5, QgsPoint(pt), epsg4326)
else:
self.showInvalid(5)
def commitMgrs(self):
text = self.mgrsLineEdit.text().strip()
text = re.sub(r'\s+', '', text) # Remove all white space
try:
lat, lon = mgrs.toWgs(text)
pt = QgsPoint(lon, lat)
self.updateCoordinates(5, pt, epsg4326)
except Exception:
self.showInvalid(6)
def commitPlus(self):
text = self.plusLineEdit.text().strip()
text = re.sub(r'\s+', '', text) # Remove all white space
try:
coord = olc.decode(text)
lat = coord.latitudeCenter
lon = coord.longitudeCenter
pt = QgsPoint(lon, lat)
self.updateCoordinates(7, pt, epsg4326)
except Exception:
self.showInvalid(7)
def updateLabel(self):
if self.inputXYOrder == 0: # Y, X
xy = '(Y, X)'
latlon = '(latitude, longitude)'
else:
xy = '(X, Y)'
latlon = '(longitude, latitude)'
crs = self.canvas.mapSettings().destinationCrs()
if crs == epsg4326:
label = 'Project CRS - {} {}'.format('EPSG:4326', latlon)
else:
label = 'Project CRS - {} {}'.format(crs.authid(), xy)
self.projectLabel.setText(label)
label = 'WGS 84 {}'.format(latlon)
self.wgs84Label.setText(label)
crs = self.customProjectionSelectionWidget.crs()
if crs == epsg4326:
label = 'Custom CRS - {} {}'.format('EPSG:4326', latlon)
else:
label = 'Custom CRS - {} {}'.format(crs.authid(), xy)
self.customLabel.setText(label)
label = 'D° M\' S" {}'.format(latlon)
self.dmsLabel.setText(label)
label = 'DDMMSS {}'.format(latlon)
self.ddmmssLabel.setText(label)
def copyWgs84(self):
s = self.wgs84LineEdit.text()
self.clipboard.setText(s)
self.iface.statusBarIface().showMessage("'{}' copied to the clipboard".format(s), 3000)
def copyProject(self):
s = self.projLineEdit.text()
self.clipboard.setText(s)
self.iface.statusBarIface().showMessage("'{}' copied to the clipboard".format(s), 3000)
def copyCustom(self):
s = self.customLineEdit.text()
self.clipboard.setText(s)
self.iface.statusBarIface().showMessage("'{}' copied to the clipboard".format(s), 3000)
def copyDms(self):
s = self.projLineEdit.text()
self.clipboard.setText(s)
self.iface.statusBarIface().showMessage("'{}' copied to the clipboard".format(s), 3000)
self.clipboard.setText(self.dmsLineEdit.text())
def copyDdmmss(self):
s = self.ddmmssLineEdit.text()
self.clipboard.setText(s)
self.iface.statusBarIface().showMessage("'{}' copied to the clipboard".format(s), 3000)
def copyUtm(self):
s = self.utmLineEdit.text()
self.clipboard.setText(s)
self.iface.statusBarIface().showMessage("'{}' copied to the clipboard".format(s), 3000)
def copyMgrs(self):
s = self.mgrsLineEdit.text()
self.clipboard.setText(s)
self.iface.statusBarIface().showMessage("'{}' copied to the clipboard".format(s), 3000)
def copyPlus(self):
s = self.plusLineEdit.text()
self.clipboard.setText(s)
self.iface.statusBarIface().showMessage("'{}' copied to the clipboard".format(s), 3000)
def customCrsChanged(self):
if self.origPt is not None:
self.updateCoordinates(-1, self.origPt, self.origCrs)
self.updateLabel()
@pyqtSlot(QgsPointXY)
def capturedPoint(self, pt):
if self.isVisible() and self.coordCaptureButton.isChecked():
self.updateCoordinates(-1, pt, epsg4326)
def startCapture(self):
if self.coordCaptureButton.isChecked():
self.lltools.mapTool.capture4326 = True
self.lltools.startCapture()
else:
self.lltools.mapTool.capture4326 = False
def stopCapture(self):
self.lltools.mapTool.capture4326 = False
self.coordCaptureButton.setChecked(False)
def showSettings(self):
self.settings.showTab(5)