1. Spatial relationships with GeoPandas
Now we know a bit about spatial relationships, let's explore how powerful these features can be!
To do this, we will extend the concept from shapely objects to GeoDataFrames, which provide streamlined access to this functionality.
2. Element-wise spatial relationship methods
Previously, we have learned how to check for certain spatial relationships between two individual geometry objects.
For example, this allows us to see if Brussels is located within France, or if Paris is located in France.
3. Element-wise spatial relationship methods
But, if we want to check for the same relationship between many observations, for example for all cities, trying one by one manually is not ideal.
4. Element-wise spatial relationship methods
To help us in these cases, the GeoDataFrame has similar methods that are automatically applied to all of its geometries. In this example, we are checking which cities are *within* the polygon of France.
The result of this operation is a pandas Series storing boolean objects and indicating, for each point in the cities geodataframe, whether it is located within the polygon of France or not.
What the geodataframe is doing for us is equivalent to applying the within() method to every single geometry in the cities geodataframe.
5. Filtering by spatial relation
Because the result is a boolean series, we can directly use it as a "mask" to filter the original dataframe as we would with a non-spatial dataframe, selecting those cities that are located within France.
For this dataset, there are three cities located in France. Note that this was a dataset with simplified geometries. Hence Andorra and Monaco, which are small independent states and not officially part of France, also fall within the polygon. In general, the filtering pattern allows us to select rows in a dataframe based on a spatial condition, and this is very powerful.
6. Filtering by spatial relation
Let's bring "filtering by spatial relation" home with another example. Take the dataset with large rivers of the world and let's extract the line geometry of the Amazon river in South America.
We could now ask the question "through which countries does the river flow?". To obtain an answer, we can check which countries cross or intersect the river.
We first extract the row corresponding to the Amazon, and create a mask with the intersects operation.
7. Filtering by spatial relation
If we use the resulting mask as a filter for the countries geodataframe itself, we arrive at the answer: the Amazon river crosses Peru, Colombia and Brazil.
By now, we have seen the within, contains, and intersects spatial relationships. These are arguably the most widely used ones, but not the only ones. If you want to learn more about all the methods available within shapely and geopandas, we recommend to browse through the shapely documentation.
8. Shapely & GeoPandas
A final remark before we jump on the exercises. Notice the pattern we have seen in this chapter up to now:
almost every method available on an individual shapely geometry
has its equivalent method on a GeoDataFrame, where it is automatically applied element-wise using each of the geometries stored in the geodataframe.
9. Let's practice!
But let's now query the Paris datasets with some spatial conditions in the exercises.