forked from Olav63/outils_OSM
déplacement des méthodes de requête dans la classe Save renommée en Utils
This commit is contained in:
parent
85eb60333a
commit
16ed9aa07b
@ -1,12 +1,23 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import json
|
||||
import requests
|
||||
from pyexcel_ods3 import save_data
|
||||
from collections import OrderedDict
|
||||
from osm_vc63 import errors
|
||||
|
||||
|
||||
class Save:
|
||||
def as_ods(self, fields, data, dossier, nom_req):
|
||||
class Utils:
|
||||
overpass_url: str
|
||||
geo_api_url: str
|
||||
dossier_sauvegarde: str
|
||||
|
||||
def __init__(self, overpass_url, geo_api_url, dossier_sauvegarde):
|
||||
self.overpass_url = overpass_url
|
||||
self.geo_api_url = geo_api_url
|
||||
self.dossier_sauvegarde = dossier_sauvegarde
|
||||
|
||||
def as_ods(self, fields, data, nom_req):
|
||||
"""Sauvegarde de data dans un classeur ods"""
|
||||
|
||||
ODSdataSheet = OrderedDict()
|
||||
@ -34,14 +45,14 @@ class Save:
|
||||
|
||||
ODSdataSheet.update({"resultats": ODSdata})
|
||||
|
||||
save_data(dossier + nom_req + ".ods", ODSdataSheet)
|
||||
save_data(self.dossier_sauvegarde + nom_req + ".ods", ODSdataSheet)
|
||||
|
||||
print("Sauvegarde résultats format ODS pour " + nom_req)
|
||||
|
||||
def as_json(self, export_json, dossier, nom_req):
|
||||
def as_json(self, export_json, nom_req):
|
||||
"""Enregistrement du JSON"""
|
||||
|
||||
jsonFile = open(dossier + nom_req + ".json", "w")
|
||||
jsonFile = open(self.dossier_sauvegarde + nom_req + ".json", "w")
|
||||
jsonFile.write(json.dumps(export_json))
|
||||
jsonFile.close()
|
||||
|
||||
@ -88,3 +99,39 @@ class Save:
|
||||
index_line = index_line + 1
|
||||
|
||||
return export_json
|
||||
|
||||
def run_overpass_query(self, critere, aire_de_recherche):
|
||||
"""Envoie la requête Overpass et retourne la réponse JSON."""
|
||||
|
||||
overpass_query = (
|
||||
"""[out:json];
|
||||
(
|
||||
"""
|
||||
+ critere
|
||||
+ """
|
||||
);
|
||||
out center;
|
||||
"""
|
||||
)
|
||||
overpass_query = overpass_query.replace("aire_de_recherche", aire_de_recherche)
|
||||
|
||||
print("Execution requete overpass : \n" + overpass_query)
|
||||
response = requests.get(self.overpass_url, params={"data": overpass_query})
|
||||
|
||||
if response.status_code != 200:
|
||||
raise errors.Overpass_error(response.status_code)
|
||||
|
||||
return response.json()
|
||||
|
||||
def run_reverse_geocoding(self, lat, lon):
|
||||
"""Retourne une adresse JSON à partir d'une position GPS."""
|
||||
|
||||
url = self.geo_api_url + "/reverse/"
|
||||
|
||||
response = requests.get(url, params={"lon": str(lon), "lat": str(lat)})
|
||||
|
||||
if response.status_code != 200:
|
||||
raise errors.Geo_api_error(response.status_code)
|
||||
|
||||
return response.json()
|
||||
|
@ -23,7 +23,7 @@ from collections import OrderedDict
|
||||
import os
|
||||
from osm_vc63 import errors
|
||||
from osm_vc63 import requetes
|
||||
from osm_vc63.save import Save
|
||||
from osm_vc63.utils import Utils
|
||||
|
||||
overpass_url="http://overpass-api.de/api/interpreter"
|
||||
geo_api_url = "https://api-adresse.data.gouv.fr"
|
||||
@ -72,42 +72,13 @@ trad_bicycle_parking = {
|
||||
|
||||
|
||||
|
||||
def run_overpass_query(critere, aire_de_recherche) :
|
||||
"""Envoie la requête Overpass et retourne la réponse JSON."""
|
||||
|
||||
overpass_query = """[out:json];
|
||||
(
|
||||
"""+critere+"""
|
||||
);
|
||||
out center;
|
||||
"""
|
||||
overpass_query = overpass_query.replace("aire_de_recherche", aire_de_recherche)
|
||||
|
||||
print("Execution requete overpass : \n" + overpass_query)
|
||||
response = requests.get(overpass_url, params={'data': overpass_query})
|
||||
|
||||
if (response.status_code != 200) :
|
||||
raise errors.Overpass_error(response.status_code)
|
||||
|
||||
return (response.json())
|
||||
|
||||
|
||||
def run_reverse_geocoding(lat, lon) :
|
||||
"""Retourne une adresse JSON à partir d'une position GPS."""
|
||||
|
||||
url = geo_api_url + "/reverse/"
|
||||
|
||||
response = requests.get(url, params={'lon' : str(lon), 'lat' : str(lat)})
|
||||
|
||||
if (response.status_code != 200) :
|
||||
raise errors.Geo_api_error(response.status_code)
|
||||
|
||||
return (response.json())
|
||||
|
||||
|
||||
def executer_requete_et_exporter_resultats(nom_req, critere, aire_de_recherche, overpass_query_fields) :
|
||||
|
||||
data = run_overpass_query(critere, aire_de_recherche)
|
||||
utils = Utils(overpass_url, geo_api_url, dossier_sauvegarde)
|
||||
data = utils.run_overpass_query(critere, aire_de_recherche)
|
||||
|
||||
nb_elements = len(data["elements"])
|
||||
|
||||
@ -161,13 +132,13 @@ def executer_requete_et_exporter_resultats(nom_req, critere, aire_de_recherche,
|
||||
print()
|
||||
"""
|
||||
|
||||
export_json = Save().nettoyage_json_pour_umap(data, overpass_query_fields)
|
||||
export_json = utils.nettoyage_json_pour_umap(data, overpass_query_fields)
|
||||
|
||||
# Sauvegarde
|
||||
os.makedirs(dossier_sauvegarde, exist_ok = True)
|
||||
|
||||
Save().as_json(export_json, dossier_sauvegarde, nom_req)
|
||||
Save().as_ods(overpass_query_fields, data, dossier_sauvegarde, nom_req)
|
||||
utils.as_json(export_json, nom_req)
|
||||
utils.as_ods(overpass_query_fields, data, nom_req)
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user