awacke1 commited on
Commit
c8a522d
·
1 Parent(s): c32fde0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -55
app.py CHANGED
@@ -1,65 +1,87 @@
1
  import streamlit as st
2
- import folium
3
- import dask.dataframe as dd
4
- from folium.plugins import MarkerCluster
5
- # from folium import folium_static
6
  import pandas as pd
 
7
 
8
- # Top 10 locations for viewing Northern Lights in Iceland
9
- northern_lights_locations = [
10
- {"name": "Reykjavik", "lat": 64.1265, "lon": -21.8174},
11
- {"name": "Akureyri", "lat": 65.6885, "lon": -18.1262},
12
- {"name": "Vik", "lat": 63.4194, "lon": -19.0096},
13
- {"name": "Hofn", "lat": 64.2539, "lon": -15.2082},
14
- {"name": "Husavik", "lat": 66.0449, "lon": -17.3382},
15
- {"name": "Grundarfjordur", "lat": 64.9226, "lon": -23.2543},
16
- {"name": "Egilsstadir", "lat": 65.2669, "lon": -14.3948},
17
- {"name": "Seydisfjordur", "lat": 65.2592, "lon": -14.0101},
18
- {"name": "Isafjordur", "lat": 66.0750, "lon": -23.1266},
19
- {"name": "Kirkjufell", "lat": 64.9426, "lon": -23.3061},
20
- ]
21
-
22
- # Function to display the map
23
- def display_map(locations, lat, lon):
24
- # Create a folium map centered in Iceland
25
- iceland_map = folium.Map(location=[lat, lon], zoom_start=6)
26
-
27
- # Create a marker cluster group
28
- marker_cluster = MarkerCluster().add_to(iceland_map)
29
 
30
- # Create a Dask dataframe from the list of locations
31
- dask_df = dd.from_pandas(pd.DataFrame(locations), npartitions=4)
 
 
 
 
 
 
 
 
 
 
 
32
 
33
- # Add markers for the top 10 locations for viewing Northern Lights
34
- def add_markers(row):
35
- folium.Marker(
36
- location=[row["lat"], row["lon"]],
37
- popup=f"<b>{row['name']}</b>",
38
- ).add_to(marker_cluster)
 
 
 
 
 
 
 
39
 
40
- dask_df.map_partitions(lambda df: df.apply(add_markers, axis=1)).compute()
 
 
 
 
41
 
42
- return iceland_map
 
 
43
 
44
- # Streamlit app
45
- st.title("Northern Lights Map in Iceland")
46
-
47
- # User input for latitude and longitude
48
- lat = st.number_input("Enter latitude:", value=64.9631, step=0.0001)
49
- lon = st.number_input("Enter longitude:", value=-19.0208, step=0.0001)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
- # Display the map
52
- st.write("Northern Lights Viewing Locations in Iceland:")
53
- folium_map = display_map(northern_lights_locations, lat, lon)
54
- folium_static(folium_map)
 
 
 
 
 
 
 
 
 
 
55
 
56
- # Cite references
57
- st.write(
58
- """
59
- References:
60
- - [Folium documentation](https://python-visualization.github.io/folium/)
61
- - [Streamlit documentation](https://docs.streamlit.io/)
62
- - [Dask documentation](https://dask.org/)
63
- - [Top 10 locations](https://www.guidetoiceland.is/best-of-iceland/top-10-places-to-see-the-northern-lights)
64
- """
65
- )
 
1
  import streamlit as st
2
+ import altair as alt
3
+ from vega_datasets import data
 
 
4
  import pandas as pd
5
+ import pydeck as pdk
6
 
7
+ # Define the data source for the map
8
+ iceland_geojson = "https://raw.githubusercontent.com/deldersveld/topojson/master/countries/iceland/iceland-counties.json"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
+ # Define the mythological cities with their respective latitude and longitude
11
+ cities = {
12
+ "Asgard": [64.7938, -17.3413],
13
+ "Helheim": [63.8278, -21.1865],
14
+ "Jotunheim": [64.8441, -19.1669],
15
+ "Midgard": [63.9863, -22.6210],
16
+ "Muspellheim": [65.3201, -16.4235],
17
+ "Nidavellir": [64.9011, -18.0580],
18
+ "Svartalfheim": [64.0114, -21.4504],
19
+ "Valhalla": [63.7267, -19.6133],
20
+ "Vanaheim": [64.7381, -17.4497],
21
+ "Yggdrasil": [64.8999, -19.0044]
22
+ }
23
 
24
+ # Define the colors to use for each city marker
25
+ colors = {
26
+ "Asgard": [255, 0, 0],
27
+ "Helheim": [255, 255, 0],
28
+ "Jotunheim": [0, 0, 255],
29
+ "Midgard": [0, 255, 0],
30
+ "Muspellheim": [255, 153, 0],
31
+ "Nidavellir": [153, 0, 255],
32
+ "Svartalfheim": [0, 255, 255],
33
+ "Valhalla": [255, 0, 255],
34
+ "Vanaheim": [153, 255, 0],
35
+ "Yggdrasil": [255, 255, 255]
36
+ }
37
 
38
+ # Define the Streamlit app layout
39
+ st.set_page_config(layout="wide")
40
+ st.title("Mythological Cities in Iceland")
41
+ st.sidebar.title("Select a City to View the Northern Lights From")
42
+ selected_city = st.sidebar.selectbox("", sorted(cities.keys()))
43
 
44
+ # Load the Icelandic county boundaries and prepare the data for the 3D map
45
+ iceland_data = alt.topo_feature(iceland_geojson, "iceland-counties")
46
+ iceland_df = pd.DataFrame(iceland_data["features"])
47
 
48
+ # Create the 3D map with Deck.gl and Altair
49
+ view_state = pdk.ViewState(latitude=64.9, longitude=-18.5, zoom=5, pitch=40)
50
+ layers = [
51
+ pdk.Layer(
52
+ "PolygonLayer",
53
+ data=iceland_data,
54
+ get_polygon="coordinates",
55
+ filled=True,
56
+ extruded=True,
57
+ get_elevation="properties.avg_elevation",
58
+ elevation_scale=1000,
59
+ get_fill_color=[200, 200, 200, 200]
60
+ ),
61
+ pdk.Layer(
62
+ "ScatterplotLayer",
63
+ data=pd.DataFrame({"latitude": [cities[selected_city][0]], "longitude": [cities[selected_city][1]]}),
64
+ get_position="[longitude, latitude]",
65
+ get_color=colors[selected_city],
66
+ get_radius=20000
67
+ )
68
+ ]
69
 
70
+ r = pdk.Deck(layers=layers, initial_view_state=view_state)
71
+ altair_chart = alt.Chart(iceland_df).mark_geoshape(
72
+ stroke="black",
73
+ strokeWidth=0.5
74
+ ).encode(
75
+ color=alt.Color("properties.avg_elevation:Q", scale=alt.Scale(scheme="viridis")),
76
+ tooltip=[alt.Tooltip("properties.name", title="County"), alt.Tooltip("properties.avg_elevation:Q", title="Elevation (m)")]
77
+ ).transform_lookup(
78
+ lookup="id",
79
+ from_=alt.LookupData(iceland_data, "id", ["properties.avg_elevation", "properties.name"])
80
+ ).properties(
81
+ width=900,
82
+ height=600
83
+ ).interactive()
84
 
85
+ Display the 3D map and the Altair chart
86
+ st.pydeck_chart(r)
87
+ st.altair_chart(altair_chart)