Skip to content

Commit

Permalink
Merge pull request #130 from Turbo87/feat/zone_letter_to_central_lati…
Browse files Browse the repository at this point in the history
…tude

Add zone_letter_to_central_latitude
  • Loading branch information
bartvanandel authored Jan 3, 2025
2 parents 6424378 + 5a202c5 commit 382cda1
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
30 changes: 30 additions & 0 deletions test/test_utm.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,3 +478,33 @@ def test_numpy_args_not_modified():
UTM.to_latlon(easting, northing, zone, letter)
assert easting == TEST_EASTING
assert northing == TEST_NORTHING


@pytest.mark.parametrize(
"zone_number, expected_lon",
[
(1, -177),
(12, -111),
(16, -87),
(31, 3),
(37, 39),
],
)
def test_zone_number_to_central_longitude(zone_number, expected_lon):
lon = UTM.zone_number_to_central_longitude(zone_number)
assert lon == expected_lon


@pytest.mark.parametrize(
"zone_letter, expected_lat",
[
("X", 78),
("C", -76),
("E", -60),
("F", -52),
("Q", 20),
],
)
def test_zone_letter_to_central_latitude(zone_letter, expected_lat):
lat = UTM.zone_letter_to_central_latitude(zone_letter)
assert lat == expected_lat
2 changes: 1 addition & 1 deletion utm/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from utm.conversion import to_latlon, from_latlon, latlon_to_zone_number, latitude_to_zone_letter, check_valid_zone
from utm.conversion import to_latlon, from_latlon, latlon_to_zone_number, latitude_to_zone_letter, check_valid_zone, zone_number_to_central_longitude, zone_letter_to_central_latitude
from utm.error import OutOfRangeError
from utm._version import __version__
29 changes: 22 additions & 7 deletions utm/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,21 @@ def in_bounds(x, lower, upper, upper_strict=False):
return lower <= x <= upper


def check_valid_zone(zone_number, zone_letter):
def check_valid_zone_letter(zone_letter):
zone_letter = zone_letter.upper()
if not 'C' <= zone_letter <= 'X' or zone_letter in ['I', 'O']:
raise OutOfRangeError('zone letter out of range (must be between C and X)')


def check_valid_zone_number(zone_number):
if not 1 <= zone_number <= 60:
raise OutOfRangeError('zone number out of range (must be between 1 and 60)')

if zone_letter:
zone_letter = zone_letter.upper()

if not 'C' <= zone_letter <= 'X' or zone_letter in ['I', 'O']:
raise OutOfRangeError('zone letter out of range (must be between C and X)')
def check_valid_zone(zone_number, zone_letter):
check_valid_zone_number(zone_number)
if zone_letter:
check_valid_zone_letter(zone_letter)


def mixed_signs(x):
Expand Down Expand Up @@ -119,9 +125,9 @@ def to_latlon(easting, northing, zone_number, zone_letter=None, northern=None, s
raise OutOfRangeError('easting out of range (must be between 100,000 m and 999,999 m)')
if not in_bounds(northing, 0, 10000000):
raise OutOfRangeError('northing out of range (must be between 0 m and 10,000,000 m)')

check_valid_zone(zone_number, zone_letter)

if zone_letter:
zone_letter = zone_letter.upper()
northern = (zone_letter >= 'N')
Expand Down Expand Up @@ -331,4 +337,13 @@ def latlon_to_zone_number(latitude, longitude):


def zone_number_to_central_longitude(zone_number):
check_valid_zone_number(zone_number)
return (zone_number - 1) * 6 - 180 + 3

def zone_letter_to_central_latitude(zone_letter):
check_valid_zone_letter(zone_letter)
zone_letter = zone_letter.upper()
if zone_letter == 'X':
return 78
else:
return -76 + (ZONE_LETTERS.index(zone_letter) * 8)

0 comments on commit 382cda1

Please sign in to comment.