From 9afb433eed68512e2e74d3a468f08e88dee46a48 Mon Sep 17 00:00:00 2001 From: Cedric Leporcq Date: Thu, 21 Apr 2022 07:09:28 +0200 Subject: [PATCH] Fix retrieve geolocation from exif --- ordigi.conf | 5 +++-- ordigi/cli.py | 8 +++----- ordigi/geolocation.py | 21 ++++++++++--------- ordigi/media.py | 47 +++++++++++++++++++++++++------------------ 4 files changed, 44 insertions(+), 37 deletions(-) diff --git a/ordigi.conf b/ordigi.conf index 3b7ee2d..e920de5 100644 --- a/ordigi.conf +++ b/ordigi.conf @@ -14,5 +14,6 @@ timeout=1 day_begins=4 # Path format -dirs_path=<%Y>/<%m-%b>-- -name=<%Y%m%d-%H%M%S>-<%u|%u>.%l +dirs_path=<%Y>/<%m-%b>__ +name=<%Y%m%d-%H%M%S>_<|>.%l +# name=<%Y%m%d-%H%M%S>-%u.%l diff --git a/ordigi/cli.py b/ordigi/cli.py index cad8444..695d5d5 100755 --- a/ordigi/cli.py +++ b/ordigi/cli.py @@ -133,11 +133,9 @@ def _get_paths(paths, root): def _cli_get_location(collection): gopt = collection.opt['Geolocation'] return GeoLocation( - { - 'geocoder': gopt['geocoder'], - 'prefer_english_names': gopt['prefer_english_names'], - 'timeout': gopt['timeout'], - } + gopt['geocoder'], + gopt['prefer_english_names'], + gopt['timeout'], ) diff --git a/ordigi/geolocation.py b/ordigi/geolocation.py index db70f0f..629c0a3 100644 --- a/ordigi/geolocation.py +++ b/ordigi/geolocation.py @@ -78,6 +78,7 @@ class GeoLocation: def lookup_osm( self, lat, lon, timeout=options.default_timeout): """Get Geolocation address data from latitude and longitude""" + locator_reverse = None try: locator = Nominatim(user_agent='myGeocoder', timeout=timeout) coords = (lat, lon) @@ -85,17 +86,17 @@ class GeoLocation: lang = 'en' else: lang = 'local' - locator_reverse = locator.reverse(coords, language=lang) - if locator_reverse is not None: - return locator_reverse.raw - - return None - - except geopy.exc.GeocoderUnavailable or geopy.exc.GeocoderServiceError as e: - self.log.error(e) - return None + try: + locator_reverse = locator.reverse(coords, language=lang) + except geopy.exc.GeocoderUnavailable or geopy.exc.GeocoderTimedOut as e: + self.log.error(e) # Fix *** TypeError: `address` must not be None except (TypeError, ValueError) as e: self.log.error(e) - return None + else: + if locator_reverse is not None: + return locator_reverse.raw + + + return None diff --git a/ordigi/media.py b/ordigi/media.py index a6ccfe7..368aec6 100644 --- a/ordigi/media.py +++ b/ordigi/media.py @@ -291,15 +291,26 @@ class Media(ReadExif): self.src_dir = src_dir self.album_from_folder = album_from_folder + self.cache = cache self.interactive = interactive self.log = LOG.getChild(self.__class__.__name__) self.metadata = None - self.cache = cache self.use_date_filename = use_date_filename self.use_file_dates = use_file_dates self.theme = request.load_theme() + self.loc_keys = ( + 'latitude', + 'longitude', + 'latitude_ref', + 'longitude_ref', + 'city', + 'state', + 'country', + 'default', + ) + def get_mimetype(self): """ Get the mimetype of the file. @@ -537,35 +548,30 @@ class Media(ReadExif): return None, None - def _set_location_metadata(self, location_id, db, loc=None): + def _set_location_from_db(self, location_id, db): self.metadata['location_id'] = location_id - loc_keys = ( - 'latitude', - 'longitude', - 'latitude_ref', - 'longitude_ref', - 'city', - 'state', - 'country', - 'default', - ) - if location_id: - for key in loc_keys: + for key in self.loc_keys: # use str to convert non string format data like latitude and # longitude self.metadata[key] = str( db.get_location_data(location_id, utils.snake2camel(key)) ) - elif loc: - for key in 'latitude', 'longitude', 'latitude_ref', 'longitude_ref': + else: + for key in self.loc_keys: self.metadata[key] = None + def _set_location_from_coordinates(self, loc): + + self.metadata['location_id'] = None + + if loc: place_name = loc.place_name( self.metadata['latitude'], self.metadata['longitude'] ) + self.log.debug("location: {place_name['default']}") for key in ('city', 'state', 'country', 'default'): # mask = 'city' # place_name = {'default': u'Sunnyvale', 'city-random': u'Sunnyvale'} @@ -573,9 +579,8 @@ class Media(ReadExif): self.metadata[key] = place_name[key] else: self.metadata[key] = None - else: - for key in loc_keys: + for key in self.loc_keys: self.metadata[key] = None def _set_album_from_folder(self): @@ -608,7 +613,9 @@ class Media(ReadExif): relpath, db_checksum = self._check_file(db, root) if db_checksum: location_id = self._set_metadata_from_db(db, relpath) + self._set_location_from_db(location_id, db) else: + # file not in db self.metadata['src_dir'] = str(self.src_dir) self.metadata['subdirs'] = str( self.file_path.relative_to(self.src_dir).parent @@ -616,10 +623,10 @@ class Media(ReadExif): self.metadata['filename'] = self.file_path.name self._set_metadata_from_exif() + self._set_location_from_coordinates(loc) self.metadata['date_media'] = self.get_date_media() - - self._set_location_metadata(location_id, db, loc) + self.metadata['location_id'] = location_id if self.album_from_folder: self._set_album_from_folder()