Jakecole1 commited on
Commit
28b1690
·
verified ·
1 Parent(s): 4cd6c7b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -21
app.py CHANGED
@@ -1,47 +1,85 @@
1
  import streamlit as st
2
  import leafmap.foliumap as leafmap
3
  import geopandas as gpd
 
4
 
5
  # Function to load GeoJSON data
6
- @st.cache_data
7
- def load_geojson_data(file_path):
8
- gdf = gpd.read_file(file_path)
9
- return gdf
 
 
 
10
 
11
- # Load the provided GeoJSON data
12
- geojson_file_path = 'kathajodi_centroids.geojson'
13
- geojson_data = load_geojson_data(geojson_file_path)
 
 
 
 
 
 
 
 
 
14
 
15
- # Sidebar for user input
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  st.sidebar.title("Geospatial Data Visualization")
17
  timestamp = st.sidebar.select_slider("Time", options=[f"{year}-01" for year in range(2016, 2024)])
18
 
19
  # Main title
20
  st.title("Heatmap view:")
21
 
 
 
 
 
22
  # Create a Leafmap map with satellite basemap
23
- m = leafmap.Map(center=[20, 78], zoom=5, basemap="SATELLITE")
24
 
25
- # Function to generate heatmap data
26
- def generate_heatmap_data(gdf):
27
- return [(point.y, point.x) for point in gdf.geometry]
28
 
29
- # Add heatmap data
30
- heatmap_data = generate_heatmap_data(geojson_data)
31
- m.add_heatmap(heatmap_data, radius=15)
32
 
33
- # Display the map
 
 
 
 
34
  m.to_streamlit(height=600)
35
 
36
  # Add an informational box
37
  with st.expander("View Information"):
38
  st.write(f"Selected Time: {timestamp}")
39
- num_points = len(geojson_data)
40
- total_area = geojson_data.geometry.area.sum()
41
- st.write(f"Number of Points: {num_points}")
42
  st.write(f"Total Area: {total_area:.2f} sq km")
43
 
44
- # Display the summary stats in a fixed position (simulate top-right box)
45
  css_code = """
46
  <style>
47
  .fixed-box {
@@ -62,7 +100,7 @@ st.markdown(
62
  f"""
63
  <div class="fixed-box">
64
  <strong>Summary Stats:</strong>
65
- <p>Number of Points: {num_points}</p>
66
  <p>Total Area: {total_area:.2f} sq km</p>
67
  </div>
68
  """, unsafe_allow_html=True
 
1
  import streamlit as st
2
  import leafmap.foliumap as leafmap
3
  import geopandas as gpd
4
+ import json
5
 
6
  # Function to load GeoJSON data
7
+ @st.cache
8
+ def load_geojson_data(file_paths):
9
+ geodata = []
10
+ for path in file_paths:
11
+ gdf = gpd.read_file(path)
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",
32
+ "features": [
33
+ {
34
+ "type": "Feature",
35
+ "properties": {},
36
+ "geometry": {
37
+ "type": "Polygon",
38
+ "coordinates": [[[79.486114,27.577006],[79.5157554,27.5646727],[79.5405574,27.5461701],[79.5695939,27.519081],[79.5756432,27.4938634],[79.5593101,27.4871556],[79.5308786,27.5257868],[79.5003298,27.5319559],[79.4849041,27.5456337],[79.4761327,27.5652089],[79.486114,27.577006]]]
39
+ }
40
+ }
41
+ ]
42
+ }
43
+ """)
44
+
45
+ # Sidebar for input
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
+ # Load your GeoJSON files
53
+ geojson_file_paths = ['/mnt/data/kathajodi_centroids.geojson']
54
+ geojson_data = load_geojson_data(geojson_file_paths)
55
+
56
  # Create a Leafmap map with satellite basemap
57
+ m = leafmap.Map(center=[27.5, 79.5], zoom=6, basemap="SATELLITE")
58
 
59
+ # Add GeoJSON data to the map
60
+ for gdf in geojson_data:
61
+ m.add_gdf(gdf, layer_name="GeoData")
62
 
63
+ # Add the polygon GeoJSON
64
+ polygon_gdf = gpd.GeoDataFrame.from_features(polygon_geojson["features"])
65
+ m.add_gdf(polygon_gdf, layer_name="Polygon")
66
 
67
+ # Generate heatmap data and add heatmap
68
+ heatmap_data = generate_heatmap_data(geojson_data + [polygon_gdf])
69
+ m.add_heatmap(heatmap_data, radius=20)
70
+
71
+ # Display the map
72
  m.to_streamlit(height=600)
73
 
74
  # Add an informational box
75
  with st.expander("View Information"):
76
  st.write(f"Selected Time: {timestamp}")
77
+ num_points = sum(len(gdf) for gdf in geojson_data)
78
+ total_area = sum(gdf.geometry.area.sum() for gdf in geojson_data) # Customize as needed.
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 {
 
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