Get startedGet started for free

Spatial operations: creating new geometries

1. Spatial operations: creating new geometries

In the previous chapter we have seen how to identify and use the spatial relationships between geometries. In this video, we will see how to create new geometries based on those relationships.

2. Spatial operations

Consider the following two geometries, a and b. Two circles that overlap partly, indicated by the darker grey area. Using those two polygons, we can create new geometries based on their different spatial relationships.

3. Spatial operations: intersection

The intersection is the overlapping section of A and B. Here, the intersection is a new polygon, indicated by the blue area. The python code to perform this operation is shown below the figure. Given that a and b are shapely geometries, we can derive the area in blue with the intersection method.

4. Spatial operations: union

A second possibility is the union of both geometries: the overlapping and non-overlapping areas of both geometries are combined into a single geometry, indicated in blue. Again, the python code to perform this is shown, and here the union method can be used.

5. Spatial operations: difference

We can also create a new geometry for the part of A that is not overlapping with B, indicated in blue. This operation is called the difference of a and b.

6. Spatial operations with GeoPandas

In the previous slides we showed how to create a new geometry based on two individual shapely geometries. The same operations can be extended to GeoPandas. Given a GeoDataFrame, we can calculate the intersection, union or difference of each of the geometries with another geometry. Let's look at an example with a subset of the countries. We have a GeoDataFrame with the country polygons of Africa.

7. Spatial operations with GeoPandas

Now consider we also have a rectangular polygon, representing an area around the equator, plotted in blue.

8. Spatial operations with GeoPandas

The intersection method of the GeoDataFrame will now calculate the intersection with the rectangle for each of the geometries of the africa GeoDataFrame element-wise. Note that for many of the countries, those that do not overlap with the rectangle, this will be an empty geometry.

9. Spatial operations with GeoPandas

What is returned is a new GeoSeries of the same length as the original dataframe, containing one row per country, but now containing only the intersection. In this example, the last element shown is an empty polygon, as that country was not overlapping with the box.

10. Let's practice!

So in this video, we saw a set of spatial operations that create new geometries, both for individual shapely geometries as element-wise for a full GeoDataframe. Let's practice!