geopandas.GeoSeries.frechet_distance#

GeoSeries.frechet_distance(other, align=None, densify=None)[source]#

Returns a Series containing the Frechet distance to aligned other.

The Fréchet distance is a measure of similarity: it is the greatest distance between any point in A and the closest point in B. The discrete distance is an approximation of this metric: only vertices are considered. The parameter densify makes this approximation less coarse by splitting the line segments between vertices before computing the distance.

Fréchet distance sweep continuously along their respective curves and the direction of curves is significant. This makes it a better measure of similarity than Hausdorff distance for curve or surface matching.

The operation works on a 1-to-1 row-wise manner:

../../../_images/binary_op-01.svg
Parameters:
otherGeoSeries or geometric object

The Geoseries (elementwise) or geometric object to find the distance to.

alignbool | None (default None)

If True, automatically aligns GeoSeries based on their indices. If False, the order of elements is preserved. None defaults to True.

densifyfloat (default None)

A value between 0 and 1, that splits each subsegment of a line string into equal length segments, making the approximation less coarse. A densify value of 0.5 will add a point halfway between each pair of points. A densify value of 0.25 will add a point every quarter of the way between each pair of points.

Returns:
Series (float)

Examples

>>> from shapely.geometry import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
...     [
...         Polygon([(0, 0), (1, 0), (1, 1)]),
...         Polygon([(0, 0), (-1, 0), (-1, 1)]),
...         LineString([(1, 1), (0, 0)]),
...         Point(0, 0),
...     ],
... )
>>> s2 = geopandas.GeoSeries(
...     [
...         Polygon([(0.5, 0.5), (1.5, 0.5), (1.5, 1.5), (0.5, 1.5)]),
...         Point(3, 1),
...         LineString([(1, 0), (2, 0)]),
...         Point(0, 1),
...     ],
...     index=range(1, 5),
... )
>>> s
0      POLYGON ((0 0, 1 0, 1 1, 0 0))
1    POLYGON ((0 0, -1 0, -1 1, 0 0))
2               LINESTRING (1 1, 0 0)
3                         POINT (0 0)
dtype: geometry
>>> s2
1    POLYGON ((0.5 0.5, 1.5 0.5, 1.5 1.5, 0.5 1.5, ...
2                                          POINT (3 1)
3                                LINESTRING (1 0, 2 0)
4                                          POINT (0 1)
dtype: geometry

We can check the frechet distance of each geometry of GeoSeries to a single geometry:

>>> point = Point(-1, 0)
>>> s.frechet_distance(point)
0    2.236068
1    1.000000
2    2.236068
3    1.000000
dtype: float64

We can also check two GeoSeries against each other, row by row. The GeoSeries above have different indices. We can either align both GeoSeries based on index values and use elements with the same index using align=True or ignore index and use elements based on their matching order using align=False:

../../../_images/binary_op-02.svg
>>> s.frechet_distance(s2, align=True)
0         NaN
1    2.121320
2    3.162278
3    2.000000
4         NaN
dtype: float64
>>> s.frechet_distance(s2, align=False)
0    0.707107
1    4.123106
2    2.000000
3    1.000000
dtype: float64

We can also set a densify value, which is a float between 0 and 1 and signifies the fraction of the distance between each pair of points that will be used as the distance between the points when densifying.

>>> l1 = geopandas.GeoSeries([LineString([(0, 0), (10, 0), (0, 15)])])
>>> l2 = geopandas.GeoSeries([LineString([(0, 0), (20, 15), (9, 11)])])
>>> l1.frechet_distance(l2)
0    18.027756
dtype: float64
>>> l1.frechet_distance(l2, densify=0.25)
0    16.77051
dtype: float64