gaia / func_utils.py
AliceTt's picture
added localisation and culture choices in front
e05937f
raw
history blame
2.2 kB
import folium
import requests
# https://huggingface.co/spaces/gaia-mistral/pest-livestock-information
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 on_init(lat, lon, address):
map_html, lat, lon = show_map(lat, lon, address)
return lat, lon, map_html
def on_delete(lat, lon, address):
address = lat = lon = None
map_html, lat, lon = show_map(lat, lon, address)
return lat, lon, address, map_html
def show_map(lat, lon, address):
if address:
lat_tmp, lon_tmp, code_insee = get_geolocation(address, None, None)
if lat_tmp or lon_tmp:
lat, lon = lat_tmp, lon_tmp
else:
return "Adress not found. Please enter a valid address", ""
lat = lat or 48.832408
lon = lon or 2.28619
if lat and lon:
lat_tmp, lon_tmp, code_insee = get_geolocation(None, lat, lon)
location_map = folium.Map(location=[lat, lon], zoom_start=14)
folium.Marker([lat, lon]).add_to(location_map)
map_html = location_map._repr_html_()
return map_html, lat, lon
def launch_simulation(lat, lon, address, culture):
# Do something
pass