Fix retrieve geolocation from exif

This commit is contained in:
Cédric Leporcq 2022-04-21 07:09:28 +02:00
parent 114187415f
commit 9afb433eed
4 changed files with 44 additions and 37 deletions

View File

@ -14,5 +14,6 @@ timeout=1
day_begins=4 day_begins=4
# Path format # Path format
dirs_path=<%Y>/<%m-%b>-<city>-<folder> dirs_path=<%Y>/<%m-%b>_<location>_<folder>
name=<%Y%m%d-%H%M%S>-<%u<original_name>|%u<name>>.%l<ext> name=<%Y%m%d-%H%M%S>_<<original_name>|<name>>.%l<ext>
# name=<%Y%m%d-%H%M%S>-%u<original_name>.%l<ext>

View File

@ -133,11 +133,9 @@ def _get_paths(paths, root):
def _cli_get_location(collection): def _cli_get_location(collection):
gopt = collection.opt['Geolocation'] gopt = collection.opt['Geolocation']
return GeoLocation( return GeoLocation(
{ gopt['geocoder'],
'geocoder': gopt['geocoder'], gopt['prefer_english_names'],
'prefer_english_names': gopt['prefer_english_names'], gopt['timeout'],
'timeout': gopt['timeout'],
}
) )

View File

@ -78,6 +78,7 @@ class GeoLocation:
def lookup_osm( self, lat, lon, timeout=options.default_timeout): def lookup_osm( self, lat, lon, timeout=options.default_timeout):
"""Get Geolocation address data from latitude and longitude""" """Get Geolocation address data from latitude and longitude"""
locator_reverse = None
try: try:
locator = Nominatim(user_agent='myGeocoder', timeout=timeout) locator = Nominatim(user_agent='myGeocoder', timeout=timeout)
coords = (lat, lon) coords = (lat, lon)
@ -85,17 +86,17 @@ class GeoLocation:
lang = 'en' lang = 'en'
else: else:
lang = 'local' lang = 'local'
try:
locator_reverse = locator.reverse(coords, language=lang) locator_reverse = locator.reverse(coords, language=lang)
if locator_reverse is not None: except geopy.exc.GeocoderUnavailable or geopy.exc.GeocoderTimedOut as e:
return locator_reverse.raw
return None
except geopy.exc.GeocoderUnavailable or geopy.exc.GeocoderServiceError as e:
self.log.error(e) self.log.error(e)
return None
# Fix *** TypeError: `address` must not be None # Fix *** TypeError: `address` must not be None
except (TypeError, ValueError) as e: except (TypeError, ValueError) as e:
self.log.error(e) self.log.error(e)
else:
if locator_reverse is not None:
return locator_reverse.raw
return None return None

View File

@ -291,15 +291,26 @@ class Media(ReadExif):
self.src_dir = src_dir self.src_dir = src_dir
self.album_from_folder = album_from_folder self.album_from_folder = album_from_folder
self.cache = cache
self.interactive = interactive self.interactive = interactive
self.log = LOG.getChild(self.__class__.__name__) self.log = LOG.getChild(self.__class__.__name__)
self.metadata = None self.metadata = None
self.cache = cache
self.use_date_filename = use_date_filename self.use_date_filename = use_date_filename
self.use_file_dates = use_file_dates self.use_file_dates = use_file_dates
self.theme = request.load_theme() self.theme = request.load_theme()
self.loc_keys = (
'latitude',
'longitude',
'latitude_ref',
'longitude_ref',
'city',
'state',
'country',
'default',
)
def get_mimetype(self): def get_mimetype(self):
""" """
Get the mimetype of the file. Get the mimetype of the file.
@ -537,35 +548,30 @@ class Media(ReadExif):
return None, None 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 self.metadata['location_id'] = location_id
loc_keys = (
'latitude',
'longitude',
'latitude_ref',
'longitude_ref',
'city',
'state',
'country',
'default',
)
if location_id: 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 # use str to convert non string format data like latitude and
# longitude # longitude
self.metadata[key] = str( self.metadata[key] = str(
db.get_location_data(location_id, utils.snake2camel(key)) db.get_location_data(location_id, utils.snake2camel(key))
) )
elif loc: else:
for key in 'latitude', 'longitude', 'latitude_ref', 'longitude_ref': for key in self.loc_keys:
self.metadata[key] = None self.metadata[key] = None
def _set_location_from_coordinates(self, loc):
self.metadata['location_id'] = None
if loc:
place_name = loc.place_name( place_name = loc.place_name(
self.metadata['latitude'], self.metadata['longitude'] self.metadata['latitude'], self.metadata['longitude']
) )
self.log.debug("location: {place_name['default']}")
for key in ('city', 'state', 'country', 'default'): for key in ('city', 'state', 'country', 'default'):
# mask = 'city' # mask = 'city'
# place_name = {'default': u'Sunnyvale', 'city-random': u'Sunnyvale'} # place_name = {'default': u'Sunnyvale', 'city-random': u'Sunnyvale'}
@ -573,9 +579,8 @@ class Media(ReadExif):
self.metadata[key] = place_name[key] self.metadata[key] = place_name[key]
else: else:
self.metadata[key] = None self.metadata[key] = None
else: else:
for key in loc_keys: for key in self.loc_keys:
self.metadata[key] = None self.metadata[key] = None
def _set_album_from_folder(self): def _set_album_from_folder(self):
@ -608,7 +613,9 @@ class Media(ReadExif):
relpath, db_checksum = self._check_file(db, root) relpath, db_checksum = self._check_file(db, root)
if db_checksum: if db_checksum:
location_id = self._set_metadata_from_db(db, relpath) location_id = self._set_metadata_from_db(db, relpath)
self._set_location_from_db(location_id, db)
else: else:
# file not in db
self.metadata['src_dir'] = str(self.src_dir) self.metadata['src_dir'] = str(self.src_dir)
self.metadata['subdirs'] = str( self.metadata['subdirs'] = str(
self.file_path.relative_to(self.src_dir).parent self.file_path.relative_to(self.src_dir).parent
@ -616,10 +623,10 @@ class Media(ReadExif):
self.metadata['filename'] = self.file_path.name self.metadata['filename'] = self.file_path.name
self._set_metadata_from_exif() self._set_metadata_from_exif()
self._set_location_from_coordinates(loc)
self.metadata['date_media'] = self.get_date_media() self.metadata['date_media'] = self.get_date_media()
self.metadata['location_id'] = location_id
self._set_location_metadata(location_id, db, loc)
if self.album_from_folder: if self.album_from_folder:
self._set_album_from_folder() self._set_album_from_folder()