2021-09-26 12:52:37 +02:00
|
|
|
|
#!/usr/bin/env python3
|
2021-10-10 16:50:27 +02:00
|
|
|
|
"""
|
|
|
|
|
Module principal :
|
|
|
|
|
- récupération de données par appel à Overpass
|
|
|
|
|
- géocodage inverse
|
|
|
|
|
- export des données en JSON pour utilisation avec umap
|
|
|
|
|
- sauvegarde des données en ods
|
|
|
|
|
"""
|
2021-09-26 12:52:37 +02:00
|
|
|
|
|
|
|
|
|
# inspiration :
|
|
|
|
|
# https://towardsdatascience.com/loading-data-from-openstreetmap-with-python-and-the-overpass-api-513882a27fd0
|
|
|
|
|
# https://geo.api.gouv.fr/adresse
|
|
|
|
|
# https://wiki.cartocite.fr/doku.php?id=umap:10_-_je_valorise_les_donnees_openstreetmap_avec_umap
|
|
|
|
|
# https://sites-formations.univ-rennes2.fr/mastersigat/Cours/Intro_Overpass.pdf
|
|
|
|
|
|
2021-10-10 18:54:19 +02:00
|
|
|
|
# usage des tags :
|
|
|
|
|
# https://taginfo.openstreetmap.org/tags/?key=amenity&value=bicycle_parking#combinations
|
2021-09-26 12:52:37 +02:00
|
|
|
|
|
2021-10-10 18:54:19 +02:00
|
|
|
|
# exemple URL données pour umap :
|
|
|
|
|
# https://www.velocite63.fr/velocite63/OSM/stationnements_velos_publics.json
|
2021-09-26 12:52:37 +02:00
|
|
|
|
# penser à cocher "proxy" dans la rubrique "données distantes" du calque
|
|
|
|
|
|
|
|
|
|
# export ODS :
|
|
|
|
|
# https://pythonhosted.org/pyexcel-ods/
|
|
|
|
|
# pip3 install pyexcel-ods3
|
|
|
|
|
|
|
|
|
|
import time
|
2021-10-03 15:37:11 +02:00
|
|
|
|
import os
|
2021-10-03 15:42:12 +02:00
|
|
|
|
from osm_vc63 import errors
|
2021-10-03 18:10:19 +02:00
|
|
|
|
from osm_vc63 import requetes
|
2021-10-10 10:55:15 +02:00
|
|
|
|
from osm_vc63.utils import Utils
|
2021-09-26 12:52:37 +02:00
|
|
|
|
|
2021-10-10 16:50:27 +02:00
|
|
|
|
OVERPASS_URL = "http://overpass-api.de/api/interpreter"
|
|
|
|
|
GEO_API_URL = "https://api-adresse.data.gouv.fr"
|
|
|
|
|
DOSSIER_SAUVEGARDE = "resultats/"
|
2021-09-26 12:52:37 +02:00
|
|
|
|
|
|
|
|
|
# nombre maxi de retries quand echec API
|
2021-10-10 16:50:27 +02:00
|
|
|
|
MAX_RETRY = 4
|
2021-09-26 12:52:37 +02:00
|
|
|
|
|
|
|
|
|
# delai en secondes entre les tentatives
|
2021-10-10 16:50:27 +02:00
|
|
|
|
RETRY_DELAY = 120
|
2021-09-26 12:52:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# id du département "Puy de Dôme" : 7406
|
|
|
|
|
# id Riom : 1693144
|
|
|
|
|
# id Clermont : 110866
|
|
|
|
|
# id Romagnat : 138269
|
|
|
|
|
# l'id de l'area se calcule en ajoutant 3600000000 au numéro de l'objet OSM
|
2021-10-10 18:54:19 +02:00
|
|
|
|
AIRE_DE_RECHERCHE = str(3_600_000_000 + 110_866)
|
2021-09-26 12:52:37 +02:00
|
|
|
|
|
2021-10-10 17:55:26 +02:00
|
|
|
|
# traductions des tags bicycle_parking
|
|
|
|
|
TRAD_BICYCLE_PARKING = {
|
2021-09-26 12:52:37 +02:00
|
|
|
|
"stands": "Arceaux",
|
|
|
|
|
"wall_loops": "Pince roues",
|
|
|
|
|
"rack": "Râteliers",
|
|
|
|
|
"anchors": "Ancrage",
|
|
|
|
|
"shed": "Abri collectif",
|
|
|
|
|
"bollard": "Potelet",
|
|
|
|
|
"lockers": "Abris individuels",
|
|
|
|
|
"wide_stands": "Arceaux espacés",
|
|
|
|
|
"ground_slots": "Fente dans le sol",
|
|
|
|
|
"building": "Bâtiment",
|
|
|
|
|
"informal": "Informel",
|
|
|
|
|
"wave": "Râteliers",
|
|
|
|
|
"streetpod": "Arceaux",
|
|
|
|
|
"tree": "Arbre à bicyclettes",
|
|
|
|
|
"crossbar": "Barre",
|
|
|
|
|
"rope": "Câble",
|
|
|
|
|
"two-tier": "Deux étages",
|
|
|
|
|
"floor": "Sol",
|
2021-10-10 10:56:13 +02:00
|
|
|
|
"handlebar_holder": "Accroche-guidons",
|
|
|
|
|
}
|
2021-10-09 15:10:36 +02:00
|
|
|
|
|
2021-09-26 12:52:37 +02:00
|
|
|
|
|
2021-10-10 17:55:26 +02:00
|
|
|
|
def main():
|
|
|
|
|
"""Routine principale"""
|
2021-09-26 12:52:37 +02:00
|
|
|
|
|
2021-10-10 17:55:26 +02:00
|
|
|
|
for req in requetes.REQS:
|
|
|
|
|
for nb_essai in range(MAX_RETRY): # on tente max_retry fois
|
|
|
|
|
try:
|
|
|
|
|
utils = Utils(OVERPASS_URL, GEO_API_URL, DOSSIER_SAUVEGARDE)
|
2021-09-26 12:52:37 +02:00
|
|
|
|
|
2021-10-10 18:54:19 +02:00
|
|
|
|
print(f"{75*'#'}\r\nRequête en cours : {req.nom}")
|
|
|
|
|
|
2021-10-10 17:55:26 +02:00
|
|
|
|
# appel overpass
|
|
|
|
|
data = utils.run_overpass_query(req.critere, AIRE_DE_RECHERCHE)
|
2021-10-10 19:24:18 +02:00
|
|
|
|
nb_resultats = len(data["elements"])
|
|
|
|
|
print(f"{nb_resultats} résultats")
|
2021-09-26 12:52:37 +02:00
|
|
|
|
|
2021-10-10 19:24:18 +02:00
|
|
|
|
if nb_resultats > 0:
|
|
|
|
|
# géocodage inverse
|
|
|
|
|
data = utils.geocodage(data)
|
2021-09-26 12:52:37 +02:00
|
|
|
|
|
2021-10-10 19:24:18 +02:00
|
|
|
|
# traduction
|
|
|
|
|
data = utils.traduction(
|
|
|
|
|
"bicycle_parking", TRAD_BICYCLE_PARKING, data
|
|
|
|
|
)
|
2021-09-26 12:52:37 +02:00
|
|
|
|
|
2021-10-10 19:24:18 +02:00
|
|
|
|
# Sauvegarde
|
|
|
|
|
os.makedirs(DOSSIER_SAUVEGARDE, exist_ok=True)
|
|
|
|
|
export_json = utils.nettoyage_json_pour_umap(data, req.champs)
|
2021-09-26 12:52:37 +02:00
|
|
|
|
|
2021-10-10 19:24:18 +02:00
|
|
|
|
utils.save_as_json(export_json, req.nom)
|
|
|
|
|
utils.save_as_ods(req.champs, data, req.nom)
|
2021-09-26 12:52:37 +02:00
|
|
|
|
|
2021-10-10 19:53:43 +02:00
|
|
|
|
# doucement sur overpass
|
|
|
|
|
time.sleep(30)
|
2021-10-09 14:31:01 +02:00
|
|
|
|
break
|
2021-10-10 16:31:54 +02:00
|
|
|
|
except errors.ApiError:
|
2021-10-10 10:56:13 +02:00
|
|
|
|
|
2021-10-10 16:50:27 +02:00
|
|
|
|
if nb_essai == MAX_RETRY:
|
2021-10-09 14:31:01 +02:00
|
|
|
|
print("trop d'erreurs d'API - abandon")
|
2021-09-26 12:52:37 +02:00
|
|
|
|
exit()
|
2021-10-10 10:56:13 +02:00
|
|
|
|
|
2021-10-10 16:50:27 +02:00
|
|
|
|
print("erreur API - on retente dans " + str(RETRY_DELAY) + "s")
|
2021-10-10 10:56:13 +02:00
|
|
|
|
|
2021-10-10 16:50:27 +02:00
|
|
|
|
time.sleep(RETRY_DELAY)
|
2021-10-10 10:56:13 +02:00
|
|
|
|
|
2021-10-10 19:53:43 +02:00
|
|
|
|
print("\r\n ### Terminé ###")
|
2021-09-26 12:52:37 +02:00
|
|
|
|
|
|
|
|
|
|
2021-10-09 14:31:01 +02:00
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|