Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,11 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
|
|
|
| 3 |
import geopandas as gpd
|
| 4 |
import json
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
@st.cache
|
| 8 |
def load_geojson_data(file_paths):
|
| 9 |
geodata = []
|
|
@@ -12,20 +14,7 @@ def load_geojson_data(file_paths):
|
|
| 12 |
geodata.append(gdf)
|
| 13 |
return geodata
|
| 14 |
|
| 15 |
-
# Function to generate heatmap data from polygons
|
| 16 |
-
def generate_heatmap_data(gdfs):
|
| 17 |
-
heatmap_data = []
|
| 18 |
-
for gdf in gdfs:
|
| 19 |
-
for _, row in gdf.iterrows():
|
| 20 |
-
if row.geometry.type == 'Polygon':
|
| 21 |
-
x, y = row.geometry.centroid.x, row.geometry.centroid.y
|
| 22 |
-
heatmap_data.append([y, x])
|
| 23 |
-
elif row.geometry.type == 'Point':
|
| 24 |
-
x, y = row.geometry.x, row.geometry.y
|
| 25 |
-
heatmap_data.append([y, x])
|
| 26 |
-
return heatmap_data
|
| 27 |
|
| 28 |
-
# Placeholder for polygon GeoJSON
|
| 29 |
polygon_geojson = json.loads("""
|
| 30 |
{
|
| 31 |
"type": "FeatureCollection",
|
|
@@ -42,66 +31,64 @@ polygon_geojson = json.loads("""
|
|
| 42 |
}
|
| 43 |
""")
|
| 44 |
|
| 45 |
-
# Sidebar
|
| 46 |
st.sidebar.title("Geospatial Data Visualization")
|
| 47 |
timestamp = st.sidebar.select_slider("Time", options=[f"{year}-01" for year in range(2016, 2024)])
|
| 48 |
|
| 49 |
# Main title
|
| 50 |
-
st.title("Heatmap view:")
|
| 51 |
|
| 52 |
-
|
| 53 |
-
geojson_file_paths = ['
|
| 54 |
geojson_data = load_geojson_data(geojson_file_paths)
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
#
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
#
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
#
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
# Display the map
|
| 72 |
-
|
| 73 |
|
| 74 |
# Add an informational box
|
| 75 |
with st.expander("View Information"):
|
| 76 |
st.write(f"Selected Time: {timestamp}")
|
| 77 |
-
num_points =
|
| 78 |
-
total_area =
|
| 79 |
st.write(f"Number of Features: {num_points}")
|
| 80 |
st.write(f"Total Area: {total_area:.2f} sq km")
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
css_code = """
|
| 84 |
-
<style>
|
| 85 |
-
.fixed-box {
|
| 86 |
-
position: fixed;
|
| 87 |
-
top: 50px;
|
| 88 |
-
right: 50px;
|
| 89 |
-
width: 200px;
|
| 90 |
-
background-color: white;
|
| 91 |
-
padding: 10px;
|
| 92 |
-
border: 1px solid black;
|
| 93 |
-
z-index: 100;
|
| 94 |
-
}
|
| 95 |
-
</style>
|
| 96 |
-
"""
|
| 97 |
-
|
| 98 |
-
st.markdown(css_code, unsafe_allow_html=True)
|
| 99 |
-
st.markdown(
|
| 100 |
-
f"""
|
| 101 |
-
<div class="fixed-box">
|
| 102 |
-
<strong>Summary Stats:</strong>
|
| 103 |
-
<p>Number of Features: {num_points}</p>
|
| 104 |
-
<p>Total Area: {total_area:.2f} sq km</p>
|
| 105 |
-
</div>
|
| 106 |
-
""", unsafe_allow_html=True
|
| 107 |
-
)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import pydeck as pdk
|
| 3 |
+
import pandas as pd
|
| 4 |
import geopandas as gpd
|
| 5 |
import json
|
| 6 |
+
from shapely.geometry import mapping
|
| 7 |
|
| 8 |
+
# Load GeoJSON data function
|
| 9 |
@st.cache
|
| 10 |
def load_geojson_data(file_paths):
|
| 11 |
geodata = []
|
|
|
|
| 14 |
geodata.append(gdf)
|
| 15 |
return geodata
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
|
|
|
| 18 |
polygon_geojson = json.loads("""
|
| 19 |
{
|
| 20 |
"type": "FeatureCollection",
|
|
|
|
| 31 |
}
|
| 32 |
""")
|
| 33 |
|
| 34 |
+
# Sidebar
|
| 35 |
st.sidebar.title("Geospatial Data Visualization")
|
| 36 |
timestamp = st.sidebar.select_slider("Time", options=[f"{year}-01" for year in range(2016, 2024)])
|
| 37 |
|
| 38 |
# Main title
|
| 39 |
+
st.title("Heatmap and Polygon view:")
|
| 40 |
|
| 41 |
+
|
| 42 |
+
geojson_file_paths = ['kathajodi_centroids.geojson','chambal_centroids.geojson']
|
| 43 |
geojson_data = load_geojson_data(geojson_file_paths)
|
| 44 |
|
| 45 |
+
# Combine all geodata into one DataFrame for Pydeck
|
| 46 |
+
all_geo_data = pd.concat(geojson_data + [gpd.GeoDataFrame.from_features(polygon_geojson["features"])])
|
| 47 |
+
all_geo_data['lon'] = all_geo_data.geometry.centroid.x
|
| 48 |
+
all_geo_data['lat'] = all_geo_data.geometry.centroid.y
|
| 49 |
+
|
| 50 |
+
# Pydeck Layer for Polygons
|
| 51 |
+
polygon_layer = pdk.Layer(
|
| 52 |
+
"PolygonLayer",
|
| 53 |
+
all_geo_data,
|
| 54 |
+
get_polygon="geometry.coordinates",
|
| 55 |
+
get_fill_color="[200, 30, 0, 160]",
|
| 56 |
+
get_line_color=[0, 0, 0],
|
| 57 |
+
pickable=True,
|
| 58 |
+
auto_highlight=True
|
| 59 |
+
)
|
| 60 |
|
| 61 |
+
# Pydeck Layer for Heatmap
|
| 62 |
+
heatmap_layer = pdk.Layer(
|
| 63 |
+
"HeatmapLayer",
|
| 64 |
+
all_geo_data,
|
| 65 |
+
get_position=['lon', 'lat'],
|
| 66 |
+
aggregation=pdk.types.String("MEAN"),
|
| 67 |
+
opacity=0.5
|
| 68 |
+
)
|
| 69 |
|
| 70 |
+
# Set the viewport location
|
| 71 |
+
view_state = pdk.ViewState(
|
| 72 |
+
latitude=27.5,
|
| 73 |
+
longitude=79.5,
|
| 74 |
+
zoom=6,
|
| 75 |
+
pitch=0
|
| 76 |
+
)
|
| 77 |
|
| 78 |
+
# Render the map using Pydeck
|
| 79 |
+
r = pdk.Deck(
|
| 80 |
+
layers=[polygon_layer, heatmap_layer],
|
| 81 |
+
initial_view_state=view_state,
|
| 82 |
+
map_style='mapbox://styles/mapbox/satellite-v9'
|
| 83 |
+
)
|
| 84 |
|
| 85 |
+
# Display the map in Streamlit
|
| 86 |
+
st.pydeck_chart(r)
|
| 87 |
|
| 88 |
# Add an informational box
|
| 89 |
with st.expander("View Information"):
|
| 90 |
st.write(f"Selected Time: {timestamp}")
|
| 91 |
+
num_points = len(all_geo_data)
|
| 92 |
+
total_area = all_geo_data.geometry.area.sum() # Customize as needed.
|
| 93 |
st.write(f"Number of Features: {num_points}")
|
| 94 |
st.write(f"Total Area: {total_area:.2f} sq km")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|