File size: 1,668 Bytes
22405ac
 
 
 
088de9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import streamlit as st
import json
import geopandas as gpd
import pyproj
import plotly.graph_objs as go
polygon = gpd.read_file(r"\Downloads\CityBoundaries.shp")

# project geopandas dataframe
map_df = polygon
map_df.to_crs(pyproj.CRS.from_epsg(4326), inplace=True)

# reading in the points shapefile
points = gpd.read_file(r"Downloads\USA_Major_Cities.shp")

# project geopandas dataframe
points.to_crs(pyproj.CRS.from_epsg(4326), inplace=True)

# define lat, long for ponints
Lat = points['Lat']
Long = points['Long']

# leases to geojson
path = r"C:\Users\project\geojson.json"

map_df.to_file(path, driver = "GeoJSON")
with open(path) as geofile:
    j_file = json.load(geofile)

#index geojson
i=1
for feature in j_file["features"]:
    feature ['id'] = str(i).zfill(2)
    i += 1
    
# mapbox token
mapboxt = 'MapBox Token'

# define layers and plot map
choro = go.Choroplethmapbox(z=map_df['STFIPS'], locations = map_df.index, colorscale = 'Viridis', geojson = j_file, text = map_df['NAME'], marker_line_width=0.1)
     # Your choropleth data here


scatt = go.Scattermapbox(lat=Lat,lon=Long,mode='markers+text',below='False', marker=dict( size=12, color ='rgb(56, 44, 100)'))
     # Your scatter data here

layout = go.Layout(title_text ='USA Cities', title_x =0.5, width=950, height=700,mapbox = dict(center= dict(lat=37, lon=-95),accesstoken= mapboxt, zoom=4,style="stamen-terrain"))
    

# streamlit multiselect widget
layer1 = st.multiselect('Layer Selection', [choro, scatt], format_func=lambda x: 'Polygon' if x==choro else 'Points')


#st.write('Layer 1:', layer1)
fig = go.Figure(data=layer1, layout=layout)


# display streamlit
st.plotly_chart(fig)