add: take into account reference cardinal direction

pull/11/head
rrr-marble 4 years ago
parent d18786b000
commit 6bd14777a3

@ -10,9 +10,11 @@ from fractions import Fraction
import sqlite3 import sqlite3
def decimal_from_rational64u(dms: str): def decimal_from_rational64u(dms: str, ref):
"""Convert coordinates from rational64u EXIF uses to represent """Convert coordinates from rational64u EXIF uses to represent
degrees, minutes, and seconds of a point to decimal format degrees, minutes, and seconds of a point to decimal format
Take into account reference cardinal direction and
turn [S]outh and [W]est coordinates negative
General formula is General formula is
dec_degree = degreesNumerator / degreesDenominator dec_degree = degreesNumerator / degreesDenominator
@ -22,8 +24,8 @@ def decimal_from_rational64u(dms: str):
https://en.wikipedia.org/wiki/Geographic_coordinate_conversion https://en.wikipedia.org/wiki/Geographic_coordinate_conversion
https://gis.stackexchange.com/questions/136925/how-to-parse-exif-gps-information-to-lat-lng-decimal-numbers https://gis.stackexchange.com/questions/136925/how-to-parse-exif-gps-information-to-lat-lng-decimal-numbers
>>> decimal_from_rational64u(dms="42/1, 18/1, 2914/100") >>> decimal_from_rational64u(dms="42/1, 18/1, 2914/100", "S")
42.30809 -42.30809
""" """
# 1 split by comma # 1 split by comma
@ -33,7 +35,7 @@ def decimal_from_rational64u(dms: str):
# 5 sum up the result # 5 sum up the result
# 6 convert to decimal # 6 convert to decimal
# 7 round to 5th decimal point # 7 round to 5th decimal point
return round( dec_coordinates = round(
float( float(
sum( sum(
a / b a / b
@ -42,6 +44,10 @@ def decimal_from_rational64u(dms: str):
), ),
5, 5,
) )
if ref in ("S", "W"):
dec_coordinates = -dec_coordinates
return dec_coordinates
def process_pictures(source: str, dest_shrunk: str, dest_original: str): def process_pictures(source: str, dest_shrunk: str, dest_original: str):
@ -88,10 +94,12 @@ def process_pictures(source: str, dest_shrunk: str, dest_original: str):
"ResizedImage": path.join(dest_shrunk, filename), "ResizedImage": path.join(dest_shrunk, filename),
"OriginalImage": path.join(dest_original, filename), "OriginalImage": path.join(dest_original, filename),
"DateTimeOriginal": exif["DateTimeOriginal"], # Q: normalize it? "DateTimeOriginal": exif["DateTimeOriginal"], # Q: normalize it?
"GPSLatitude": decimal_from_rational64u(exif["GPSLatitude"]), "GPSLatitude": decimal_from_rational64u(
"GPSLatitudeRef": exif["GPSLatitudeRef"], exif["GPSLatitude"], exif["GPSLatitudeRef"]
"GPSLongitude": decimal_from_rational64u(exif["GPSLongitude"]), ),
"GPSLongitudeRef": exif["GPSLongitudeRef"], "GPSLongitude": decimal_from_rational64u(
exif["GPSLongitude"], exif["GPSLongitudeRef"]
),
} }
except KeyError as e: except KeyError as e:
print(f"Image '{filename}' has no valid exif") print(f"Image '{filename}' has no valid exif")

Loading…
Cancel
Save