Get startedGet started for free

Shapely geometries and spatial relationships

1. Shapely geometries and spatial relationships

Hi I'm Dani Arribas-Bel, and I'll walk you through the next chapter!

2. Scalar geometry values

In the previous chapter, we have worked with GeoDataFrames of the geopandas library. For example, the cities dataframe. We have seen that there is a geometry column, holding the vector features, in this case consisting of point geometries. But what are the values in this column? That is what we will explore in this video. Let's extract one of the values of the geometry column, using the loc attribute of a dataframe. This lets us access a single value based on its row and column label. Here we extract the value for the row with label 170 and the column geometry, which I know is the row representing the city of Brussels.

3. Scalar geometry values

If we print this value, we can see it is a Point geometry. And when checking the type of this value, we see it is a shapely point object.

4. The Shapely python package

Shapely is a python package to work with geometric objects. It provides those Point, LineString and Polygon geometry objects, and is used by GeoPandas under the hood. The geometry column of a GeoDataFrame, which is a GeoSeries, thus consists of shapely objects.

5. Geometry objects

We can access those geometry objects from a GeoDataFrame, same as we would for any single value in a pandas DataFrame, be it a number, a date, or any other object type. Here, we access Brussels, Paris, and the polygons for France, Belgium and the United Kingdom from the countries dataset, so we can use them in the examples next. But geometries can also be created manually. For example, here's how to create a Point geometry with coordinates 1 and 2.

6. Spatial methods

Shapely geometries come with spatial functionality built-in. For example, a Polygon has an area attribute. Just like we have seen before for the GeoDataFrame. Another example is the distance method, which calculates the distance between two geometries. Here, we calculate the distance, in a straight line, between Brussels and Paris. We will see in the next chapters in which unit this is expressed. And Shapely supports many more similar methods out-of-the-box!

7. Spatial relationships

Beyond methods for a single object, shapely allows us to spatially relate different geometries. Let's take a look at our example objects. Since shapely has no method to visualize multiple geometries, we can quickly put the geometries in a GeoSeries and plot that. We see here the shapes of France, United Kingdom and Belgium, the points representing Paris and Brussels, and a line connecting both cities. Using this figure, we can illustrate spatial relations between objects. For example, Brussels is located "within" the polygon of Belgium; or, the other way around, Belgium *contains* the point of Brussels. The Belgian border "touches" that of France, so we can consider them neighbours. The line between between paris and brussels *intersects* the two countries, but does not intersect the polygon of the United Kingdom. These spatial relations are important, and are one of the aspects that makes spatial data special.

8. Spatial relationships

Now, let's see how what we've just covered in words translates directly into code! Checking that Belgium indeed contains brussels return True. On the other hand, asking if France contains Brussels correctly gives False. We are using here the contains method of the geometry, and as argument we pass the other geometry for which we want to check a certain relation. Also the other spatial relationships mentioned in the previous slide can be coded with the within, touches, intersects methods.

9. Let's practice!

Now we know the concepts, let's practice with real world data!