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.6. (click on the link) and reload all steps:
https://colab.research.google.com/drive/1lb25kyQ4eJ9-1L4WXZU9J54NE4Jmxpg1?usp=sharing
The next code helps to install a package that will enable to return the coordinates of the maritime route between the two ports. For more details, please, see https://developer.searoutes.com/reference/getgeocodingport.
!pip install searoute
Collecting searoute Downloading searoute-1.1.0.tar.gz (587 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 587.9/587.9 kB 7.7 MB/s eta 0:00:00 , ...,Successfully built searoute turfpy Installing collected packages: geojson, turfpy, osmnx, searoute Successfully installed geojson-3.0.1 osmnx-1.5.1 searoute-1.1.0 turfpy-0.0.7
The next code helps to extract the coordinates of one route between two ports using the command sr.searoute.
import searoute as sr
# Define origin and destination coordinate points:
origin = [0.3515625, 50.064191736659104]
destination = [117.42187500000001, 39.36827914916014]
route = sr.searoute(origin, destination)
# > Returns a GeoJSON LineString Feature
# show route distance with unit
print("{:.1f} {}".format(route.properties['length'], route.properties['units']))
# Optionally, define the units for the length calculation included in the properties object.
# Defaults to km, can be can be 'm' = meters 'mi = miles 'ft' = feets 'in' = inches 'deg' = degrees
# 'cen' = centimeters 'rad' = radians 'naut' = nauticals 'yd' = yards
routeMiles = sr.searoute(origin, destination, units="mi", append_orig_dest=True, include_ports=True)
routeMiles
20383.8 km
{"geometry": {"coordinates": [[0.351562, 50.064192], [0.374906, 49.764891], [0.371929, 49.763637], [0.3404, 49.779448], [0.1, 49.9], [-0.313513, 49.914768], [-0.550422, 49.923229], [-0.553245, 49.92333], [-1.3, 49.95], [-1.47977, 49.895072], [-1.665743, 49.838248], [-1.93365, 49.75639], [-1.992006, 49.738559], [-2.39189, 49.616376], [-3.197043, 49.370363], [-3.986163, 49.129249], [-4.030414, 49.115729], [-4.270518, 49.042365], [-5.06401, 48.799916], [-5.5, 48.6667], [-6.698867, 47.340067], [-7.954994, 45.668797], [-8.49576, 44.934776], ...,[117.421875, 39.368279]], "type": "LineString"}, "properties": {"duration_hours": 461.7701645514209, "length": 12753.500879099618, "port_dest": {"cty": "China", "name": "Tanggu", "port": "CNTGU", "t": null, "x": 117.39, "y": 39.01}, "port_origin": {"cty": "France", "name": "Fecamp", "port": "FRFEC", "t": null, "x": 0.374906, "y": 49.764891}, "units": "mi"}, "type": "Feature"}
The next code shows in to select just the coordinates of the routes. It returns a list of lists.
routeMiles["geometry"]["coordinates"]
[[0.351562, 50.064192], [0.374906, 49.764891], [0.371929, 49.763637], [0.3404, 49.779448], [0.1, 49.9], ...,[118.1725, 38.7726], [117.39, 39.01], [117.421875, 39.368279]]
The next code shows on a map the draw of the route.
import folium
m = folium.Map(location=[37.862499, 58.238056],
zoom_start=4)
loc = []
for lon,lat in routeMiles["geometry"]["coordinates"]:
loc.append([lat, lon])
folium.PolyLine(loc,
color='red',
weight=5,
opacity=0.8).add_to(m)
m
The next code employs the previous map that had been stored in the variable map and merges the choropleth map with the risk measure with the line of the route.
folium.PolyLine(loc,
color='red',
weight=5,
opacity=0.8).add_to(map)
map
The Python code with all the steps is summarized in this Google Colab (click on the link):
https://colab.research.google.com/drive/1bowGlMeVSCzU0634OgYhjGUe4XAnk03j?usp=sharing