2024-07-24 19:13:42 +02:00
|
|
|
###
|
|
|
|
# Filtrage de photos situées en France dans un dossier contenant des séquences de photo géolocalisées
|
|
|
|
# Rechercher et déplacer automatiquement les photos géolocalisées dans une certaine bounding box et les déplacer dans le dossier de destination.
|
|
|
|
#
|
|
|
|
# utilisation:
|
|
|
|
# python find_france_photos_and_move.py --source_dir /chemin/du/répertoire/source --destination_dir /chemin/du/répertoire/destination
|
|
|
|
#
|
|
|
|
###
|
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import exifread
|
|
|
|
|
|
|
|
# Définition du rectangle entourant la France métropolitaine et un peu autour
|
2024-10-25 23:19:29 +02:00
|
|
|
france_bbox: tuple[float, float, float, float] = (
|
|
|
|
42.0,
|
|
|
|
-5.0,
|
|
|
|
51.0,
|
|
|
|
10.0,
|
|
|
|
) # (lat_min, lon_min, lat_max, lon_max)
|
2024-07-24 19:13:42 +02:00
|
|
|
# Définition du rectangle entourant la France métropolitaine et un peu autour
|
2024-10-25 23:19:29 +02:00
|
|
|
france_bbox: tuple[float, float, float, float] = (
|
|
|
|
42.0,
|
|
|
|
-5.0,
|
|
|
|
51.0,
|
|
|
|
10.0,
|
|
|
|
) # (lat_min, lon_min, lat_max, lon_max)
|
2024-07-24 19:13:42 +02:00
|
|
|
|
|
|
|
# Définition du rectangle entourant la Guadeloupe
|
|
|
|
guadeloupe_bbox: tuple[float, float, float, float] = (15.8, -61.8, 17.3, -59.3)
|
|
|
|
|
|
|
|
# Définition du rectangle entourant la Martinique
|
|
|
|
martinique_bbox: tuple[float, float, float, float] = (14.3, -61.3, 15.1, -59.3)
|
|
|
|
|
|
|
|
# Définition du rectangle entourant la Guyane française
|
|
|
|
guyane_bbox: tuple[float, float, float, float] = (2.0, -54.5, 6.5, -51.5)
|
|
|
|
|
|
|
|
# Définition du rectangle entourant La Réunion
|
|
|
|
reunion_bbox: tuple[float, float, float, float] = (-21.3, 55.2, -20.8, 55.8)
|
|
|
|
|
|
|
|
# Définition du rectangle entourant Mayotte
|
|
|
|
mayotte_bbox: tuple[float, float, float, float] = (-13.0, 45.0, -12.5, 45.5)
|
|
|
|
|
|
|
|
# Définition du rectangle entourant Saint-Pierre-et-Miquelon
|
|
|
|
spm_bbox: tuple[float, float, float, float] = (46.7, -56.2, 47.1, -55.6)
|
|
|
|
|
|
|
|
# Définition du rectangle entourant les îles de Saint-Martin et Saint-Barthélemy
|
|
|
|
stm_sbh_bbox: tuple[float, float, float, float] = (18.0, -64.5, 18.5, -62.5)
|
|
|
|
|
|
|
|
# Définition du rectangle entourant Wallis-et-Futuna
|
|
|
|
wf_bbox: tuple[float, float, float, float] = (-13.3, -176.2, -13.1, -175.8)
|
|
|
|
|
|
|
|
# Définition du rectangle entourant la Nouvelle-Calédonie
|
|
|
|
nc_bbox: tuple[float, float, float, float] = (-22.5, 165.5, -18.5, 169.5)
|
|
|
|
|
|
|
|
# Définition du rectangle entourant la Polynésie française
|
|
|
|
pf_bbox: tuple[float, float, float, float] = (-27.5, -140.0, -7.5, -134.0)
|
|
|
|
|
|
|
|
# Définition du rectangle entourant les Terres australes et antarctiques françaises
|
|
|
|
taaf_bbox: tuple[float, float, float, float] = (-49.5, 68.5, -37.5, 77.5)
|
|
|
|
|
|
|
|
# Chemin du répertoire source
|
2024-10-25 23:19:29 +02:00
|
|
|
source_dir: str = "/home/cipherbliss/Téléchargements/FIBRELAND/TEST_IN_FR/"
|
2024-07-24 19:13:42 +02:00
|
|
|
|
|
|
|
# Chemin du répertoire destination
|
2024-10-25 23:19:29 +02:00
|
|
|
destination_dir: str = "/home/cipherbliss/Téléchargements/FIBRELAND/IN_FRANCE/"
|
|
|
|
sequence_folder: str = "principale_sequence"
|
2024-07-24 19:13:42 +02:00
|
|
|
count_files_all: int = 0
|
|
|
|
count_files_moved: int = 0
|
|
|
|
# Crée le répertoire destination si il n'existe pas
|
|
|
|
if not os.path.exists(destination_dir):
|
|
|
|
os.makedirs(destination_dir)
|
|
|
|
|
|
|
|
|
|
|
|
# Fonction pour déplacer un fichier si il est dans le rectangle de la France
|
|
|
|
def move_file_if_in_france(filepath, sequence_folder):
|
|
|
|
global count_files_all
|
|
|
|
global count_files_moved
|
|
|
|
|
|
|
|
# Ouvre le fichier image et lit les informations EXIF
|
|
|
|
latitude, longitude = get_gps_info(filepath)
|
|
|
|
|
|
|
|
if latitude and longitude:
|
2024-10-25 23:19:29 +02:00
|
|
|
print(f"Latitude: {latitude}, Longitude: {longitude}")
|
2024-07-24 19:13:42 +02:00
|
|
|
if are_lat_lon_in_france(latitude, longitude):
|
|
|
|
move_file_in_destination(filepath, sequence_folder)
|
|
|
|
else:
|
2024-10-25 23:19:29 +02:00
|
|
|
print("Informations GPS non trouvées")
|
2024-07-24 19:13:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
def move_file_in_destination(filepath, sequence_folder):
|
|
|
|
global count_files_moved
|
|
|
|
# Déplace le fichier dans le sous-répertoire "photos_in_france"
|
2024-10-25 23:19:29 +02:00
|
|
|
dest_subdir = os.path.join(
|
|
|
|
destination_dir, sequence_folder, os.path.basename(os.path.dirname(filepath))
|
|
|
|
)
|
2024-07-24 19:13:42 +02:00
|
|
|
if not os.path.exists(dest_subdir):
|
|
|
|
os.makedirs(dest_subdir)
|
|
|
|
shutil.move(filepath, os.path.join(dest_subdir, filepath))
|
|
|
|
count_files_moved += 1
|
|
|
|
print(f"Moved {filepath} to {dest_subdir}")
|
|
|
|
return True
|
|
|
|
|
2024-10-25 23:19:29 +02:00
|
|
|
|
2024-07-24 19:13:42 +02:00
|
|
|
def are_lat_lon_in_france(gps_lat, gps_lon):
|
|
|
|
"""
|
|
|
|
recherche d'une zone du territoire français
|
|
|
|
|
|
|
|
|
|
|
|
France métropolitaine : 551 695 km²
|
|
|
|
Terres australes et antarctiques françaises : 432 000 km²
|
|
|
|
Guyane française : 83 534 km²
|
|
|
|
Nouvelle-Calédonie : 18 575 km²
|
|
|
|
Polynésie française : 4 167 km²
|
|
|
|
La Réunion : 2 512 km²
|
|
|
|
Martinique : 1 128 km²
|
|
|
|
Guadeloupe : 1 628 km²
|
|
|
|
Mayotte : 374 km²
|
|
|
|
Saint-Pierre-et-Miquelon : 242 km²
|
|
|
|
Wallis-et-Futuna : 142 km²
|
|
|
|
Saint-Martin et Saint-Barthélemy : 53 km²
|
|
|
|
|
|
|
|
|
|
|
|
:param gps_lat:
|
|
|
|
:param gps_lon:
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
global france_bbox, guyane_bbox, nc_bbox, pf_bbox, reunion_bbox, guadeloupe_bbox, martinique_bbox, mayotte_bbox, spm_bbox, stm_sbh_bbox, wf_bbox, taaf_bbox
|
|
|
|
|
|
|
|
print("lat lon :", gps_lat, gps_lon)
|
|
|
|
|
2024-10-25 23:19:29 +02:00
|
|
|
if (
|
|
|
|
france_bbox[0] <= gps_lat <= france_bbox[2]
|
|
|
|
and france_bbox[1] <= gps_lon <= france_bbox[3]
|
|
|
|
):
|
2024-07-24 19:13:42 +02:00
|
|
|
return "France métropolitaine"
|
2024-10-25 23:19:29 +02:00
|
|
|
elif (
|
|
|
|
taaf_bbox[0] <= gps_lat <= taaf_bbox[2]
|
|
|
|
and taaf_bbox[1] <= gps_lon <= taaf_bbox[3]
|
|
|
|
):
|
2024-07-24 19:13:42 +02:00
|
|
|
return "Terres australes et antarctiques françaises"
|
2024-10-25 23:19:29 +02:00
|
|
|
elif (
|
|
|
|
guyane_bbox[0] <= gps_lat <= guyane_bbox[2]
|
|
|
|
and guyane_bbox[1] <= gps_lon <= guyane_bbox[3]
|
|
|
|
):
|
2024-07-24 19:13:42 +02:00
|
|
|
return "Guyane française"
|
2024-10-25 23:19:29 +02:00
|
|
|
elif (
|
|
|
|
reunion_bbox[0] <= gps_lat <= reunion_bbox[2]
|
|
|
|
and reunion_bbox[1] <= gps_lon <= reunion_bbox[3]
|
|
|
|
):
|
2024-07-24 19:13:42 +02:00
|
|
|
return "La Réunion"
|
2024-10-25 23:19:29 +02:00
|
|
|
elif wf_bbox[0] <= gps_lat <= wf_bbox[2] and wf_bbox[1] <= gps_lon <= wf_bbox[3]:
|
2024-07-24 19:13:42 +02:00
|
|
|
return "Wallis-et-Futuna"
|
2024-10-25 23:19:29 +02:00
|
|
|
elif (
|
|
|
|
stm_sbh_bbox[0] <= gps_lat <= stm_sbh_bbox[2]
|
|
|
|
and stm_sbh_bbox[1] <= gps_lon <= stm_sbh_bbox[3]
|
|
|
|
):
|
2024-07-24 19:13:42 +02:00
|
|
|
return "Saint-Martin et Saint-Barthélemy"
|
2024-10-25 23:19:29 +02:00
|
|
|
elif (
|
|
|
|
spm_bbox[0] <= gps_lat <= spm_bbox[2] and spm_bbox[1] <= gps_lon <= spm_bbox[3]
|
|
|
|
):
|
2024-07-24 19:13:42 +02:00
|
|
|
return "Saint-Pierre-et-Miquelon"
|
2024-10-25 23:19:29 +02:00
|
|
|
elif (
|
|
|
|
mayotte_bbox[0] <= gps_lat <= mayotte_bbox[2]
|
|
|
|
and mayotte_bbox[1] <= gps_lon <= mayotte_bbox[3]
|
|
|
|
):
|
2024-07-24 19:13:42 +02:00
|
|
|
return "Mayotte"
|
2024-10-25 23:19:29 +02:00
|
|
|
elif (
|
|
|
|
martinique_bbox[0] <= gps_lat <= martinique_bbox[2]
|
|
|
|
and martinique_bbox[1] <= gps_lon <= martinique_bbox[3]
|
|
|
|
):
|
2024-07-24 19:13:42 +02:00
|
|
|
return "Martinique"
|
2024-10-25 23:19:29 +02:00
|
|
|
elif (
|
|
|
|
guadeloupe_bbox[0] <= gps_lat <= guadeloupe_bbox[2]
|
|
|
|
and guadeloupe_bbox[1] <= gps_lon <= guadeloupe_bbox[3]
|
|
|
|
):
|
2024-07-24 19:13:42 +02:00
|
|
|
return "Guadeloupe"
|
|
|
|
|
2024-10-25 23:19:29 +02:00
|
|
|
elif pf_bbox[0] <= gps_lat <= pf_bbox[2] and pf_bbox[1] <= gps_lon <= pf_bbox[3]:
|
2024-07-24 19:13:42 +02:00
|
|
|
return "Polynésie française"
|
2024-10-25 23:19:29 +02:00
|
|
|
elif nc_bbox[0] <= gps_lat <= nc_bbox[2] and nc_bbox[1] <= gps_lon <= nc_bbox[3]:
|
2024-07-24 19:13:42 +02:00
|
|
|
return "Nouvelle-Calédonie"
|
|
|
|
else:
|
2024-10-25 23:19:29 +02:00
|
|
|
return None # "Hors de France"
|
2024-07-24 19:13:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_gps_info(filepath):
|
2024-10-25 23:19:29 +02:00
|
|
|
with open(filepath, "rb") as f:
|
2024-07-24 19:13:42 +02:00
|
|
|
tags = exifread.process_file(f)
|
|
|
|
gps_info = {}
|
|
|
|
|
|
|
|
# Recherche les informations GPS dans les informations EXIF
|
|
|
|
|
|
|
|
# print("clés exif ", tags.keys())
|
|
|
|
for tag in tags.keys():
|
2024-10-25 23:19:29 +02:00
|
|
|
if tag.startswith("GPS"):
|
2024-07-24 19:13:42 +02:00
|
|
|
gps_info[tag] = tags[tag]
|
|
|
|
|
|
|
|
# Extraction des informations de latitude et de longitude
|
2024-10-25 23:19:29 +02:00
|
|
|
gps_latitude = convert_rational_to_float(gps_info.get("GPS GPSLatitude"))
|
|
|
|
gps_longitude = convert_rational_to_float(gps_info.get("GPS GPSLongitude"))
|
2024-07-24 19:13:42 +02:00
|
|
|
|
|
|
|
if gps_latitude and gps_longitude:
|
|
|
|
return gps_latitude, gps_longitude
|
|
|
|
else:
|
|
|
|
return None, None
|
|
|
|
|
|
|
|
|
|
|
|
def convert_rational_to_float(rational):
|
|
|
|
return float(rational.values[0].num) / float(rational.values[0].den)
|
|
|
|
|
|
|
|
|
2024-10-25 23:19:29 +02:00
|
|
|
if __name__ == "__main__":
|
2024-07-24 19:13:42 +02:00
|
|
|
parser = argparse.ArgumentParser()
|
2024-10-25 23:19:29 +02:00
|
|
|
parser.add_argument(
|
|
|
|
"--source_dir",
|
|
|
|
default="/home/cipherbliss/Téléchargements/FIBRELAND/TEST_IN_FR/",
|
|
|
|
help="Chemin du répertoire source",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--destination_dir",
|
|
|
|
default="/home/cipherbliss/Téléchargements/FIBRELAND/IN_FRANCE/",
|
|
|
|
help="Chemin du répertoire destination",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--sequence_folder",
|
|
|
|
default="principale_sequence",
|
|
|
|
help="Nom du dossier de séquence",
|
|
|
|
)
|
2024-07-24 19:13:42 +02:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
# Parcourt tous les fichiers dans le répertoire source et ses sous-répertoires
|
|
|
|
for root, dirs, files in os.walk(args.source_dir):
|
|
|
|
for filename in files:
|
|
|
|
# Vérifie si le fichier est une image
|
2024-10-25 23:19:29 +02:00
|
|
|
if filename.lower().endswith(
|
|
|
|
(".png", ".jpg", ".jpeg", ".gif", ".bmp", ".tif")
|
|
|
|
):
|
2024-07-24 19:13:42 +02:00
|
|
|
filepath = os.path.join(root, filename)
|
|
|
|
move_file_if_in_france(filepath, sequence_folder)
|
|
|
|
|
2024-10-25 23:19:29 +02:00
|
|
|
print(
|
|
|
|
"fichiers se situant en france déplacés: ",
|
|
|
|
count_files_moved,
|
|
|
|
" / ",
|
|
|
|
count_files_all,
|
|
|
|
)
|