diff --git a/elodie/geolocation.py b/elodie/geolocation.py index 1616b44..14b6ddf 100644 --- a/elodie/geolocation.py +++ b/elodie/geolocation.py @@ -54,10 +54,12 @@ def decimal_to_dms(decimal): >>> decimal_to_dms(-125.976893) [Fraction(125, 1), Fraction(58, 1), Fraction(92037, 2500)] """ - remainder, degrees = math.modf(abs(decimal)) - remainder, minutes = math.modf(remainder * 60) # @TODO figure out a better and more proper way to do seconds - return (pyexiv2.Rational(degrees, 1), pyexiv2.Rational(minutes, 1), pyexiv2.Rational(int(remainder*1000000000), 1000000000)) + degrees = int(decimal) + subminutes = abs((decimal - int(decimal)) * 60) + minutes = int(subminutes) + subseconds = abs((subminutes - int(subminutes)) * 60) + return (pyexiv2.Rational(degrees, 1), pyexiv2.Rational(minutes, 1), pyexiv2.Rational(subseconds, 1)) def dms_to_decimal(degrees, minutes, seconds, sign=' '): """Convert degrees, minutes, seconds into decimal degrees. diff --git a/elodie/tests/geolocation_test.py b/elodie/tests/geolocation_test.py new file mode 100644 index 0000000..dc4d4d5 --- /dev/null +++ b/elodie/tests/geolocation_test.py @@ -0,0 +1,20 @@ +# Project imports +import os +import sys + +import re + +sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))) + +import helper +from elodie import geolocation + +os.environ['TZ'] = 'GMT' + +def test_decimal_to_dms(): + target_decimal_value = 37.383336725 + + dms = geolocation.decimal_to_dms(target_decimal_value) + check_value = dms[0].to_float() + dms[1].to_float() / 60 + dms[2].to_float() / 3600 + + assert target_decimal_value == check_value, '%s does not match %s' % (check_value, target_decimal_value)