Closes gh-50 Add unsigned option to `geolocation.decimal_to_dms` function and call with `False` from photo.py

This commit is contained in:
Jaisen Mathai 2015-12-27 00:10:19 -08:00
parent 229a23c471
commit c9d7bc7102
3 changed files with 25 additions and 4 deletions

View File

@ -44,18 +44,24 @@ def coordinates_by_name(name):
return None return None
def decimal_to_dms(decimal): def decimal_to_dms(decimal, signed=True):
# if decimal is negative we need to make the degrees and minutes negative also # if decimal is negative we need to make the degrees and minutes negative also
sign = 1 sign = 1
if(decimal < 0): if(decimal < 0):
sign = -1 sign = -1
# http://anothergisblog.blogspot.com/2011/11/convert-decimal-degree-to-degrees.html
degrees = int(decimal) degrees = int(decimal)
subminutes = abs((decimal - int(decimal)) * 60) subminutes = abs((decimal - int(decimal)) * 60)
minutes = int(subminutes) * sign minutes = int(subminutes) * sign
subseconds = abs((subminutes - int(subminutes)) * 60) * sign subseconds = abs((subminutes - int(subminutes)) * 60) * sign
subseconds_fraction = Fraction(subseconds) subseconds_fraction = Fraction(subseconds)
if(signed == False):
degrees = abs(degrees)
minutes = abs(minutes)
subseconds_fraction = Fraction(abs(subseconds))
return (pyexiv2.Rational(degrees, 1), pyexiv2.Rational(minutes, 1), pyexiv2.Rational(subseconds_fraction.numerator, subseconds_fraction.denominator)) return (pyexiv2.Rational(degrees, 1), pyexiv2.Rational(minutes, 1), pyexiv2.Rational(subseconds_fraction.numerator, subseconds_fraction.denominator))
def dms_to_decimal(degrees, minutes, seconds, sign=' '): def dms_to_decimal(degrees, minutes, seconds, sign=' '):

View File

@ -172,9 +172,9 @@ class Photo(Media):
exif_metadata = pyexiv2.ImageMetadata(source) exif_metadata = pyexiv2.ImageMetadata(source)
exif_metadata.read() exif_metadata.read()
exif_metadata['Exif.GPSInfo.GPSLatitude'] = geolocation.decimal_to_dms(latitude) exif_metadata['Exif.GPSInfo.GPSLatitude'] = geolocation.decimal_to_dms(latitude, False)
exif_metadata['Exif.GPSInfo.GPSLatitudeRef'] = pyexiv2.ExifTag('Exif.GPSInfo.GPSLatitudeRef', 'N' if latitude >= 0 else 'S') exif_metadata['Exif.GPSInfo.GPSLatitudeRef'] = pyexiv2.ExifTag('Exif.GPSInfo.GPSLatitudeRef', 'N' if latitude >= 0 else 'S')
exif_metadata['Exif.GPSInfo.GPSLongitude'] = geolocation.decimal_to_dms(longitude) exif_metadata['Exif.GPSInfo.GPSLongitude'] = geolocation.decimal_to_dms(longitude, False)
exif_metadata['Exif.GPSInfo.GPSLongitudeRef'] = pyexiv2.ExifTag('Exif.GPSInfo.GPSLongitudeRef', 'E' if longitude >= 0 else 'W') exif_metadata['Exif.GPSInfo.GPSLongitudeRef'] = pyexiv2.ExifTag('Exif.GPSInfo.GPSLongitudeRef', 'E' if longitude >= 0 else 'W')
exif_metadata.write() exif_metadata.write()

View File

@ -25,3 +25,18 @@ def test_decimal_to_dms():
check_value = round(check_value, 8) check_value = round(check_value, 8)
assert target_decimal_value == check_value, '%s does not match %s' % (check_value, target_decimal_value) assert target_decimal_value == check_value, '%s does not match %s' % (check_value, target_decimal_value)
def test_decimal_to_dms_unsigned():
for x in range(0, 1000):
target_decimal_value = random.uniform(0.0, 180.0) * -1
dms = geolocation.decimal_to_dms(target_decimal_value, False)
check_value = dms[0].to_float() + dms[1].to_float() / 60 + dms[2].to_float() / 3600
target_decimal_value = round(target_decimal_value, 8)
check_value = round(check_value, 8)
new_target_decimal_value = abs(target_decimal_value)
assert new_target_decimal_value == check_value, '%s does not match %s' % (check_value, new_target_decimal_value)