From d18786b00028f73abf324c639cc362d2524445a2 Mon Sep 17 00:00:00 2001 From: rrr-marble Date: Sun, 19 Dec 2021 23:17:34 +0300 Subject: [PATCH] add: transform coordinates to decimal form --- util/import_photos.py | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/util/import_photos.py b/util/import_photos.py index 32a4065..401e750 100644 --- a/util/import_photos.py +++ b/util/import_photos.py @@ -6,9 +6,44 @@ import filetype from os import path, walk from sys import argv, stderr from shutil import move +from fractions import Fraction import sqlite3 +def decimal_from_rational64u(dms: str): + """Convert coordinates from rational64u EXIF uses to represent + degrees, minutes, and seconds of a point to decimal format + + General formula is + dec_degree = degreesNumerator / degreesDenominator + + minutesNumerator / minutesDenominator / 60 + + secondsNumerator / secondsDenominator / 3600 + + 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 + + >>> decimal_from_rational64u(dms="42/1, 18/1, 2914/100") + 42.30809 + """ + + # 1 split by comma + # 2 turn each into Fraction + # 3 zip fractions with their respective (degrees, minutes, seconds) denominator + # 4 divide the fraction + # 5 sum up the result + # 6 convert to decimal + # 7 round to 5th decimal point + return round( + float( + sum( + a / b + for (a, b) in zip((Fraction(f) for f in dms.split(",")), (1, 60, 3600)) + ) + ), + 5, + ) + + def process_pictures(source: str, dest_shrunk: str, dest_original: str): """Process images from the base directory in the first command line argument. Place the resized copies to dest_shrunk and @@ -53,9 +88,9 @@ def process_pictures(source: str, dest_shrunk: str, dest_original: str): "ResizedImage": path.join(dest_shrunk, filename), "OriginalImage": path.join(dest_original, filename), "DateTimeOriginal": exif["DateTimeOriginal"], # Q: normalize it? - "GPSLatitude": exif["GPSLatitude"], + "GPSLatitude": decimal_from_rational64u(exif["GPSLatitude"]), "GPSLatitudeRef": exif["GPSLatitudeRef"], - "GPSLongitude": exif["GPSLongitude"], + "GPSLongitude": decimal_from_rational64u(exif["GPSLongitude"]), "GPSLongitudeRef": exif["GPSLongitudeRef"], } except KeyError as e: