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
The following steps will serve as a guideline to create a choropleth using available data on an internet page:
Load the notebook with commands developed in step 1.1. (click on the link):
https://colab.research.google.com/drive/1BGUOYlSMjsupiz3_lhm9PD1LwwSKSUhg?usp=sharing
JSON stands for JavaScript Object Notation. It is a text format for storing and transporting data. JSON is "self-describing" and easy to understand. The following code gives an example of how to create a variable in a JSON format:
person = {
"firstName": "Jane",
"lastName": "Doe",
"hobbies": ["running", "sky diving", "singing"],
"age": 35,
"children": [
{
"firstName": "Alice",
"age": 6
},
{
"firstName": "Bob",
"age": 8
}
]
}
person
This command produces the following figure:
To access specific data in a JSON variable the following commands could be employed. As you can see, JSON supports primitive types, like strings and numbers, as well as nested lists and objects. Wait, that looks like a Python dictionary! Let's check how to access some parts of the data.
print(person['firstName'])
print(person['children'][0])
print(person['children'][0]['firstName'])
The following result will be produced:
Jane {'firstName': 'Alice', 'age': 6} Alice
In the next example, it is possible to extract JSON data from a webpage (using command requests.get) and store it at a variable r. Then, it is possible to convert the variable r content into a JSON format file (using the command r.json) and store it at a variable wdcs.
import json
import requests
url = "https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DV0101EN/labs/Data_Files/world_countries.json"
r = requests.get(url)
wdcs = r.json()
wdcs
This will produce the result:
{'type': 'FeatureCollection', 'features': [{'type': 'Feature', 'properties': {'name': 'Afghanistan'}, 'geometry': {'type': 'Polygon', 'coordinates': [[[61.210817, 35.650072], [62.230651, 35.270664], [62.984662, 35.404041], [63.193538, 35.857166], [63.982896, 36.007957], [64.546479, 36.312073], [64.746105, 37.111818], [65.588948, 37.305217], ...
It is possible extract the name of all countries loaded from the Web Page. Using the keyword 'features' it is possible to select each country and its correspoding properties as done in the following code.
# Extracting properties of each country
for country in wdcs['features']:
print(country['properties'])
This will produce the following result:
{'name': 'Afghanistan'} {'name': 'Angola'} {'name': 'Albania'} {'name': 'United Arab Emirates'} {'name': 'Argentina'} {'name': 'Armenia'} {'name': 'Antarctica'} {'name': 'French Southern and Antarctic Lands'} {'name': 'Australia'} {'name': 'Austria'} {'name': 'Azerbaijan'} {'name': 'Burundi'} {'name': 'Belgium'} {'name': 'Benin'} {'name': 'Burkina Faso'} {'name': 'Bangladesh'} {'name': 'Bulgaria'} {'name': 'The Bahamas'} {'name': 'Bosnia and Herzegovina'} {'name': 'Belarus'} {'name': 'Belize'} {'name': 'Bolivia'} {'name': 'Brazil'} ...
For each country, it is possible to extract its corresponding geometry which is represented by an identifier type ('Polygon' or 'MultiPolygon') and a list of pairs (lists) of latitude and longitude points that delimits country borders.
# Extracting properties of each country
for country in wdcs['features']:
print(country['geometry'])
This will produce:
{'type': 'Polygon', 'coordinates': [[[61.210817, 35.650072], ...]]}
{'type': 'MultiPolygon', 'coordinates': [[[[16.326528, -5.87747], ...]]]}
{'type': 'Polygon', 'coordinates': [[[20.590247, 41.855404], ...]]}
import folium
map = folium.Map(location=[30,10], zoom_start=3, tiles = 'cartodbpositron')
folium.GeoJson(wdcs).add_to(map)
map
This will generate the following map:
The Python code with all the steps is summarized in this Google Colab (click on the link):
https://colab.research.google.com/drive/1ItkrIjeY1714-CE4OF4yWKNjE6TqHnbd?usp=sharing