Jakecole1 commited on
Commit
09d401b
·
verified ·
1 Parent(s): 233fd2c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -81
app.py CHANGED
@@ -1,101 +1,112 @@
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_data
10
  def load_geojson_data(file_paths):
11
  geodata = []
12
  for path in file_paths:
13
  gdf = gpd.read_file(path)
 
 
14
  geodata.append(gdf)
15
  return geodata
16
 
17
- # Sample Polygon GeoJSON
18
- polygon_geojson = json.loads("""
19
- {
20
- "type": "FeatureCollection",
21
- "features": [
22
- {
23
- "type": "Feature",
24
- "properties": {"density": 10},
25
- "geometry": {
26
- "type": "Polygon",
27
- "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]]]
28
- }
29
- }
30
- ]
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("Hexagon and Polygon view:")
 
 
 
40
 
41
 
42
- geojson_file_paths = ['chambal_centroids.geojson','kathajodi_centroids.geojson']
43
  geojson_data = load_geojson_data(geojson_file_paths)
44
 
45
 
46
- if geojson_data:
47
- all_geo_data = pd.concat(geojson_data + [gpd.GeoDataFrame.from_features(polygon_geojson["features"])])
48
- else:
49
- all_geo_data = gpd.GeoDataFrame.from_features(polygon_geojson["features"])
50
-
51
- # Add centroid coordinates to all_geo_data
52
- all_geo_data['lon'] = all_geo_data.geometry.centroid.x
53
- all_geo_data['lat'] = all_geo_data.geometry.centroid.y
54
-
55
- # Hexagon Layer
56
- hexagon_layer = pdk.Layer(
57
- "HexagonLayer",
58
- all_geo_data,
59
- get_position=['lon', 'lat'],
60
- elevation_scale=50,
61
- radius=2000,
62
- elevation_range=[0, 3000],
63
- pickable=True,
64
- extruded=True,
65
- )
66
-
67
- polygon_layer = pdk.Layer(
68
- "PolygonLayer",
69
- all_geo_data,
70
- get_polygon="geometry.coordinates",
71
- get_fill_color="[200, 30, 0, 160]",
72
- get_line_color=[0, 0, 0],
73
- pickable=True,
74
- auto_highlight=True
75
- )
76
-
77
- # Set viewport location
78
- view_state = pdk.ViewState(
79
- latitude=27.5,
80
- longitude=79.5,
81
- zoom=6,
82
- pitch=40
83
- )
84
-
85
-
86
- r = pdk.Deck(
87
- layers=[hexagon_layer, polygon_layer],
88
- initial_view_state=view_state,
89
- map_style='mapbox://styles/mapbox/satellite-v9'
90
- )
91
-
92
-
93
- st.pydeck_chart(r)
94
-
95
- # Add an informational box
96
- with st.expander("View Information"):
97
- st.write(f"Selected Time: {timestamp}")
98
- num_points = len(all_geo_data)
99
- total_area = all_geo_data.geometry.area.sum()
100
- st.write(f"Number of Features: {num_points}")
101
- st.write(f"Total Area: {total_area:.2f} sq km")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import pydeck as pdk
 
3
  import geopandas as gpd
4
+ import pandas as pd
5
+
6
 
 
7
  @st.cache_data
8
  def load_geojson_data(file_paths):
9
  geodata = []
10
  for path in file_paths:
11
  gdf = gpd.read_file(path)
12
+ gdf['lon'] = gdf.geometry.x
13
+ gdf['lat'] = gdf.geometry.y
14
  geodata.append(gdf)
15
  return geodata
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  st.sidebar.title("Geospatial Data Visualization")
18
  timestamp = st.sidebar.select_slider("Time", options=[f"{year}-01" for year in range(2016, 2024)])
19
 
20
+
21
+ st.title("Geospatial Data Visualization")
22
+
23
+
24
+ geojson_file_paths = ['kathajodi_centroids.geojson','chambal_centroids.geojson']
25
 
26
 
 
27
  geojson_data = load_geojson_data(geojson_file_paths)
28
 
29
 
30
+ all_geo_data = pd.concat(geojson_data)
31
+ all_geo_data['lon'] = all_geo_data.geometry.x
32
+ all_geo_data['lat'] = all_geo_data.geometry.y
33
+
34
+
35
+ if 'view_state' not in st.session_state:
36
+ st.session_state.view_state = pdk.ViewState(
37
+ latitude=all_geo_data['lat'].mean(),
38
+ longitude=all_geo_data['lon'].mean(),
39
+ zoom=10,
40
+ pitch=45,
41
+ bearing=0
42
+ )
43
+
44
+
45
+ tab1, tab2 = st.tabs(["Heatmap", "3D Hexagon Plot"])
46
+
47
+
48
+ with tab1:
49
+ st.subheader("Heatmap View")
50
+
51
+ # Pydeck HeatmapLayer for Points
52
+ heatmap_layer = pdk.Layer(
53
+ "HeatmapLayer",
54
+ all_geo_data,
55
+ get_position=['lon', 'lat'],
56
+ aggregation=pdk.types.String("MEAN"),
57
+ opacity=0.5
58
+ )
59
+
60
+ r = pdk.Deck(
61
+ layers=[heatmap_layer],
62
+ initial_view_state=st.session_state.view_state,
63
+ map_style='mapbox://styles/mapbox/satellite-v9'
64
+ )
65
+
66
+
67
+ st.session_state.view_state = r.initial_view_state
68
+
69
+
70
+ st.pydeck_chart(r)
71
+
72
+ # Add an informational box
73
+ with st.expander("View Information"):
74
+ st.write(f"Selected Time: {timestamp}")
75
+ num_points = len(all_geo_data)
76
+ st.write(f"Number of Features: {num_points}")
77
+
78
+ # 3D Hexagon Plot Tab
79
+ with tab2:
80
+ st.subheader("3D Hexagon Map View")
81
+
82
+
83
+ hexagon_layer = pdk.Layer(
84
+ "HexagonLayer",
85
+ all_geo_data,
86
+ get_position=['lon', 'lat'],
87
+ radius=200,
88
+ elevation_scale=20,
89
+ elevation_range=[0, 1000],
90
+ extruded=True,
91
+ coverage=1,
92
+ pickable=True
93
+ )
94
+
95
+
96
+ r = pdk.Deck(
97
+ layers=[hexagon_layer],
98
+ initial_view_state=st.session_state.view_state,
99
+ map_style='mapbox://styles/mapbox/satellite-v9'
100
+ )
101
+
102
+
103
+ st.session_state.view_state = r.initial_view_state
104
+
105
+
106
+ st.pydeck_chart(r)
107
+
108
+ # Add informational box
109
+ with st.expander("View Information"):
110
+ st.write(f"Selected Time: {timestamp}")
111
+ num_points = len(all_geo_data)
112
+ st.write(f"Number of Features: {num_points}")