From 9b96c4c74509452fe3871aa79927224302c846de Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Thu, 19 Oct 2023 18:33:20 +0300 Subject: [PATCH] scanning photos for GPS datat --- photomap/photo_map.html | 12030 ++++++++++++++++++++++++++++++++++++++ photomap/pmap.py | 84 + 2 files changed, 12114 insertions(+) create mode 100644 photomap/photo_map.html create mode 100644 photomap/pmap.py diff --git a/photomap/photo_map.html b/photomap/photo_map.html new file mode 100644 index 0000000..d8a2d57 --- /dev/null +++ b/photomap/photo_map.html @@ -0,0 +1,12030 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + \ No newline at end of file diff --git a/photomap/pmap.py b/photomap/pmap.py new file mode 100644 index 0000000..5c311ba --- /dev/null +++ b/photomap/pmap.py @@ -0,0 +1,84 @@ + +import os +import folium +from pathlib import Path +from PIL import Image, ExifTags + +# Define function to extract latitude and longitude from photo +def get_coordinates(photo_path): + """Extracting EXIF data from jpg files requires an external package because teh EXIF standard + is interpreted differently by the many different implementations of the JPG file format + """ + try: + # Read EXIF data from photo + img = Image.open(photo_path) + exif_data = img._getexif() + + # Extract latitude and longitude from EXIF data + gps_info = exif_data.get(34853) #'GPSInfo is a dict + # This does not mean that all the GPS fields are populated. + + lat = gps_info.get(2, None) + lon = gps_info.get(4, None) + isn = gps_info.get(1, None) + ise = gps_info.get(3, None) + if isn and isn != "N": + print(f"{photo_path} WRONG hemisphere N/S {isn}") + if ise and ise != "E": + print(f"{photo_path} WRONG hemisphere E/W") + point = gps_info.get(17, None) # direction the camera is point towards + pointref = gps_info.get(16, None) # "M" for magnetic + taken = gps_info.get(29, None) # GPS datestamp + except: + #print(f"{photo_path} - lat/lon exception") + # for key, value in gps_info.items(): + # print(key, value, value.type()) + return None, None + + # Convert coordinates to decimal format + if lat and lon: + # lat and lon are tuples e.g. (47.0, 35.0, 5.77) + latitude = float(lat[0] + lat[1] / 60 + lat[2] / 3600) + longitude = float(lon[0] + lon[1] / 60 + lon[2] / 3600) + + # Reverse geocode coordinates to get address + #address = geolocator.reverse((latitude, longitude)) + return latitude, longitude + else: + # print(f"{photo_path} - lat/lon conversion exception {lat=} {lon=}") + # for key, value in gps_info.items(): + # print(key, value, type(value)) + return None, None + + +# Specify the folder containing the photos +photo_folder = Path("../../expoweb") +photo_folder = Path("/mnt/d/EXPO/PHOTOS") + +photos = photo_folder.rglob("*.jpg") +# for p in photos: + # print(p) + +# Initialize a Folium map centered at an initial location +map = folium.Map(location=[47.691036, 13.821314], zoom_start=13) + +photoset = [] +# Iterate through photos in the folder +for filename in photos: # os.listdir(photo_folder): + photo_path = filename #os.path.join(photo_folder, filename) + + # Extract latitude, longitude, and address from photo + latitude, longitude = get_coordinates(photo_path) + + if latitude and longitude: + print(f"{photo_path} {latitude:.6f} {longitude:.6f}") + # Create a marker for each photo with its location and address + folium.Marker( + location=[latitude, longitude], + popup=f"{filename}", + icon=folium.Icon(color="red", icon="camera"), + ).add_to(map) + photoset.append(filename) +# Save the map as an HTML file +map.save("photo_map.html") +print(f"Found {len(photoset)} GPS located photos in '{photo_folder}' and subdirectories.")