coordconvertergc 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026, Gracious Chinguwo
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: coordconvertergc
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A Python package for converting geographic coordinates between degrees, minutes, seconds and decimal degrees.
|
|
5
|
+
License: MIT
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Author: Gracious Chinguwo
|
|
8
|
+
Requires-Python: >=3.12,<4.0
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
|
|
17
|
+
# coordconvertergc
|
|
18
|
+
|
|
19
|
+
A Python package for converting geographic coordinates between degrees, minutes, seconds and decimal degrees.
|
|
20
|
+
|
|
21
|
+
Created by Gracious Chinguwo
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
Install the package from PyPi using:
|
|
25
|
+
pip install coordconvertergc
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
28
|
+
1. Convert DMS to decimal degrees
|
|
29
|
+
Use to_decimal() to convert latitude and longitude from DMS to decimal degrees. Southern and western coordinates are returned as negative values.
|
|
30
|
+
|
|
31
|
+
from coordconvertergc import to_decimal
|
|
32
|
+
latitude = (15, 47, 0, "S")
|
|
33
|
+
longitude = (35, 0, 0, "E")
|
|
34
|
+
|
|
35
|
+
result = to_decimal(latitude, longitude)
|
|
36
|
+
print(result)
|
|
37
|
+
|
|
38
|
+
Output:
|
|
39
|
+
(-15.783333333333333, 35.0)
|
|
40
|
+
|
|
41
|
+
2. Convert decimal degrees to DMS
|
|
42
|
+
Use to_degree() to convert decimal-degree latitude and longitude to DMS coordinates.
|
|
43
|
+
|
|
44
|
+
from coordconvertergc import to_degree
|
|
45
|
+
latitude = -15.783333333333333
|
|
46
|
+
longitude = 35.0
|
|
47
|
+
|
|
48
|
+
result = to_degree(latitude, longitude)
|
|
49
|
+
print(result)
|
|
50
|
+
|
|
51
|
+
output:
|
|
52
|
+
((15, 47, 0.0, 'S'), (35, 0, 0.0, 'E'))
|
|
53
|
+
|
|
54
|
+
## Input Validation
|
|
55
|
+
The functions validate coordinate ranges, DMS values, and compass directions. Invalid inputs raise a ValueError with an explanatory message.
|
|
56
|
+
|
|
57
|
+
Example 1: Invalid latitude
|
|
58
|
+
|
|
59
|
+
to_degree(95, 35)
|
|
60
|
+
|
|
61
|
+
This raises a ValueError because latitude must be between -90 and 90 degrees.
|
|
62
|
+
|
|
63
|
+
Example 2: Invalid longitude
|
|
64
|
+
|
|
65
|
+
to_degree(15, 200)
|
|
66
|
+
|
|
67
|
+
This raises a ValueError because longitude must be between -180 and 180 degrees.
|
|
68
|
+
|
|
69
|
+
These checks help ensure that only geographically valid coordinates are converted.
|
|
70
|
+
|
|
71
|
+
## Contributing
|
|
72
|
+
|
|
73
|
+
Contributions are welcome. Please read the contributing guidelines and code of conduct before contributing.
|
|
74
|
+
|
|
75
|
+
## License
|
|
76
|
+
|
|
77
|
+
This project is licensed under the MIT License. See the LICENSE file for details.
|
|
78
|
+
|
|
79
|
+
## Project links
|
|
80
|
+
- GitHub repository: https://github.com/Chinguwo8/coordconvertergc
|
|
81
|
+
|
|
82
|
+
- PyPI package: https://pypi.org/project/coordconvertergc/
|
|
83
|
+
|
|
84
|
+
## Credits
|
|
85
|
+
This package was created using Cookiecutter and the py-pkgs-cookiecutter template
|
|
86
|
+
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# coordconvertergc
|
|
2
|
+
|
|
3
|
+
A Python package for converting geographic coordinates between degrees, minutes, seconds and decimal degrees.
|
|
4
|
+
|
|
5
|
+
Created by Gracious Chinguwo
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
Install the package from PyPi using:
|
|
9
|
+
pip install coordconvertergc
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
1. Convert DMS to decimal degrees
|
|
13
|
+
Use to_decimal() to convert latitude and longitude from DMS to decimal degrees. Southern and western coordinates are returned as negative values.
|
|
14
|
+
|
|
15
|
+
from coordconvertergc import to_decimal
|
|
16
|
+
latitude = (15, 47, 0, "S")
|
|
17
|
+
longitude = (35, 0, 0, "E")
|
|
18
|
+
|
|
19
|
+
result = to_decimal(latitude, longitude)
|
|
20
|
+
print(result)
|
|
21
|
+
|
|
22
|
+
Output:
|
|
23
|
+
(-15.783333333333333, 35.0)
|
|
24
|
+
|
|
25
|
+
2. Convert decimal degrees to DMS
|
|
26
|
+
Use to_degree() to convert decimal-degree latitude and longitude to DMS coordinates.
|
|
27
|
+
|
|
28
|
+
from coordconvertergc import to_degree
|
|
29
|
+
latitude = -15.783333333333333
|
|
30
|
+
longitude = 35.0
|
|
31
|
+
|
|
32
|
+
result = to_degree(latitude, longitude)
|
|
33
|
+
print(result)
|
|
34
|
+
|
|
35
|
+
output:
|
|
36
|
+
((15, 47, 0.0, 'S'), (35, 0, 0.0, 'E'))
|
|
37
|
+
|
|
38
|
+
## Input Validation
|
|
39
|
+
The functions validate coordinate ranges, DMS values, and compass directions. Invalid inputs raise a ValueError with an explanatory message.
|
|
40
|
+
|
|
41
|
+
Example 1: Invalid latitude
|
|
42
|
+
|
|
43
|
+
to_degree(95, 35)
|
|
44
|
+
|
|
45
|
+
This raises a ValueError because latitude must be between -90 and 90 degrees.
|
|
46
|
+
|
|
47
|
+
Example 2: Invalid longitude
|
|
48
|
+
|
|
49
|
+
to_degree(15, 200)
|
|
50
|
+
|
|
51
|
+
This raises a ValueError because longitude must be between -180 and 180 degrees.
|
|
52
|
+
|
|
53
|
+
These checks help ensure that only geographically valid coordinates are converted.
|
|
54
|
+
|
|
55
|
+
## Contributing
|
|
56
|
+
|
|
57
|
+
Contributions are welcome. Please read the contributing guidelines and code of conduct before contributing.
|
|
58
|
+
|
|
59
|
+
## License
|
|
60
|
+
|
|
61
|
+
This project is licensed under the MIT License. See the LICENSE file for details.
|
|
62
|
+
|
|
63
|
+
## Project links
|
|
64
|
+
- GitHub repository: https://github.com/Chinguwo8/coordconvertergc
|
|
65
|
+
|
|
66
|
+
- PyPI package: https://pypi.org/project/coordconvertergc/
|
|
67
|
+
|
|
68
|
+
## Credits
|
|
69
|
+
This package was created using Cookiecutter and the py-pkgs-cookiecutter template
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
[tool.poetry]
|
|
2
|
+
name = "coordconvertergc"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "A Python package for converting geographic coordinates between degrees, minutes, seconds and decimal degrees."
|
|
5
|
+
authors = ["Gracious Chinguwo"]
|
|
6
|
+
license = "MIT"
|
|
7
|
+
readme = "README.md"
|
|
8
|
+
|
|
9
|
+
[tool.poetry.dependencies]
|
|
10
|
+
python = "^3.12"
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
[build-system]
|
|
14
|
+
requires = ["poetry-core>=1.0.0"]
|
|
15
|
+
build-backend = "poetry.core.masonry.api"
|
|
16
|
+
|
|
17
|
+
[dependency-groups]
|
|
18
|
+
dev = [
|
|
19
|
+
"pytest (>=9.1.1,<10.0.0)"
|
|
20
|
+
]
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
def to_decimal(latitude, longitude):
|
|
2
|
+
"""
|
|
3
|
+
Convert latitude and longitude from degrees, minutes, and seconds
|
|
4
|
+
(DMS) to decimal degrees.
|
|
5
|
+
|
|
6
|
+
Parameters
|
|
7
|
+
----------
|
|
8
|
+
latitude : tuple
|
|
9
|
+
Latitude as (degrees, minutes, seconds, direction).
|
|
10
|
+
Direction must be N or S.
|
|
11
|
+
longitude : tuple
|
|
12
|
+
Longitude as (degrees, minutes, seconds, direction).
|
|
13
|
+
Direction must be E or W.
|
|
14
|
+
|
|
15
|
+
Returns
|
|
16
|
+
-------
|
|
17
|
+
tuple
|
|
18
|
+
A pair containing (latitude, longitude) in decimal degrees.
|
|
19
|
+
South and west coordinates are negative.
|
|
20
|
+
|
|
21
|
+
Raises
|
|
22
|
+
------
|
|
23
|
+
ValueError
|
|
24
|
+
If the coordinate format, values, or direction is invalid.
|
|
25
|
+
|
|
26
|
+
Examples
|
|
27
|
+
--------
|
|
28
|
+
>>> to_decimal((15, 47, 0, "S"), (35, 0, 0, "E"))
|
|
29
|
+
(-15.783333333333333, 35.0)
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
def validate_coordinate(coordinate, coordinate_type):
|
|
33
|
+
if not isinstance(coordinate, (tuple, list)) or len(coordinate) != 4:
|
|
34
|
+
raise ValueError(
|
|
35
|
+
"Coordinate must contain (degrees, minutes, seconds, direction)."
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
degrees, minutes, seconds, direction = coordinate
|
|
39
|
+
|
|
40
|
+
if any(
|
|
41
|
+
isinstance(value, bool) or not isinstance(value, (int, float))
|
|
42
|
+
for value in (degrees, minutes, seconds)
|
|
43
|
+
):
|
|
44
|
+
raise ValueError("Degrees, minutes, and seconds must be numeric.")
|
|
45
|
+
|
|
46
|
+
if not all(
|
|
47
|
+
value == value and abs(value) != float("inf")
|
|
48
|
+
for value in (degrees, minutes, seconds)
|
|
49
|
+
):
|
|
50
|
+
raise ValueError("Coordinate values must be finite numbers.")
|
|
51
|
+
|
|
52
|
+
if degrees < 0 or minutes < 0 or seconds < 0:
|
|
53
|
+
raise ValueError("Degrees, minutes, and seconds cannot be negative.")
|
|
54
|
+
|
|
55
|
+
if minutes >= 60 or seconds >= 60:
|
|
56
|
+
raise ValueError("Minutes and seconds must be less than 60.")
|
|
57
|
+
|
|
58
|
+
direction = direction.upper() if isinstance(direction, str) else ""
|
|
59
|
+
|
|
60
|
+
if coordinate_type == "latitude":
|
|
61
|
+
if direction not in ("N", "S"):
|
|
62
|
+
raise ValueError("Latitude direction must be N or S.")
|
|
63
|
+
maximum = 90
|
|
64
|
+
else:
|
|
65
|
+
if direction not in ("E", "W"):
|
|
66
|
+
raise ValueError("Longitude direction must be E or W.")
|
|
67
|
+
maximum = 180
|
|
68
|
+
|
|
69
|
+
if degrees > maximum or (
|
|
70
|
+
degrees == maximum and (minutes != 0 or seconds != 0)
|
|
71
|
+
):
|
|
72
|
+
raise ValueError(
|
|
73
|
+
f"{coordinate_type.title()} cannot exceed {maximum} degrees."
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
return degrees, minutes, seconds, direction
|
|
77
|
+
|
|
78
|
+
lat_deg, lat_min, lat_sec, lat_dir = validate_coordinate(
|
|
79
|
+
latitude, "latitude"
|
|
80
|
+
)
|
|
81
|
+
lon_deg, lon_min, lon_sec, lon_dir = validate_coordinate(
|
|
82
|
+
longitude, "longitude"
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
lat_decimal = lat_deg + (lat_min / 60) + (lat_sec / 3600)
|
|
86
|
+
lon_decimal = lon_deg + (lon_min / 60) + (lon_sec / 3600)
|
|
87
|
+
|
|
88
|
+
if lat_dir == "S":
|
|
89
|
+
lat_decimal = -lat_decimal
|
|
90
|
+
|
|
91
|
+
if lon_dir == "W":
|
|
92
|
+
lon_decimal = -lon_decimal
|
|
93
|
+
|
|
94
|
+
return lat_decimal, lon_decimal
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def to_degree(latitude, longitude):
|
|
98
|
+
"""
|
|
99
|
+
Convert decimal-degree latitude and longitude to DMS coordinates.
|
|
100
|
+
|
|
101
|
+
Parameters
|
|
102
|
+
----------
|
|
103
|
+
latitude : int or float
|
|
104
|
+
Latitude in decimal degrees, from -90 to 90.
|
|
105
|
+
longitude : int or float
|
|
106
|
+
Longitude in decimal degrees, from -180 to 180.
|
|
107
|
+
|
|
108
|
+
Returns
|
|
109
|
+
-------
|
|
110
|
+
tuple
|
|
111
|
+
Two tuples in the format (degrees, minutes, seconds, direction).
|
|
112
|
+
|
|
113
|
+
Raises
|
|
114
|
+
------
|
|
115
|
+
ValueError
|
|
116
|
+
If coordinates are not finite numbers or are outside valid ranges.
|
|
117
|
+
|
|
118
|
+
Examples
|
|
119
|
+
--------
|
|
120
|
+
>>> to_degree(-15.783333333333333, 35.0)
|
|
121
|
+
((15, 47, 0.0, 'S'), (35, 0, 0.0, 'E'))
|
|
122
|
+
"""
|
|
123
|
+
|
|
124
|
+
for value, name, maximum in (
|
|
125
|
+
(latitude, "Latitude", 90),
|
|
126
|
+
(longitude, "Longitude", 180),
|
|
127
|
+
):
|
|
128
|
+
if (
|
|
129
|
+
isinstance(value, bool)
|
|
130
|
+
or not isinstance(value, (int, float))
|
|
131
|
+
or value != value
|
|
132
|
+
or abs(value) == float("inf")
|
|
133
|
+
):
|
|
134
|
+
raise ValueError(f"{name} must be a finite number.")
|
|
135
|
+
|
|
136
|
+
if not -maximum <= value <= maximum:
|
|
137
|
+
raise ValueError(f"{name} must be between {-maximum} and {maximum}.")
|
|
138
|
+
|
|
139
|
+
lat_direction = "N" if latitude >= 0 else "S"
|
|
140
|
+
lon_direction = "E" if longitude >= 0 else "W"
|
|
141
|
+
|
|
142
|
+
latitude = abs(latitude)
|
|
143
|
+
longitude = abs(longitude)
|
|
144
|
+
|
|
145
|
+
def decimal_to_dms(value, direction):
|
|
146
|
+
degrees = int(value)
|
|
147
|
+
minutes_full = (value - degrees) * 60
|
|
148
|
+
minutes = int(minutes_full)
|
|
149
|
+
seconds = round((minutes_full - minutes) * 60, 6)
|
|
150
|
+
|
|
151
|
+
# Correct rounding that reaches 60 seconds or minutes.
|
|
152
|
+
if seconds >= 60:
|
|
153
|
+
seconds = 0.0
|
|
154
|
+
minutes += 1
|
|
155
|
+
|
|
156
|
+
if minutes >= 60:
|
|
157
|
+
minutes = 0
|
|
158
|
+
degrees += 1
|
|
159
|
+
|
|
160
|
+
return degrees, minutes, seconds, direction
|
|
161
|
+
|
|
162
|
+
return (
|
|
163
|
+
decimal_to_dms(latitude, lat_direction),
|
|
164
|
+
decimal_to_dms(longitude, lon_direction),
|
|
165
|
+
)
|
|
166
|
+
|