File size: 6,781 Bytes
21b4654
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96ffd58
21b4654
2a5e8d1
 
21b4654
 
 
 
 
 
95f57aa
52432c1
 
 
21b4654
 
 
 
 
 
52432c1
21b4654
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ab6037
21b4654
5ab6037
21b4654
 
 
5ab6037
52432c1
 
5ab6037
21b4654
 
 
d097c03
21b4654
 
 
 
 
 
 
52432c1
 
21b4654
 
5ab6037
21b4654
 
 
 
 
 
 
 
 
 
 
52432c1
 
 
 
21b4654
 
 
5ab6037
 
21b4654
 
 
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import numpy as np
import streamlit as st
import requests
import pydeck as pdk
import pandas as pd
import geopandas as gpd
import plotly.express as px
import folium
import webbrowser
from shapely.geometry import Point
from folium import plugins
from streamlit_folium import st_folium

def load_polygon(filepath):
    return gpd.read_file(filepath)
path=''
# Polygon1 = load_polygon(path + 'risk_zones.shp')
# Polygon2 = load_polygon(path + 'n_inondable_01_01for_s.shp')
# Polygon3 = load_polygon(path + 'ZUS_FRM_BDA09_L93.shp')
#

# Initialize polygons if not already in session state
if 'polygons' not in st.session_state:
    st.session_state.polygons = {
        "Polygon1": load_polygon(path+'risk_zones.shp'),
        "Polygon2": load_polygon(path+'n_inondable_01_01for_s.shp'),
        "Polygon3": load_polygon(path+'ZUS_FRM_BDA09_L93.shp'),
        "Polygon4": load_polygon(path+'ZFU_FRM_Scan25_L93.shp')
               
    }

if 'polygons'  in st.session_state:
    st.session_state.polygons["Polygon1"]['geometry'] = st.session_state.polygons["Polygon1"]['geometry'].to_crs(epsg=4326)
    st.session_state.polygons["Polygon2"]['geometry'] = st.session_state.polygons["Polygon2"]['geometry'].to_crs(epsg=4326)
    st.session_state.polygons["Polygon3"]['geometry'] = st.session_state.polygons["Polygon3"]['geometry'].to_crs(epsg=4326)
    st.session_state.polygons["Polygon4"]['geometry'] = st.session_state.polygons["Polygon4"]['geometry'].to_crs(epsg=4326)

def create_geodataframe(longitude, latitude):
    geometry = [Point(longitude, latitude)]
    gdf = gpd.GeoDataFrame(geometry=geometry, crs="EPSG:4326")
    return gdf

def get_address_suggestions(query):
    if not query:
        return []

    url = "https://api-adresse.data.gouv.fr/search/"
    params = {'q': query, 'autocomplete': 1, 'limit': 5}
    response = requests.get(url, params=params)

    if response.status_code == 200:
        data = response.json()
        suggestions = [{'label': feature['properties']['label'], 'coordinates': feature['geometry']['coordinates']}
                       for feature in data['features']]
        return suggestions
    else:
        return []


# Function to create a map
def create_map(latitude, longitude):
    map_data = pd.DataFrame({
        'lat': [latitude],
        'lon': [longitude]
    })

    st.pydeck_chart(pdk.Deck(
        map_style='mapbox://styles/mapbox/light-v9',
        initial_view_state=pdk.ViewState(
            latitude=latitude,
            longitude=longitude,
            zoom=11,
            pitch=50,
        ),
        layers=[
            pdk.Layer(
                'ScatterplotLayer',
                data=map_data,
                get_position='[lon, lat]',
                get_color='[200, 30, 0, 160]',
                get_radius=200,
            ),
        ],
    ))


# Streamlit app layout
def main():
    st.title("Immopolis Adress validation APP")

    # Session state to store the current suggestions
    if 'suggestions' not in st.session_state:
        st.session_state.suggestions = []

    # Text input for address with on_change callback
    query = st.text_input("Enter your address", "", key="query")

    # Update suggestions when query changes
    st.session_state.suggestions = get_address_suggestions(query)

    # Display autocomplete suggestions
    if query:
        selected_suggestion = st.selectbox("Did you mean:", [s['label'] for s in st.session_state.suggestions], index=0,
                                           key="selected_suggestion")
    else:
        selected_suggestion = ""


    if selected_suggestion:
        selected_data = next((item for item in st.session_state.suggestions if item['label'] == selected_suggestion),
                             None)
        if selected_data and 'coordinates' in selected_data:
            longitude, latitude = selected_data['coordinates']
            st.write(f"Latitude: {latitude}, Longitude: {longitude}")
            #m = folium.Map(location=[longitude, latitude], zoom_start=6)
            gdf = create_geodataframe(longitude, latitude)
            gdf['geometry'] = gdf['geometry'].to_crs(epsg=4326)
            st.write(gdf)

            polygon_name1=gpd.sjoin(gdf, st.session_state.polygons["Polygon1"], how="left", predicate="within")['index_right']

            polygon_name2=gpd.sjoin(gdf, st.session_state.polygons["Polygon2"], how="left", predicate="within")['index_right']

            polygon_name3=gpd.sjoin(gdf, st.session_state.polygons["Polygon3"], how="left", predicate="within")['index_right']

            polygon_name4=gpd.sjoin(gdf, st.session_state.polygons["Polygon4"], how="left", predicate="within")['index_right']


            m=folium.Map([gdf['geometry'].y, gdf['geometry'].x],zoom_start = 15)
            folium.CircleMarker([gdf['geometry'].y, gdf['geometry'].x], radius=1, color='red').add_to(m)
            #folium.GeoJson(st.session_state.polygons["Polygon2"], color='blue').add_to(m)
            #folium.GeoJson(st.session_state.polygons["Polygon3"], color='orange').add_to(m)

            if not np.isnan(polygon_name1[0]):
                folium.GeoJson(st.session_state.polygons["Polygon1"][st.session_state.polygons["Polygon1"].index==polygon_name1[0]],color='yellow').add_to(m)
            if not np.isnan(polygon_name2[0]):
                folium.GeoJson(st.session_state.polygons["Polygon2"][st.session_state.polygons["Polygon2"].index==polygon_name2[0]],color='blue').add_to(m)
            if not np.isnan(polygon_name3[0]):
                folium.GeoJson(st.session_state.polygons["Polygon3"][st.session_state.polygons["Polygon3"].index==polygon_name3[0]],color='orange').add_to(m)
            if not np.isnan(polygon_name4[0]):
                folium.GeoJson(st.session_state.polygons["Polygon4"][st.session_state.polygons["Polygon4"].index==polygon_name4[0]],color='grey').add_to(m)

            point = Point(longitude, latitude)
            


            if not np.isnan(polygon_name1[0]):
                st.markdown(f"Address is High risk zone", unsafe_allow_html=True)

            if not np.isnan(polygon_name2[0]):
                st.markdown(f"Address is in flooding area", unsafe_allow_html=True)

            if not np.isnan(polygon_name3[0]):
                st.markdown(f"Address is in sensitive area", unsafe_allow_html=True)

            if not np.isnan(polygon_name4[0]):
                st.markdown(f"Address is in sensitive neighborhood", unsafe_allow_html=True)

            if np.isnan(polygon_name1[0]) and np.isnan(polygon_name2[0])  and np.isnan(polygon_name3[0]) and np.isnan(polygon_name4[0]):
                st.markdown(f"Risk check OK", unsafe_allow_html=True)


            st_folium(m, width=700, height=500)

# Run the app
if __name__ == "__main__":
    main()