Goals of this notebook:
citiesdataframes, determine for each city the country in which it is located.
%matplotlib inline import pandas as pd import geopandas pd.options.display.max_rows = 10
countries = geopandas.read_file("zip://./data/ne_110m_admin_0_countries.zip") cities = geopandas.read_file("zip://./data/ne_110m_populated_places.zip") rivers = geopandas.read_file("zip://./data/ne_50m_rivers_lake_centerlines.zip")
Pandas provides functionality to join or merge dataframes in different ways, see https://chrisalbon.com/python/data_wrangling/pandas_join_merge_dataframe/ for an overview and https://pandas.pydata.org/pandas-docs/stable/merging.html for the full documentation.
To illustrate the concept of joining the information of two dataframes with pandas, let's take a small subset of our
cities2 = cities[cities['name'].isin(['Bern', 'Brussels', 'London', 'Paris'])].copy() cities2['iso_a3'] = ['CHE', 'BEL', 'GBR', 'FRA']
countries2 = countries[['iso_a3', 'name', 'continent']] countries2.head()
We added a 'iso_a3' column to the
cities dataset, indicating a code of the country of the city. This country code is also present in the
countries dataset, which allows us to merge those two dataframes based on the common column.
cities dataframe with
countries will transfer extra information about the countries (the full name, the continent) to the
cities dataframe, based on a common key:
But, for this illustrative example, we added the common column manually, it is not present in the original dataset. However, we can still know how to join those two datasets based on their spatial coordinates.
In the previous notebook 02-spatial-relationships.ipynb, we have seen the notion of spatial relationships between geometry objects: within, contains, intersects, ...
In this case, we know that each of the cities is located within one of the countries, or the other way around that each country can contain multiple cities.
We can test such relationships using the methods we have seen in the previous notebook:
france = countries.loc[countries['name'] == 'France', 'geometry'].squeeze()
The above gives us a boolean series, indicating for each point in our
cities dataframe whether it is located within the area of France or not.
Because this is a boolean series as result, we can use it to filter the original dataframe to only show those cities that are actually within France:
We could now repeat the above analysis for each of the countries, and add a column to the
cities dataframe indicating this country. However, that would be tedious to do manually, and is also exactly what the spatial join operation provides us.
(note: the above result is incorrect, but this is just because of the coarse-ness of the countries dataset)
In this case, we want to join the
cities dataframe with the information of the
countries dataframe, based on the spatial relationship between both datasets.
We use the
joined = geopandas.sjoin(cities, countries, op='within', how='left')
We will again use the Paris datasets to do some exercises. Let's start importing them again:
districts = geopandas.read_file("data/paris_districts_utm.geojson") stations = geopandas.read_file("data/paris_sharing_bike_stations_utm.geojson")
# %load _solved/solutions/03-spatial-joins1.py
# %load _solved/solutions/03-spatial-joins2.py
# %load _solved/solutions/03-spatial-joins3.py
# %load _solved/solutions/03-spatial-joins4.py
# %load _solved/solutions/03-spatial-joins5.py
# %load _solved/solutions/03-spatial-joins6.py
# %load _solved/solutions/03-spatial-joins7.py
In the spatial join operation above, we are not changing the geometries itself. We are not joining geometries, but joining attributes based on a spatial relationship between the geometries. This also means that the geometries need to at least overlap partially.
If you want to create new geometries based on joining (combining) geometries of different dataframes into one new dataframe (eg by taking the intersection of the geometries), you want an overlay operation.
africa = countries[countries['continent'] == 'Africa']
cities['geometry'] = cities.buffer(2)
geopandas.overlay(africa, cities, how='difference').plot()