forked from Olav63/outils_OSM
Merge branch '#8_Optimisation_geocodage_inverse'
This commit is contained in:
commit
7b4d4cf711
@ -25,6 +25,7 @@ import os
|
||||
import datetime
|
||||
import shutil
|
||||
import pathlib
|
||||
import csv
|
||||
from collections import OrderedDict
|
||||
import requests
|
||||
from pyexcel_ods3 import save_data
|
||||
@ -148,77 +149,74 @@ class Utils:
|
||||
|
||||
return response.json()
|
||||
|
||||
def run_reverse_geocoding(self, lat, lon):
|
||||
"""Retourne une adresse JSON à partir d'une position GPS."""
|
||||
def geocodage_csv(self, data):
|
||||
"""
|
||||
Renseigne une adresse pour chaque élément de data
|
||||
en une fois via csv
|
||||
"""
|
||||
|
||||
url = self.geo_api_url + "/reverse/"
|
||||
url = self.geo_api_url + "/reverse/csv/"
|
||||
|
||||
response = requests.get(url, params={"lon": str(lon), "lat": str(lat)})
|
||||
# création du fichier à envoyer à l'API
|
||||
with open("tmp_geocodage.csv", "w", newline="") as tmp_csv_file:
|
||||
csv_writer = csv.writer(tmp_csv_file)
|
||||
csv_writer.writerow(["lat", "lon"])
|
||||
|
||||
for element in data["elements"]:
|
||||
if element["type"] == "node":
|
||||
csv_writer.writerow([element["lat"], element["lon"]])
|
||||
else:
|
||||
csv_writer.writerow(
|
||||
[element["center"]["lat"], element["center"]["lon"]]
|
||||
)
|
||||
|
||||
# préparation et envoi de la requête
|
||||
payload = dict(
|
||||
[("data", ("tmp_geocodage.csv", open("tmp_geocodage.csv", "rb").read()))]
|
||||
)
|
||||
response = requests.post(url, files=payload)
|
||||
|
||||
# nettoyage
|
||||
os.remove("tmp_geocodage.csv")
|
||||
|
||||
if response.status_code != 200:
|
||||
raise errors.GeoApiError(response.status_code)
|
||||
|
||||
return response.json()
|
||||
|
||||
def geocodage(self, data):
|
||||
"""Renseigne une adresse pour chaque élément de data"""
|
||||
|
||||
# affectation des addresses
|
||||
for element in data["elements"]:
|
||||
for row in csv.DictReader(response.text.splitlines()):
|
||||
if element["type"] == "node":
|
||||
lat_ok = row["lat"] == str(element["lat"])
|
||||
lon_ok = row["lon"] == str(element["lon"])
|
||||
else:
|
||||
lat_ok = row["lat"] == str(element["center"]["lat"])
|
||||
lon_ok = row["lon"] == str(element["center"]["lon"])
|
||||
|
||||
if element["type"] == "node":
|
||||
rev_geocode = self.run_reverse_geocoding(element["lat"], element["lon"])
|
||||
else:
|
||||
rev_geocode = self.run_reverse_geocoding(
|
||||
element["center"]["lat"], element["center"]["lon"]
|
||||
)
|
||||
|
||||
api_adresse = rev_geocode["features"][0]
|
||||
|
||||
element["tags"]["api_adresse:geometry:coordinates:lon"] = api_adresse[
|
||||
"geometry"
|
||||
]["coordinates"][0]
|
||||
element["tags"]["api_adresse:geometry:coordinates:lat"] = api_adresse[
|
||||
"geometry"
|
||||
]["coordinates"][1]
|
||||
|
||||
element["tags"]["api_adresse:properties:label"] = api_adresse["properties"][
|
||||
"label"
|
||||
]
|
||||
element["tags"]["api_adresse:properties:score"] = api_adresse["properties"][
|
||||
"score"
|
||||
]
|
||||
|
||||
if "housenumber" in api_adresse["properties"]:
|
||||
element["tags"]["api_adresse:properties:housenumber"] = api_adresse[
|
||||
"properties"
|
||||
]["housenumber"]
|
||||
|
||||
element["tags"]["api_adresse:properties:type"] = api_adresse["properties"][
|
||||
"type"
|
||||
]
|
||||
|
||||
element["tags"]["api_adresse:properties:name"] = api_adresse["properties"][
|
||||
"name"
|
||||
]
|
||||
element["tags"]["api_adresse:properties:postcode"] = api_adresse[
|
||||
"properties"
|
||||
]["postcode"]
|
||||
element["tags"]["api_adresse:properties:citycode"] = api_adresse[
|
||||
"properties"
|
||||
]["citycode"]
|
||||
element["tags"]["api_adresse:properties:city"] = api_adresse["properties"][
|
||||
"city"
|
||||
]
|
||||
|
||||
if "street" in api_adresse["properties"]:
|
||||
element["tags"]["api_adresse:properties:street"] = api_adresse[
|
||||
"properties"
|
||||
]["street"]
|
||||
|
||||
element["tags"]["api_adresse:properties:attribution"] = rev_geocode[
|
||||
"attribution"
|
||||
]
|
||||
element["tags"]["api_adresse:properties:licence"] = rev_geocode["licence"]
|
||||
if lat_ok and lon_ok:
|
||||
element["tags"]["api_adresse:geometry:coordinates:lon"] = row[
|
||||
"result_longitude"
|
||||
]
|
||||
element["tags"]["api_adresse:geometry:coordinates:lat"] = row[
|
||||
"result_latitude"
|
||||
]
|
||||
element["tags"]["api_adresse:properties:label"] = row[
|
||||
"result_label"
|
||||
]
|
||||
element["tags"]["api_adresse:properties:housenumber"] = row[
|
||||
"result_housenumber"
|
||||
]
|
||||
element["tags"]["api_adresse:properties:type"] = row["result_type"]
|
||||
element["tags"]["api_adresse:properties:name"] = row["result_name"]
|
||||
element["tags"]["api_adresse:properties:postcode"] = row[
|
||||
"result_postcode"
|
||||
]
|
||||
element["tags"]["api_adresse:properties:citycode"] = row[
|
||||
"result_citycode"
|
||||
]
|
||||
element["tags"]["api_adresse:properties:city"] = row["result_city"]
|
||||
element["tags"]["api_adresse:properties:street"] = row[
|
||||
"result_street"
|
||||
]
|
||||
|
||||
logging.info("Géocodage inversé terminé")
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user