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 step 2.5. (click on the link) and reload all steps:
https://colab.research.google.com/drive/1xHbcvflGxg-T2cCXkhvGE2Ubyr9t5_hE?usp=sharing
The next code helps to insert the field 'Combined_Score' in the JSON variable wdcs.
# Creating a new field for each #country: 'combined_score' of risk per country
list_countries_names = list(df_risk['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 Combined Score of risk
combined_score_value = df_risk.loc[df_risk['country'] == country_name,'Combined_Score'].values[0]
# Inserting the 'combined_score_value' of risk score per country in the JSON variable
country['properties']['combined_score'] = float(combined_score_value)
# If not found the country name, then insert the value zero in the JSON variable.
else:
country['properties']['combined_score'] = 0.0
wdcs
{'type': 'FeatureCollection', 'features': [{'type': 'Feature', 'properties': {'name': 'Afghanistan', 'combined_score': 0.6702986215}, 'geometry': {'type': 'Polygon', 'coordinates': [[[61.210817, 35.650072], ..., [61.210817, 35.650072]]]}, 'id': 'AFG'}, {'type': 'Feature', 'properties': {'name': 'Angola', 'combined_score': 0.767313165}, 'geometry': {'type': 'MultiPolygon', 'coordinates': [[[[16.326528, -5.87747], ...
The next code helps to show if the new value of the risk score had been correctly inserted into the JSON variable.
# Extracting properties of each country
for country in wdcs['features']:
#print(country['properties'])
print(country['properties']['combined_score'])
0.6702986215 0.767313165 0.372736933 0.1938966 0.6777903135 0.203256629 0.0 0.0 0.393912523 0.198900957 0.245246763 ...
The next code shows in a map the new data per country, i.e., 'combined_score'.
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_risk['Combined_Score']))
max_value = max(list(df_risk['Combined_Score']))
feature_name = 'combined_score'
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 Risk']),
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/1lb25kyQ4eJ9-1L4WXZU9J54NE4Jmxpg1?usp=sharing