Spaces:
Paused
Paused
File size: 1,844 Bytes
03df0fa |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
import requests
import re
def get_geolocation(adresse, latitude, longitude):
"""Return latitude, longitude & code INSEE from an adress. Latitude & longitude only if they are not specified in the function.
Args:
adresse (str): Adress or city for example
Returns:
Tuple(float, float, str): latitude, longitude & code INSEE
"""
if latitude is not None and longitude is not None:
url = f"https://data.geopf.fr/geocodage/reverse?lon={longitude}&lat={latitude}&index=parcel"
else:
url = f"https://data.geopf.fr/geocodage/search?q={adresse}"
response = requests.get(url)
if response.status_code == 200:
data = response.json()["features"][0]
if 'parcel' in url:
properties = data["properties"]
code_insee = properties["departmentcode"] + \
properties["municipalitycode"]
else:
coordinates = data["geometry"]["coordinates"]
latitude, longitude = coordinates[1], coordinates[0]
code_insee = data["properties"]["citycode"]
return latitude, longitude, code_insee
return None, None, None
def get_risques(code_insee):
"""Get list of risks from a 'code INSEE'
Args:
code_insee (str): Code INSEE
Returns:
List[str]: List of risks
"""
requete = f"https://georisques.gouv.fr/api/v1/gaspar/risques?code_insee={code_insee}"
response = requests.get(requete)
if response.status_code == 200:
risques = eval(response.content.decode().replace("null", "-1"))
if risques["results"] > 0:
risques = risques["data"]
risques = [r["libelle_risque_long"]
for r in risques[0]["risques_detail"]]
else:
risques = []
else:
risques = None
return risques
|