#!/usr/bin/env python3 """ 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 """ # 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 # usage des tags : https://taginfo.openstreetmap.org/tags/?key=amenity&value=bicycle_parking#combinations # exemple URL données pour umap : https://www.velocite63.fr/velocite63/OSM/stationnements_velos_publics.json # penser à cocher "proxy" dans la rubrique "données distantes" du calque # export ODS : # https://pythonhosted.org/pyexcel-ods/ # pip3 install pyexcel-ods3 import time import os from osm_vc63 import errors from osm_vc63 import requetes from osm_vc63.utils import Utils OVERPASS_URL = "http://overpass-api.de/api/interpreter" GEO_API_URL = "https://api-adresse.data.gouv.fr" DOSSIER_SAUVEGARDE = "resultats/" # nombre maxi de retries quand echec API MAX_RETRY = 4 # delai en secondes entre les tentatives RETRY_DELAY = 120 # 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 AIRE_DE_RECHERCHE = str(3600000000 + 110866) # traductions des tags bicycle_parking TRAD_BICYCLE_PARKING = { "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", "handlebar_holder": "Accroche-guidons", } def main(): """Routine principale""" 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) # appel overpass data = utils.run_overpass_query(req.critere, AIRE_DE_RECHERCHE) # géocodage inverse data = utils.geocodage(data) # traduction data = utils.traduction("bicycle_parking", TRAD_BICYCLE_PARKING, data) # Sauvegarde os.makedirs(DOSSIER_SAUVEGARDE, exist_ok=True) export_json = utils.nettoyage_json_pour_umap(data, req.champs) utils.save_as_json(export_json, req.nom) utils.save_as_ods(req.champs, data, req.nom) break except errors.ApiError: if nb_essai == MAX_RETRY: print("trop d'erreurs d'API - abandon") exit() print("erreur API - on retente dans " + str(RETRY_DELAY) + "s") time.sleep(RETRY_DELAY) print("Fini") if __name__ == "__main__": main()