File size: 3,229 Bytes
e05937f
 
38af3d3
e05937f
319e1da
f0b11dd
fe52f62
 
2914aec
e05937f
38af3d3
 
e05937f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38af3d3
e05937f
 
 
 
 
 
 
 
 
 
 
38af3d3
e05937f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38af3d3
 
 
 
e05937f
fe52f62
38af3d3
fe52f62
 
 
 
f0b11dd
 
 
38af3d3
 
 
2914aec
 
 
fe52f62
 
 
f0b11dd
38af3d3
 
 
 
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import folium
import requests
import gradio as gr


from compute_yield import plot_yield
from summary_test import get_summaries

from visualize.visualize import get_plots


# code from 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


# code from https://huggingface.co/spaces/gaia-mistral/pest-livestock-information
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


# code from https://huggingface.co/spaces/gaia-mistral/pest-livestock-information
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 go_to_page_2():
    return gr.Column(visible=False), gr.Column(visible=True)


def launch_simulation(lat, lon, address, culture):
    current_situation_summary, agripv_summary = get_summaries()
    page1, page_2 = go_to_page_2()
    plot_1, plot_2, plot_3, plot_4, plot_5 = get_plots()
    current_plot_yields = plot_yield(
        latitude=lat, longitude=lon, culture=culture, shading_coef=0
    )
    shaded_plot_yields = plot_yield(
        latitude=lat, longitude=lon, culture=culture, shading_coef=0.2
    )
    return (
        current_situation_summary,
        agripv_summary,
        plot_1,
        plot_2,
        plot_3,
        plot_4,
        plot_5,
        current_plot_yields,
        shaded_plot_yields,
        page1,
        page_2,
        gr.Button(visible=True),
    )