geopandas.GeoSeries.shared_paths#

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

Returns the shared paths between two geometries.

Geometries within the GeoSeries should be only (Multi)LineStrings or LinearRings. A GeoSeries of GeometryCollections is returned with two elements in each GeometryCollection. The first element is a MultiLineString containing shared paths with the same direction for both inputs. The second element is a MultiLineString containing shared paths with the opposite direction for the two inputs.

You can extract individual geometries of the resulting GeometryCollection using the GeoSeries.get_geometry() method.

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 shared paths with. Has to contain only (Multi)LineString or LinearRing geometry types.

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.

Returns:
GeoSeries

Examples

>>> from shapely.geometry import LineString, MultiLineString
>>> s = geopandas.GeoSeries(
...     [
...         LineString([(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)]),
...         LineString([(1, 0), (2, 0), (2, 1), (1, 1), (1, 0)]),
...         MultiLineString([[(1, 0), (2, 0)], [(2, 1), (1, 1), (1, 0)]]),
...     ],
... )
>>> s
0             LINESTRING (0 0, 1 0, 1 1, 0 1, 0 0)
1             LINESTRING (1 0, 2 0, 2 1, 1 1, 1 0)
2    MULTILINESTRING ((1 0, 2 0), (2 1, 1 1, 1 0))
dtype: geometry

We can find the shared paths between each geometry and a single shapely geometry:

../../../_images/binary_op-03.svg
>>> l = LineString([(1, 1), (0, 1)])
>>> s.shared_paths(l)
0    GEOMETRYCOLLECTION (MULTILINESTRING ((1 1, 0 1...
1    GEOMETRYCOLLECTION (MULTILINESTRING EMPTY, MUL...
2    GEOMETRYCOLLECTION (MULTILINESTRING EMPTY, MUL...
dtype: geometry

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

../../../_images/binary_op-02.svg
>>> s2 = geopandas.GeoSeries(
...     [
...         LineString([(1, 1), (0, 1)]),
...         LineString([(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)]),
...         LineString([(1, 0), (2, 0), (2, 1), (1, 1), (1, 0)]),
...     ],
...     index=[1, 2, 3]
... )
>>> s.shared_paths(s2, align=True)
0                                                 None
1    GEOMETRYCOLLECTION (MULTILINESTRING EMPTY, MUL...
2    GEOMETRYCOLLECTION (MULTILINESTRING EMPTY, MUL...
3                                                 None
dtype: geometry
>>>
>>> s.shared_paths(s2, align=False)
0    GEOMETRYCOLLECTION (MULTILINESTRING ((1 1, 0 1...
1    GEOMETRYCOLLECTION (MULTILINESTRING EMPTY, MUL...
2    GEOMETRYCOLLECTION (MULTILINESTRING ((1 0, 2 0...
dtype: geometry