Pycon.de: spherical geometries in spherely and geopandas - Joris Van den Bossche

Tags: pycon, python

(One of my summaries of the 2025 pycon.de conference in Darmstadt, DE).

The earth is no longer flat, at least in the spherely/geopandas world :-)

In shapely and geopandas, latitude/longitude are normally treated as rectangular x/y coordinates. This is incorrect as the earth is round. So distances you calculate will be wrong.

A solution is to work with projected coordinates. So if you use a local projection (like “Lambert 72” for Belgium), your calculations will be perfectly fine within that region. The projected “flat” coordinates will be good enough.

Why don’t we use projected coordinates for everything? Well, because it isn’t possible. You get distortion, like the regular “mercator” projection that dramatically enlarges Greenland, for instance. Data that’s near the poles also gets funny. Oh, and if you have data on Fiji, which is around 180 degrees longitude, your data might be projected partially on the left and partially on the right side of the map…

Well, what is the shape of the earth? You can treat it as a perfect sphere. That’s a useful approximation, which is also used in geopandas. But, yes, it is an approximation: the earth is actually more a spheroid. But a perfect sphere is good enough for most applications.

s2geometry is an open source geometry library (made by google) that’s useful. Spherely is a python binding for s2geometry, intended to be familiar to shapely users. It was made with PyBind11. It tries to use a numpy vector interface like shapely 2.0.

It is still in an early stage of development, but most of the basic features are already implemented and work just fine. Some functionality that is missing (April 2025): buffer, distance-within, bounds, project/interpolate. Spatial indexing should be faster. (Note: the geopandas integration is almost ready, but it has not been released yet).

He showed some examples where spherely gave the right answer (and shapely the wrong one).

An advantage of using the spherical method directly is that it is faster than first converting the data to a local coordinate system.

https://reinout.vanrees.org/images/2025/pycon-17.jpeg

Photo explanation: picture from the recent “on traxs” model railway exhibition in Utrecht (NL)

 
vanrees.org logo

Reinout van Rees

My name is Reinout van Rees and I program in Python, I live in the Netherlands, I cycle recumbent bikes and I have a model railway.

Weblog feeds

Most of my website content is in my weblog. You can keep up to date by subscribing to the automatic feeds (for instance with Google reader):