1. Read, clean, and prepare data to build maps
1.1. Create your first choropleth world map
1.2. Create dictionaries: JSON and GeoJSON
1.3. Read and clean immigration data
2. Problem & Solution
2.1. Fusion of GeoJson and immigration data
2.2. Create your first map with pinpoints
2.3. Using WITS data to build a bar graph
2.4. Insert a bar graph in a pinpoint map
2.5. Obtaining the risk of each country
Load the notebook with commands developed in steps and 1.2 and 1.3. (click on the link):
https://colab.research.google.com/drive/1OfDdV5wv_F8fkRpNWlcI8r553u0AsPZ1?usp=sharing
The next code helps to insert the value on column 'Total' from data frame df into a new field called 'population' in the wdcs GeoJSON variable.
# Creating a new field for each country: total of immigration per country
list_countries_names = list(df['Country'])
# Selecting each country available in the JSON data
for country in wdcs['features']:
# Obtaining the name of each country
country_name = country['properties']['name']
# Verifying if the country name in JSON exists in the dataframe with the immigration data
if country_name in list_countries_names:
# Extracting the total of immigration
population_value = df.loc[df['Country'] == country_name,'Total'].values[0]
# Inserting the 'Total' of immigration per country in the JSON variable
country['properties']['population'] = float(population_value)
# If not found the country name, then insert the value zero in the JSON variable.
else:
country['properties']['population'] = 0.0
wdcs
A part of the new output could be seen:
{'type': 'FeatureCollection', 'features': [{'type': 'Feature', 'properties': {'name': 'Afghanistan', 'population': 58639.0}, 'geometry': {'type': 'Polygon', 'coordinates': [[[61.210817, 35.650072], [62.230651, 35.270664], [62.984662, 35.404041], [63.193538, 35.857166], ...
The next code confirms if the previous command had been effective.
# Extracting properties of each country
for country in wdcs['features']:
#print(country['properties'])
print(country['properties']['population'])
58639.0
2113.0
15699.0
...
The next command will employ the new field 'Population' in GeoJSON to build a choropleth map.
import folium
import matplotlib
#import branca.colormap as cm
fill_opacity = 0.7
border_color = 'black'
color_list = ['Purples','Reds','Wistia','Greens','Blues']
color_map_name = color_list[-1]
custom_cm = matplotlib.colormaps.get_cmap(color_map_name)
min_value = min(list(df['Total']))
max_value = max(list(df['Total']))
feature_name = 'population'
def get_color(custom_cm, val, vmin, vmax):
return matplotlib.colors.to_hex(custom_cm((val-vmin)/(vmax-vmin)))
# Creating a new map
map = folium.Map(location=[30,10], zoom_start=3, tiles = 'cartodbpositron')
# Creating a new Layer using population data
coro = folium.GeoJson(
wdcs,
style_function=lambda feature: {
#'fillColor': '#65b3d0',
'fillColor': get_color(custom_cm, feature['properties'][feature_name], vmin=min_value, vmax=max_value),
'color': border_color,
'fillOpacity': fill_opacity,
},
tooltip = folium.GeoJsonTooltip(fields=[feature_name], aliases=['Country pop.']),
name = feature_name
)
# Creating a new feature group to put population layer inside it
pop_group = folium.FeatureGroup(name=feature_name, show=True)
# Adding population layer to feature group
pop_group.add_child(coro)
# Inserting feature group into the map
map.add_child(pop_group)
# Inserting Layer control in the map
map.add_child(LayerControl(colapsed=False))
map
The Python code with all the steps is summarized in this Google Colab (click on the link):
https://colab.research.google.com/drive/1rTb0cNc_F8Fizi2og2OpxPtz4iFDtw2q?usp=sharing