Get startedGet started for free

Applying custom spatial operations

1. Applying custom spatial operations

Shapely and GeoPandas provide a lot of geospatial functionality out of the box. But, sometimes you may want to do a more specialized spatial query or operation that cannot be readily done with one of the available functions. Luckily, we can still use those functions as building blocks for a custom spatial operation, and apply this to each of the geometries.

2. Total river length within 50 km of each city?

Assume we want to do the following analysis: we want to know the total river length within 50 kilometers of each city, as illustrated with the thicker blue lines on the figure.

3. Total river length within 50 km of each city?

We can write this operation for a single city, here Cairo, by composing several of the spatial methods and attributes we have seen: creating a buffer around the point, then taking the intersection with the rivers, and getting the length of each intersecting linestring and taking the sum of those. If we now want to repeat this analysis for each city, so for each point in the GeoDataFrame, we can use the pandas apply method.

4. The apply() method

The apply method of a Series or GeoSeries can be used to call a function for each of the values of that Series, resulting in a new Series. In practice, the signature looks as the following. The first argument passed to apply is the function that we want to apply. This function should take as first argument the value of the Series on which it is being applied. If your function has additional arguments, you can pass those through as keyword arguments (kwargs) in apply. For a GeoSeries, the function thus takes each of the geometries as first argument. But let's see how that works in practice.

5. Applying a custom spatial operation

Going back to the example of the river length, we can first write the same custom set of operations as a function. This function takes the geometry as the first argument. As an additional argument we have the rivers GeoDataFrame. Calling this function on the single point geometry of Cairo still returns the same result. But now, thanks to apply, we can also broadcast this function to each of the points in the cities dataset.

6. Applying a custom spatial operation

For that, we use the apply method of the GeoSeries, here the geometries of the cities dataset. We pass the function we defined as the first argument, and then specify further keyword arguments that will be passed to the function, in this case to specify the rivers dataframe. This returns a new Series where each value is the result of calling the function on one of the geometries of the cities dataframe.

7. Applying a custom spatial operation

We can then, for example, add the result as a new column. Looking at the first rows, we indeed see a column with the river length in the surroundings of each city.

8. Let's practice!

OK, in this video we have seen how we can turn a custom set of operations into a function, and then easily apply it to each of the geometries. But note that you should only do this if the available methods itself are not sufficient. Let's now practice this on the mining site data!