geopandas.GeoSeries.difference#

GeoSeries.difference(other, align=True)[source]#

Returns a GeoSeries of the points in each aligned geometry that are not in other.

../../../_images/binary_geo-difference.svg

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 difference to.

alignbool (default True)

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

Returns:
GeoSeries

Examples

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

We can do difference of each geometry and a single shapely geometry:

../../../_images/binary_op-03.svg
>>> s.difference(Polygon([(0, 0), (1, 1), (0, 1)]))
0       POLYGON ((0 2, 2 2, 1 1, 0 1, 0 2))
1         POLYGON ((0 2, 2 2, 1 1, 0 1, 0 2))
2                       LINESTRING (1 1, 2 2)
3    MULTILINESTRING ((2 0, 1 1), (1 1, 0 2))
4                                 POINT EMPTY
dtype: geometry

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 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
>>> s.difference(s2, align=True)
0                                        None
1         POLYGON ((0 2, 2 2, 1 1, 0 1, 0 2))
2    MULTILINESTRING ((0 0, 1 1), (1 1, 2 2))
3                            LINESTRING EMPTY
4                                 POINT (0 1)
5                                        None
dtype: geometry
>>> s.difference(s2, align=False)
0         POLYGON ((0 2, 2 2, 1 1, 0 1, 0 2))
1    POLYGON ((0 0, 0 2, 1 2, 2 2, 1 1, 0 0))
2    MULTILINESTRING ((0 0, 1 1), (1 1, 2 2))
3                       LINESTRING (2 0, 0 2)
4                                 POINT EMPTY
dtype: geometry