drguilhermeapolinario commited on
Commit
09fb6a9
·
verified ·
1 Parent(s): d9cae3e

Update views/maps.py

Browse files
Files changed (1) hide show
  1. views/maps.py +186 -186
views/maps.py CHANGED
@@ -1,187 +1,187 @@
1
- import folium
2
- import geopandas as gpd
3
- import plotly.express as px
4
- from branca.colormap import LinearColormap
5
- from folium.plugins import HeatMap
6
- from streamlit_folium import folium_static
7
-
8
- import streamlit as st
9
- from streamlit_extras.stylable_container import stylable_container
10
- from streamlit_extras.add_vertical_space import add_vertical_space
11
- import streamlit_shadcn_ui as ui
12
-
13
- with stylable_container(
14
- key="banner",
15
- css_styles="""
16
- img {
17
- width: 1800px;
18
- height: 400px;
19
- overflow: hidden;
20
- position: relative;
21
- object-fit: cover;
22
- border-radius: 20px; /* Adiciona bordas arredondadas */
23
- mask-image: linear-gradient(to bottom, rgba(0, 0, 0, 1), rgba(0, 0, 0, 0));
24
- -webkit-mask-image: linear-gradient(to bottom, rgba(0, 0, 0, 1), rgba(0, 0, 0, 0)); /* For Safari */
25
- }
26
- """,
27
- ):
28
- st.image("src/images/mp.jpg")
29
-
30
- st.title("Mapas da área")
31
- add_vertical_space(5)
32
- st.markdown(""" ### :world_map: **UBS Flamengo: (IBGE 2022)** """)
33
- add_vertical_space(5)
34
-
35
-
36
- @st.cache_data
37
- def load_data():
38
- """
39
- A function that loads and reads geojson data for UBS Flamengo and converts it to the specified coordinate reference system.
40
- """
41
- return gpd.read_file("views\\flamengo_ibge2022.geojson").to_crs(epsg=4326)
42
-
43
- gdf = load_data()
44
- LATITUDE = -19.971591804
45
- LONGITUDE = -44.057912815
46
- lat = -19.96214
47
- long = -44.05603
48
-
49
-
50
-
51
- total_pop = gdf["POP"].sum()
52
- col1, col2, col3 = st.columns([1, 1, 5])
53
-
54
- with col1:
55
- # st.write(f"###### População Total: {total_pop:,}")
56
- map_type = st.selectbox("Tipo de mapa", ["População", "Densidade", "Heatmap"])
57
-
58
- with col2:
59
- # st.write(f"###### Número de Setores Censitários: {len(gdf)}")
60
- base_map = st.selectbox("Mapa base", ["Cartodb Positron", "OpenStreetMap"])
61
-
62
- with col3:
63
- total_pop = gdf["POP"].sum()
64
- st.write(
65
- f"### 👪 População Total: {total_pop:,} habitantes, dados do Censo 2022 IBGE"
66
- )
67
- st.write(f"### 🗺️ Número de Setores Censitários: {len(gdf)}")
68
-
69
- add_vertical_space(5)
70
- add_vertical_space(5)
71
-
72
- col1, col2 = st.columns(2)
73
- with col1:
74
- m = folium.Map(
75
- location=[LATITUDE, LONGITUDE], tiles=base_map, zoom_start=15
76
- )
77
- dns_p = "Densidade pop. (hab/km²) UBS Flamengo - IBGE 2022"
78
- if map_type in ["População", "Densidade"]:
79
- if map_type == "População":
80
- column = "POP"
81
- caption = "Pop. residente UBS Flamengo IBGE 2022"
82
- else:
83
- gdf["DENSIDADE"] = gdf["POP"] / gdf["AREA_KM2"]
84
- column = "DENSIDADE"
85
- caption = dns_p
86
- colorscale = px.colors.sequential.Viridis
87
- colormap = LinearColormap(
88
- colors=colorscale,
89
- vmin=gdf[column].min(),
90
- vmax=gdf[column].max(),
91
- caption=caption,
92
- )
93
- folium.GeoJson(
94
- gdf,
95
- style_function=lambda feature: {
96
- "fillColor": colormap(feature["properties"][column]),
97
- "color": "black",
98
- "weight": 1,
99
- "fillOpacity": 0.7,
100
- },
101
- highlight_function=lambda feature: {
102
- "fillColor": "#ffaf00",
103
- "color": "green",
104
- "weight": 3,
105
- "fillOpacity": 0.9,
106
- },
107
- tooltip=folium.features.GeoJsonTooltip(
108
- fields=["CD_SETOR", column, "AREA_KM2"],
109
- aliases=[
110
- "Setor Censitário:",
111
- f"{caption}:",
112
- "Área (km²):",
113
- ],
114
- style=(
115
- "background-color: white; color: #333333;"
116
- "font-family: calibri; font-size: 12px;"
117
- "padding: 10px;"
118
- ),
119
- ),
120
- ).add_to(m)
121
- colormap.add_to(m)
122
-
123
- elif map_type == "Heatmap":
124
- heat_data = [
125
- [
126
- row["geometry"].centroid.y,
127
- row["geometry"].centroid.x,
128
- row["POP"],
129
- ]
130
- for idx, row in gdf.iterrows()
131
- ]
132
- HeatMap(heat_data).add_to(m)
133
- folium.Marker(
134
- [lat, long],
135
- popup="UBS Flamengo",
136
- tooltip="UBS Flamengo",
137
- icon=folium.Icon(color="red", icon="info-sign"),
138
- ).add_to(m)
139
-
140
- STYLE_STATEMENT = (
141
- "<style>.leaflet-control-layers"
142
- "{ position: fixed; top: 10px; left: 50px; } </style>"
143
- )
144
- m.get_root().html.add_child(folium.Element(STYLE_STATEMENT))
145
- folium_static(m)
146
-
147
- with col2:
148
- fig = px.bar(
149
- gdf,
150
- x="CD_SETOR",
151
- y="POP",
152
- title="Distribuição da População por Setor Censitário",
153
- color="POP",
154
- color_continuous_scale=px.colors.sequential.Viridis,
155
- )
156
- st.plotly_chart(fig)
157
-
158
- age_columns = [
159
- "POP_0A4",
160
- "POP_5A9",
161
- "POP_10A14",
162
- "POP_15A19",
163
- "POP_20A24",
164
- "POP_25A29",
165
- "POP_30A34",
166
- "POP_35A39",
167
- "POP_40A44",
168
- "POP_45A49",
169
- "POP_50A54",
170
- "POP_55A59",
171
- "POP_60A64",
172
- "POP_65A69",
173
- "POP_70A74",
174
- "POP_75A79",
175
- "POP_80A84",
176
- "POP_85A89",
177
- "POP_90A94",
178
- "POP_95A99",
179
- "POP_100OUMAIS",
180
- ]
181
-
182
-
183
-
184
- add_vertical_space(10)
185
-
186
-
187
  st.write('----')
 
1
+ import folium
2
+ import geopandas as gpd
3
+ import plotly.express as px
4
+ from branca.colormap import LinearColormap
5
+ from folium.plugins import HeatMap
6
+ from streamlit_folium import folium_static
7
+
8
+ import streamlit as st
9
+ from streamlit_extras.stylable_container import stylable_container
10
+ from streamlit_extras.add_vertical_space import add_vertical_space
11
+ import streamlit_shadcn_ui as ui
12
+
13
+ with stylable_container(
14
+ key="banner",
15
+ css_styles="""
16
+ img {
17
+ width: 1800px;
18
+ height: 400px;
19
+ overflow: hidden;
20
+ position: relative;
21
+ object-fit: cover;
22
+ border-radius: 20px; /* Adiciona bordas arredondadas */
23
+ mask-image: linear-gradient(to bottom, rgba(0, 0, 0, 1), rgba(0, 0, 0, 0));
24
+ -webkit-mask-image: linear-gradient(to bottom, rgba(0, 0, 0, 1), rgba(0, 0, 0, 0)); /* For Safari */
25
+ }
26
+ """,
27
+ ):
28
+ st.image("mp.jpg")
29
+
30
+ st.title("Mapas da área")
31
+ add_vertical_space(5)
32
+ st.markdown(""" ### :world_map: **UBS Flamengo: (IBGE 2022)** """)
33
+ add_vertical_space(5)
34
+
35
+
36
+ @st.cache_data
37
+ def load_data():
38
+ """
39
+ A function that loads and reads geojson data for UBS Flamengo and converts it to the specified coordinate reference system.
40
+ """
41
+ return gpd.read_file("views\\flamengo_ibge2022.geojson").to_crs(epsg=4326)
42
+
43
+ gdf = load_data()
44
+ LATITUDE = -19.971591804
45
+ LONGITUDE = -44.057912815
46
+ lat = -19.96214
47
+ long = -44.05603
48
+
49
+
50
+
51
+ total_pop = gdf["POP"].sum()
52
+ col1, col2, col3 = st.columns([1, 1, 5])
53
+
54
+ with col1:
55
+ # st.write(f"###### População Total: {total_pop:,}")
56
+ map_type = st.selectbox("Tipo de mapa", ["População", "Densidade", "Heatmap"])
57
+
58
+ with col2:
59
+ # st.write(f"###### Número de Setores Censitários: {len(gdf)}")
60
+ base_map = st.selectbox("Mapa base", ["Cartodb Positron", "OpenStreetMap"])
61
+
62
+ with col3:
63
+ total_pop = gdf["POP"].sum()
64
+ st.write(
65
+ f"### 👪 População Total: {total_pop:,} habitantes, dados do Censo 2022 IBGE"
66
+ )
67
+ st.write(f"### 🗺️ Número de Setores Censitários: {len(gdf)}")
68
+
69
+ add_vertical_space(5)
70
+ add_vertical_space(5)
71
+
72
+ col1, col2 = st.columns(2)
73
+ with col1:
74
+ m = folium.Map(
75
+ location=[LATITUDE, LONGITUDE], tiles=base_map, zoom_start=15
76
+ )
77
+ dns_p = "Densidade pop. (hab/km²) UBS Flamengo - IBGE 2022"
78
+ if map_type in ["População", "Densidade"]:
79
+ if map_type == "População":
80
+ column = "POP"
81
+ caption = "Pop. residente UBS Flamengo IBGE 2022"
82
+ else:
83
+ gdf["DENSIDADE"] = gdf["POP"] / gdf["AREA_KM2"]
84
+ column = "DENSIDADE"
85
+ caption = dns_p
86
+ colorscale = px.colors.sequential.Viridis
87
+ colormap = LinearColormap(
88
+ colors=colorscale,
89
+ vmin=gdf[column].min(),
90
+ vmax=gdf[column].max(),
91
+ caption=caption,
92
+ )
93
+ folium.GeoJson(
94
+ gdf,
95
+ style_function=lambda feature: {
96
+ "fillColor": colormap(feature["properties"][column]),
97
+ "color": "black",
98
+ "weight": 1,
99
+ "fillOpacity": 0.7,
100
+ },
101
+ highlight_function=lambda feature: {
102
+ "fillColor": "#ffaf00",
103
+ "color": "green",
104
+ "weight": 3,
105
+ "fillOpacity": 0.9,
106
+ },
107
+ tooltip=folium.features.GeoJsonTooltip(
108
+ fields=["CD_SETOR", column, "AREA_KM2"],
109
+ aliases=[
110
+ "Setor Censitário:",
111
+ f"{caption}:",
112
+ "Área (km²):",
113
+ ],
114
+ style=(
115
+ "background-color: white; color: #333333;"
116
+ "font-family: calibri; font-size: 12px;"
117
+ "padding: 10px;"
118
+ ),
119
+ ),
120
+ ).add_to(m)
121
+ colormap.add_to(m)
122
+
123
+ elif map_type == "Heatmap":
124
+ heat_data = [
125
+ [
126
+ row["geometry"].centroid.y,
127
+ row["geometry"].centroid.x,
128
+ row["POP"],
129
+ ]
130
+ for idx, row in gdf.iterrows()
131
+ ]
132
+ HeatMap(heat_data).add_to(m)
133
+ folium.Marker(
134
+ [lat, long],
135
+ popup="UBS Flamengo",
136
+ tooltip="UBS Flamengo",
137
+ icon=folium.Icon(color="red", icon="info-sign"),
138
+ ).add_to(m)
139
+
140
+ STYLE_STATEMENT = (
141
+ "<style>.leaflet-control-layers"
142
+ "{ position: fixed; top: 10px; left: 50px; } </style>"
143
+ )
144
+ m.get_root().html.add_child(folium.Element(STYLE_STATEMENT))
145
+ folium_static(m)
146
+
147
+ with col2:
148
+ fig = px.bar(
149
+ gdf,
150
+ x="CD_SETOR",
151
+ y="POP",
152
+ title="Distribuição da População por Setor Censitário",
153
+ color="POP",
154
+ color_continuous_scale=px.colors.sequential.Viridis,
155
+ )
156
+ st.plotly_chart(fig)
157
+
158
+ age_columns = [
159
+ "POP_0A4",
160
+ "POP_5A9",
161
+ "POP_10A14",
162
+ "POP_15A19",
163
+ "POP_20A24",
164
+ "POP_25A29",
165
+ "POP_30A34",
166
+ "POP_35A39",
167
+ "POP_40A44",
168
+ "POP_45A49",
169
+ "POP_50A54",
170
+ "POP_55A59",
171
+ "POP_60A64",
172
+ "POP_65A69",
173
+ "POP_70A74",
174
+ "POP_75A79",
175
+ "POP_80A84",
176
+ "POP_85A89",
177
+ "POP_90A94",
178
+ "POP_95A99",
179
+ "POP_100OUMAIS",
180
+ ]
181
+
182
+
183
+
184
+ add_vertical_space(10)
185
+
186
+
187
  st.write('----')