méthodes de sauvegarde déplacées dans une classe
This commit is contained in:
parent
27aea631bf
commit
bcb9b7e9b2
48
osm_vc63/save.py
Normal file
48
osm_vc63/save.py
Normal file
@ -0,0 +1,48 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import json
|
||||
from pyexcel_ods3 import save_data
|
||||
from collections import OrderedDict
|
||||
|
||||
|
||||
class Save:
|
||||
def as_ods(self, fields, data, dossier, nom_req):
|
||||
"""Sauvegarde de data dans un classeur ods"""
|
||||
|
||||
ODSdataSheet = OrderedDict()
|
||||
ODSdata = []
|
||||
ODSdata.append(fields.keys())
|
||||
index_line = 2
|
||||
|
||||
for element in data["elements"]:
|
||||
line = []
|
||||
index_col = 0
|
||||
|
||||
for field in fields.keys():
|
||||
if field in element["tags"]:
|
||||
if field == "capacity":
|
||||
val = element["tags"][field]
|
||||
line.append(int(val) if val.isdigit() else val)
|
||||
else:
|
||||
line.append(element["tags"][field])
|
||||
else:
|
||||
line.append("")
|
||||
index_col = index_col + 1
|
||||
|
||||
ODSdata.append(line)
|
||||
index_line = index_line + 1
|
||||
|
||||
ODSdataSheet.update({"resultats": ODSdata})
|
||||
|
||||
save_data(dossier + nom_req + ".ods", ODSdataSheet)
|
||||
|
||||
print("Sauvegarde résultats format ODS pour " + nom_req)
|
||||
|
||||
def as_json(self, export_json, dossier, nom_req):
|
||||
"""Enregistrement du JSON"""
|
||||
|
||||
jsonFile = open(dossier + nom_req + ".json", "w")
|
||||
jsonFile.write(json.dumps(export_json))
|
||||
jsonFile.close()
|
||||
|
||||
print("Sauvegarde résultat format JSON/OSM " + nom_req)
|
@ -23,6 +23,7 @@ from collections import OrderedDict
|
||||
import os
|
||||
from osm_vc63 import errors
|
||||
from osm_vc63 import requetes
|
||||
from osm_vc63.save import Save
|
||||
|
||||
overpass_url="http://overpass-api.de/api/interpreter"
|
||||
geo_api_url = "https://api-adresse.data.gouv.fr"
|
||||
@ -69,47 +70,6 @@ trad_bicycle_parking = {
|
||||
"handlebar_holder": "Accroche-guidons"}
|
||||
|
||||
|
||||
def sauvegarde_ods(fields, data, dossier, nom_req):
|
||||
"""Sauvegarde de la requête"""
|
||||
|
||||
ODSdataSheet = OrderedDict()
|
||||
ODSdata = []
|
||||
ODSdata.append(fields.keys())
|
||||
index_line = 2
|
||||
|
||||
for element in data["elements"]:
|
||||
line = []
|
||||
index_col = 0
|
||||
|
||||
for field in fields.keys():
|
||||
if field in element["tags"]:
|
||||
if field == "capacity":
|
||||
val = element["tags"][field]
|
||||
line.append(int(val) if val.isdigit() else val)
|
||||
else:
|
||||
line.append(element["tags"][field])
|
||||
else:
|
||||
line.append("")
|
||||
index_col = index_col + 1
|
||||
|
||||
ODSdata.append(line)
|
||||
index_line = index_line + 1
|
||||
|
||||
ODSdataSheet.update({"resultats": ODSdata})
|
||||
|
||||
save_data(dossier + nom_req + ".ods", ODSdataSheet)
|
||||
|
||||
print("Sauvegarde résultats format ODS pour " + nom_req)
|
||||
|
||||
|
||||
def sauvegarde_json(export_json, dossier, nom_req):
|
||||
jsonFile = open(dossier + nom_req + ".json", "w")
|
||||
jsonFile.write(json.dumps(export_json))
|
||||
jsonFile.close()
|
||||
|
||||
print("Sauvegarde résultat format JSON/OSM " + nom_req)
|
||||
|
||||
|
||||
def nettoyage_json_pour_umap(data, overpass_query_fields):
|
||||
export_json = {"version": data["version"],
|
||||
"generator" : data["generator"] + " and ETALAB API",
|
||||
@ -241,8 +201,8 @@ def executer_requete_et_exporter_resultats(nom_req, critere, aire_de_recherche,
|
||||
# Sauvegarde
|
||||
os.makedirs(dossier_sauvegarde, exist_ok = True)
|
||||
|
||||
sauvegarde_json(export_json, dossier_sauvegarde, nom_req)
|
||||
sauvegarde_ods(overpass_query_fields, data, dossier_sauvegarde, nom_req)
|
||||
Save().as_json(export_json, dossier_sauvegarde, nom_req)
|
||||
Save().as_ods(overpass_query_fields, data, dossier_sauvegarde, nom_req)
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user