-
Notifications
You must be signed in to change notification settings - Fork 0
/
createMap.py
executable file
·73 lines (59 loc) · 2.04 KB
/
createMap.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# (c) Tiago Tamagusko 2022
"""Create map with road quality data."""
from __future__ import annotations
import folium
import geopandas as gpd
import pandas as pd
from streamlit_folium import folium_static
def road_quality_map(data: str, center: list) -> None:
"""Create a map with road quality data.
Args:
data: csv file (plus path) with road quality data.
center: Center (lat, lon) of the map.
Returns:
Map with road quality data.
"""
# download geojson data
df_world = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))
df = pd.read_csv(data, encoding="utf-8")
# filter countries with data
countries = list(df.name)
gdf = df_world[df_world["name"].isin(countries)]
# rename name column to country
df = df.rename(columns={"name": "country"})
gdf = gdf.rename(columns={"name": "country"})
# countries with no data
no_data = df_world[~df_world["name"].isin(countries)]
# clean geo data
gdf = gdf[["country", "continent", "geometry"]]
# add road quality data to geo data
gdf = pd.merge(gdf, df, how="right", on=["country"])
# clean data
gdf = gdf.dropna()
# create map
Map = folium.Map(location=center, tiles="CartoDB Positron", zoom_start=4)
cp = folium.Choropleth(
geo_data=gdf,
name="choropleth",
data=df,
columns=["country", "quality_road"],
key_on="feature.properties.country",
fill_color="RdYlBu",
fill_opacity=0.8,
line_opacity=0.2,
legend_name="Quality of road infrastructure (1 = extremely poor; 7 = extremely good)",
).add_to(Map)
# fill countries with no data (gray)
folium.GeoJson(
data=no_data,
style_function=lambda feature: {
"fillColor": "gray",
"fillOpacity": 0.5,
"color": "gray",
"weight": 1,
},
).add_to(Map)
# add tooltip values
folium.GeoJsonTooltip(["country", "quality_road"]).add_to(cp.geojson)
folium.LayerControl().add_to(Map)
folium_static(Map)