[coordinates] adding to_valid_latitude to bound 90 and -90 degrees
This commit is contained in:
@@ -19,6 +19,7 @@ import math
|
||||
import re
|
||||
|
||||
from geodata.encoding import safe_decode
|
||||
from geodata.math.floats import isclose
|
||||
|
||||
beginning_re = re.compile('^[^0-9\-]+', re.UNICODE)
|
||||
end_re = re.compile('[^0-9]+$', re.UNICODE)
|
||||
@@ -78,6 +79,19 @@ def is_valid_longitude(longitude):
|
||||
return not math.isinf(longitude) and not math.isnan(longitude)
|
||||
|
||||
|
||||
def to_valid_latitude(latitude):
|
||||
'''Convert longitude into the -180 to 180 scale'''
|
||||
if not is_valid_latitude(latitude):
|
||||
raise ValueError('Invalid latitude {}'.format(latitude))
|
||||
|
||||
if isclose(latitude, 90.0):
|
||||
latitude = 89.9999
|
||||
elif isclose(latitude, -90.0):
|
||||
latitude = -89.9999
|
||||
|
||||
return latitude
|
||||
|
||||
|
||||
def to_valid_longitude(longitude):
|
||||
'''Convert longitude into the -180 to 180 scale'''
|
||||
if not is_valid_longitude(longitude):
|
||||
@@ -145,6 +159,7 @@ def latlon_to_decimal(latitude, longitude):
|
||||
if not is_valid_longitude(longitude):
|
||||
raise ValueError('Invalid longitude: {}'.format(longitude))
|
||||
|
||||
latitude = to_valid_latitude(latitude)
|
||||
longitude = to_valid_longitude(longitude)
|
||||
|
||||
return latitude, longitude
|
||||
|
||||
Reference in New Issue
Block a user