Spaces:
Sleeping
Sleeping
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() | |