-
Notifications
You must be signed in to change notification settings - Fork 0
/
latlngcalc_dialog.py
128 lines (111 loc) · 5.05 KB
/
latlngcalc_dialog.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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
LatlngcalcDialog
A QGIS plugin
Allows conversion for latitudes and longitudes between decimal degrees and DMS
Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
-------------------
begin : 2022-01-18
git sha : $Format:%H$
copyright : (C) 2022 by Athena Solutions Limited
email : contact@josephkariuki.com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it is 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. *
* *
***************************************************************************/
"""
import os
from qgis.PyQt import uic
from qgis.PyQt import QtWidgets
# This loads your .ui file so that PyQt can populate your plugin with the elements from Qt Designer
FORM_CLASS, _ = uic.loadUiType(os.path.join(
os.path.dirname(__file__), 'latlngcalc_dialog_base.ui'))
class LatlngcalcDialog(QtWidgets.QDialog, FORM_CLASS):
def __init__(self, parent=None):
"""Constructor."""
super(LatlngcalcDialog, self).__init__(parent)
# Set up the user interface from Designer through FORM_CLASS.
# After self.setupUi() you can access any designer object by doing
# self.<objectname>, and you can use autoconnect slots - see
# http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html
# #widgets-and-dialogs-with-auto-connect
self.setupUi(self)
# Connect signals to slots
self.spbLatD.valueChanged.connect(self.on_lat_dms_to_dd)
self.spbLatM.valueChanged.connect(self.on_lat_dms_to_dd)
self.spbLatS.valueChanged.connect(self.on_lat_dms_to_dd)
self.comboNS.currentTextChanged.connect(self.on_lat_dms_to_dd)
self.spbLongD.valueChanged.connect(self.on_long_dms_to_dd)
self.spbLongM.valueChanged.connect(self.on_long_dms_to_dd)
self.spbLongS.valueChanged.connect(self.on_long_dms_to_dd)
self.comboEW.currentTextChanged.connect(self.on_long_dms_to_dd)
self.spbLatDecDeg.editingFinished.connect(self.on_lat_dd_to_dms)
self.spbLongDecDeg.editingFinished.connect(self.on_long_dd_to_dms)
def on_lat_dms_to_dd(self):
"""
Slot raised to calculate Decimal Minutes and Seconds to Decimal
Degrees for Latitude.
"""
i_deg = self.spbLatD.value()
i_min = self.spbLatM.value()
d_sec = self.spbLatS.value()
dec_deg = float(i_deg) + i_min/60 + d_sec/3600
ns_hemisphere = self.comboNS.currentText()
self.spbLatDecDeg.setValue(dec_deg)
if ns_hemisphere == 'South':
dec_deg *= -1
self.spbLatDecDeg.setValue(dec_deg)
def on_long_dms_to_dd(self):
"""
Slot raised to calculate Decimal Minutes and Seconds to Decimal
Degrees for Longitude.
"""
init_deg = self.spbLongD.value()
init_min = self.spbLongM.value()
d_sec = self.spbLongS.value()
dec_deg = float(init_deg) + init_min/60 + d_sec/3600
ew_hemisphere = self.comboEW.currentText()
self.spbLongDecDeg.setValue(dec_deg)
if ew_hemisphere == 'West':
dec_deg *= -1
self.spbLongDecDeg.setValue(dec_deg)
def on_lat_dd_to_dms(self):
"""
Slot raised to convert Decimal Degrees to Decimal Minutes and
Seconds (DMS) for Latitude.
"""
dec_deg = self.spbLatDecDeg.value()
init_deg = int(dec_deg)
dec_min = int(dec_deg - init_deg) * 60
init_min = int(dec_min)
d_sec = (dec_min - init_min) * 60
self.spbLatD.setValue(abs(init_deg))
self.spbLatM.setValue(abs(init_min))
self.spbLatS.setValue(abs(d_sec))
if dec_deg < 0:
self.comboNS.setCurrentText("South")
else:
self.comboNS.setCurrentText("North")
def on_long_dd_to_dms(self):
"""
Slot raised to convert Decimal Degrees to Decimal Minutes and
Seconds (DMS) for Longitude.
"""
dec_deg = self.spbLongDecDeg.value()
init_deg = int(dec_deg)
dec_min = int(dec_deg - init_deg) * 60
init_min = int(dec_min)
d_sec = (dec_min - init_min) * 60
self.spbLongD.setValue(abs(init_deg))
self.spbLongM.setValue(abs(init_min))
self.spbLongS.setValue(abs(d_sec))
if dec_deg < 0:
self.comboEW.setCurrentText("West")
else:
self.comboEW.setCurrentText("East")