Spaces:
Sleeping
Sleeping
rmm
commited on
Commit
·
800aa5b
1
Parent(s):
859e75b
feat: implemented GPS coords extraction when present
Browse files- src/input_handling.py +37 -0
src/input_handling.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
from PIL import Image
|
2 |
from PIL import ExifTags
|
3 |
import re
|
@@ -193,6 +194,42 @@ def get_image_datetime(image_file: UploadedFile) -> str | None:
|
|
193 |
# TODO: add to logger
|
194 |
return None
|
195 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
196 |
|
197 |
# an arbitrary set of defaults so testing is less painful...
|
198 |
# ideally we add in some randomization to the defaults
|
|
|
1 |
+
from fractions import Fraction
|
2 |
from PIL import Image
|
3 |
from PIL import ExifTags
|
4 |
import re
|
|
|
194 |
# TODO: add to logger
|
195 |
return None
|
196 |
|
197 |
+
def decimal_coords(coords:tuple, ref:str) -> Fraction:
|
198 |
+
# https://stackoverflow.com/a/73267185
|
199 |
+
decimal_degrees = coords[0] + coords[1] / 60 + coords[2] / 3600
|
200 |
+
if ref == "S" or ref =='W':
|
201 |
+
decimal_degrees = -decimal_degrees
|
202 |
+
return decimal_degrees
|
203 |
+
|
204 |
+
|
205 |
+
def get_image_latlon(image_file: UploadedFile) -> tuple[float, float] | None:
|
206 |
+
"""
|
207 |
+
Extracts the latitude and longitude from the EXIF metadata of an uploaded image file.
|
208 |
+
|
209 |
+
Args:
|
210 |
+
image_file (UploadedFile): The uploaded image file from which to extract the latitude and longitude.
|
211 |
+
|
212 |
+
Returns:
|
213 |
+
tuple[float, float]: The latitude and longitude as a tuple if available, otherwise None.
|
214 |
+
|
215 |
+
Raises:
|
216 |
+
Warning: If the latitude and longitude could not be extracted from the image metadata.
|
217 |
+
"""
|
218 |
+
try:
|
219 |
+
image = Image.open(image_file)
|
220 |
+
exif_data = image._getexif()
|
221 |
+
if exif_data is not None:
|
222 |
+
if ExifTags.Base.GPSInfo in exif_data:
|
223 |
+
gps_ifd = exif_data.get(ExifTags.Base.GPSInfo)
|
224 |
+
|
225 |
+
lat = float(decimal_coords(gps_ifd[ExifTags.GPS.GPSLatitude], gps_ifd[ExifTags.GPS.GPSLatitudeRef]))
|
226 |
+
lon = float(decimal_coords(gps_ifd[ExifTags.GPS.GPSLongitude], gps_ifd[ExifTags.GPS.GPSLongitudeRef]))
|
227 |
+
|
228 |
+
return lat, lon
|
229 |
+
|
230 |
+
except Exception as e: # FIXME: what types of exception?
|
231 |
+
st.warning(f"Could not extract latitude and longitude from image metadata. (file: {str(image_file)}")
|
232 |
+
|
233 |
|
234 |
# an arbitrary set of defaults so testing is less painful...
|
235 |
# ideally we add in some randomization to the defaults
|