Mapping & Geocoding#
https://melaniewalsh.github.io/Intro-Cultural-Analytics/07-Mapping/01-Mapping.html
Making Interactive Maps#
To map our geocoded coordinates, we’re going to use the Python library Folium. Folium is built on top of the popular JavaScript library Leaflet.
To install and import Folium, run the cells below:
import folium
Base Map#
First, we need to establish a base map. This is where we’ll map our geocoded Ithaca locations. To do so, we’re going to call folium.Map()
and enter the general latitude/longitude coordinates of the Ithaca area at a particular zoom.
(To find latitude/longitude coordintes for a particular location, you can use Google Maps, as described here.)
CCDR_map = folium.Map(location=[20,0], zoom_start=2.5)
CCDR_map
Add a Marker#
Adding a marker to a map is easy with Folium! We’ll simply call folium.Marker()
at a particular lat/lon, enter some text to display when the marker is clicked on, and then add it to our base map.
folium.Marker(location=[28.3949,84.1240], popup="Nepal").add_to(CCDR_map)
CCDR_map
Choropleth Maps#
To create a chropleth map with Folium, we need to pair a “geo.json” file (which indicates which parts of the map to shade) with a CSV file (which includes the variable that we want to shade by).
import pandas as pd
world_boundaries = (
"http://geojson.xyz/naturalearth-3.3.0/ne_50m_admin_0_countries.geojson"
)
CCDR_map = folium.Map(location=(20, 0), zoom_start=3, tiles="cartodb positron")
folium.GeoJson(world_boundaries).add_to(CCDR_map)
CCDR_map
CCDR_countries = pd.read_csv("../_static/CCDR_countries.csv")
CCDR_countries
ISO3 | Status | Name | Date | |
---|---|---|---|---|
0 | PAK | 3 | Pakistan | October 2022 |
1 | NPL | 3 | Nepal | November 2022 |
2 | BGD | 3 | Bangladesh | December 2022 |
3 | KHM | 3 | Cambodia | February 2023 |
4 | IND | 3 | India | April 2023 |
5 | BFA | 3 | Burkina Faso | January 2023 |
6 | MLI | 3 | Mali | January 2023 |
7 | NER | 3 | Niger | January 2023 |
8 | TCD | 3 | The Chad | January 2023 |
9 | GHA | 3 | Ghana | December 2022 |
10 | MRT | 3 | Mauritania | January 2023 |
11 | GNB | 3 | Guinea-Bissau | February 2023 |
12 | NGA | 3 | Nigeria | January 2023 |
13 | SEN | 3 | Senegal | May 2023 |
14 | ETH | 3 | Ethiopia | December 2022 |
15 | RWA | 3 | Rwanda | May 2023 |
16 | DOM | 3 | Dominican Republic | April 2023 |
17 | ATG | 3 | Antigua & Barbuda | September 2023 |
18 | DMA | 3 | Dominica | September 2023 |
19 | KNA | 3 | Saint Kitts & Nevis | September 2023 |
20 | LCA | 3 | Saint Lucia | September 2023 |
21 | GRD | 3 | Grenada | September 2023 |
22 | VCT | 3 | Saint Vincent & Grenadines | September 2023 |
23 | THA | 3 | Thailand | November 2023 |
24 | MYS | 3 | Malaysia | February 2024 |
25 | MNG | 1 | Mongolia | March 2024 |
26 | PHL | 0 | Philippines | April 2024 |
CCDR_map = folium.Map(location=[20,0], zoom_start=2.5)
bins = [0, 1, 2, 3]
folium.Choropleth(
geo_data = world_boundaries,
name = "Countries covered by CCDR-tools analytics",
data = CCDR_countries,
columns = ['ISO3','Status'],
key_on = 'feature.properties.iso_a3',
bins = [float(x) for x in bins],
fill_color = 'RdYlGn',
fill_opacity = 0.7,
line_opacity = 0,
nan_fill_color = "none",
legend_name = 'Countries covered',
style_function=lambda feature: {
"fillColor": step(country_dict[feature["id"]]),
"color": "black",
"weight": 2,
},
).add_to(CCDR_map)
tooltip = folium.features.GeoJson(
world_boundaries,
tooltip=folium.features.GeoJsonTooltip(fields=['name', 'pop_est', 'economy', 'income_grp'],
aliases=['Country', 'Population', 'Economy', 'Income'],
localize=True)
)
CCDR_map.add_child(tooltip)
folium.LayerControl().add_to(CCDR_map)
CCDR_map