Buffer, dissolve and explode

Functions that buffer, dissolve and/or explodes geometries while fixing geometries.

The functions do the same as the geopandas buffer, dissolve and explode methods, except for the following:

  • Geometries are made valid after buffer and dissolve.

  • The buffer resolution defaults to 30 (geopandas’ default is 16).

  • If ‘by’ is not specified, the index will be labeled 0, 1, …, n - 1 after exploded, instead of 0, 0, …, 0 as it will with the geopandas defaults.

  • index_parts is set to False, which will be the default in a future version of geopandas.

  • The buff function returns a GeoDataFrame, the geopandas method returns a GeoSeries.

buff(gdf, distance, resolution=30, copy=True, join_style='round', **buffer_kwargs)[source]

Buffers a GeoDataFrame with high resolution and returns a new GeoDataFrame.

Parameters:
  • gdf (GeoDataFrame | GeoSeries) – the GeoDataFrame that will be buffered, dissolved and exploded.

  • distance (int | float) – the distance (meters, degrees, depending on the crs) to buffer the geometry by

  • resolution (int) – The number of segments used to approximate a quarter circle. Here defaults to 30, as opposed to the default 16 in geopandas.

  • join_style (int | str) – Buffer join style.

  • copy (bool) – Whether to copy the GeoDataFrame before buffering. Defaults to True.

  • **buffer_kwargs – additional keyword arguments passed to geopandas’ buffer.

Return type:

GeoDataFrame

Returns:

A buffered GeoDataFrame.

buffdiss(gdf, distance, resolution=30, copy=True, n_jobs=1, join_style='round', **dissolve_kwargs)[source]

Buffers and dissolves geometries.

It takes a GeoDataFrame and buffer, fixes, dissolves and fixes geometries. If the ‘by’ parameter is not specified, the index will labeled 0, 1, …, n - 1, instead of 0, 0, …, 0. If ‘by’ is speficied, this will be the index.

Parameters:
  • gdf (GeoDataFrame) – the GeoDataFrame that will be buffered and dissolved.

  • distance (int | float) – the distance (meters, degrees, depending on the crs) to buffer the geometry by

  • resolution (int) – The number of segments used to approximate a quarter circle. Here defaults to 30, as opposed to the default 16 in geopandas.

  • join_style (int | str) – Buffer join style.

  • copy (bool) – Whether to copy the GeoDataFrame before buffering. Defaults to True.

  • n_jobs (int) – Number of threads to use. Defaults to 1.

  • **dissolve_kwargs – additional keyword arguments passed to geopandas’ dissolve.

Return type:

GeoDataFrame

Returns:

A buffered GeoDataFrame where geometries are dissolved.

Examples:

Read points and create reproducible random group labels and numeric values.

>>> import sgis as sg
>>> import numpy as np
>>> points = sg.read_parquet_url(
...     "https://media.githubusercontent.com/media/statisticsnorway/ssb-sgis/main/tests/testdata/points_oslo.parquet"
... )[["geometry"]]
>>> rng = np.random.default_rng(0)
>>> points["group"] = rng.choice([*"abd"], len(points))
>>> points["number"] = rng.random(size=len(points))
>>> print(points.to_string(max_rows=10, max_colwidth=50, show_dimensions=True))
                       geometry group    number
0    POINT (263122.7 6651184.9)     d  0.081324
1    POINT (272456.1 6653369.5)     b  0.875228
2    POINT (270082.3 6653032.7)     b  0.941706
3    POINT (259804.8 6650339.7)     a  0.261864
4    POINT (272876.2 6652889.1)     a  0.012101
..                          ...   ...       ...
995  POINT (266801.7 6647844.5)     d  0.725364
996    POINT (261274 6653593.4)     a  0.687725
997    POINT (263542.9 6645427)     b  0.923277
998    POINT (269226.7 6650628)     a  0.765027
999  POINT (264570.3 6644239.5)     d  0.387705

[1000 rows x 3 columns]

Buffer by 100 meters and dissolve.

>>> print(sg.buffdiss(points, 100).to_string(max_rows=10, max_colwidth=50, show_dimensions=True))
  group    number                                           geometry
0     d  0.081324  MULTIPOLYGON (((256411.714 6649864.787, 256408...

[1 rows x 3 columns]

Dissolve by ‘group’ and get sum of columns.

>>> print(sg.buffdiss(points, 100, by="group", aggfunc="sum").to_string(max_rows=10, max_colwidth=50, show_dimensions=True))
           number                                           geometry
group
a      162.382079  MULTIPOLYGON (((258174.755 6647919.926, 258172...
b      160.033421  MULTIPOLYGON (((258399.355 6647815.126, 258397...
d      175.070618  MULTIPOLYGON (((258219.455 6648068.626, 258217...

[3 rows x 2 columns]

To get the ‘by’ columns as columns, not index.

>>> print(sg.buffdiss(points, 100, by="group", as_index=False).to_string(max_rows=10, max_colwidth=50, show_dimensions=True))
  group    number                                           geometry
0     a  0.261864  MULTIPOLYGON (((258174.755 6647919.926, 258172...
1     b  0.875228  MULTIPOLYGON (((258399.355 6647815.126, 258397...
2     d  0.081324  MULTIPOLYGON (((258219.455 6648068.626, 258217...

[3 rows x 3 columns]
buffdissexp(gdf, distance, *, resolution=30, index_parts=False, copy=True, grid_size=None, join_style='round', n_jobs=1, **dissolve_kwargs)[source]

Buffers and dissolves overlapping geometries.

It takes a GeoDataFrame and buffer, fixes, dissolves, fixes and explodes geometries. If the ‘by’ parameter is not specified, the index will labeled 0, 1, …, n - 1, instead of 0, 0, …, 0. If ‘by’ is speficied, this will be the index.

Parameters:
  • gdf (GeoDataFrame) – the GeoDataFrame that will be buffered, dissolved and exploded.

  • distance (int | float) – the distance (meters, degrees, depending on the crs) to buffer the geometry by

  • resolution (int) – The number of segments used to approximate a quarter circle. Here defaults to 30, as opposed to the default 16 in geopandas.

  • index_parts (bool) – If False (default), the index after dissolve is respected. If True, an integer index level is added during explode.

  • copy (bool) – Whether to copy the GeoDataFrame before buffering. Defaults to True.

  • grid_size (float | int | None) – Rounding of the coordinates. Defaults to None.

  • n_jobs (int) – Number of threads to use. Defaults to 1.

  • join_style (int | str) – Buffer join style.

  • **dissolve_kwargs – additional keyword arguments passed to geopandas’ dissolve.

Return type:

GeoDataFrame

Returns:

A buffered GeoDataFrame where overlapping geometries are dissolved.

buffdissexp_by_cluster(gdf, distance, *, resolution=30, copy=True, n_jobs=1, join_style='round', **dissolve_kwargs)[source]

Buffers and dissolves overlapping geometries.

Works exactly like buffdissexp, but, before dissolving, the geometries are divided into clusters based on overlap (uses the function sgis.get_polygon_clusters). The geometries are then dissolved based on this column (and optionally other columns).

This might be many times faster than a regular buffdissexp, if there are many non-overlapping geometries.

Parameters:
  • gdf (GeoDataFrame) – the GeoDataFrame that will be buffered, dissolved and exploded.

  • distance (int | float) – the distance (meters, degrees, depending on the crs) to buffer the geometry by

  • resolution (int) – The number of segments used to approximate a quarter circle. Here defaults to 30, as opposed to the default 16 in geopandas.

  • join_style (int | str) – Buffer join style.

  • copy (bool) – Whether to copy the GeoDataFrame before buffering. Defaults to True.

  • n_jobs (int) – int = 1,

  • **dissolve_kwargs – additional keyword arguments passed to geopandas’ dissolve.

Return type:

GeoDataFrame

Returns:

A buffered GeoDataFrame where overlapping geometries are dissolved.

diss(gdf, by=None, aggfunc='first', as_index=True, grid_size=None, n_jobs=1, **dissolve_kwargs)[source]

Dissolves geometries.

It takes a GeoDataFrame and dissolves and fixes geometries.

Parameters:
  • gdf (GeoDataFrame) – the GeoDataFrame that will be dissolved and exploded.

  • by (str | Sequence[str] | None) – Columns to dissolve by.

  • aggfunc (str | Callable | dict[str, str | Callable]) – How to aggregate the non-geometry colums not in “by”.

  • as_index (bool) – Whether the ‘by’ columns should be returned as index. Defaults to True to be consistent with geopandas.

  • grid_size (float | int | None) – Rounding of the coordinates. Defaults to None.

  • n_jobs (int) – Number of threads to use. Defaults to 1.

  • **dissolve_kwargs – additional keyword arguments passed to geopandas’ dissolve.

Return type:

GeoDataFrame

Returns:

A GeoDataFrame with dissolved geometries.

diss_by_cluster(gdf, predicate=None, n_jobs=1, **dissolve_kwargs)[source]

Dissolves overlapping geometries through clustering with sjoin and networkx.

Works exactly like dissexp, but, before dissolving, the geometries are divided into clusters based on overlap (uses the function sgis.get_polygon_clusters). The geometries are then dissolved based on this column (and optionally other columns).

This might be many times faster than a regular dissexp, if there are many non-overlapping geometries.

Parameters:
  • gdf (GeoDataFrame) – the GeoDataFrame that will be dissolved and exploded.

  • predicate – Spatial predicate to use.

  • n_jobs (int) – Number of threads to use. Defaults to 1.

  • **dissolve_kwargs – Keyword arguments passed to geopandas’ dissolve.

Return type:

GeoDataFrame

Returns:

A GeoDataFrame where overlapping geometries are dissolved.

dissexp(gdf, by=None, aggfunc='first', as_index=True, index_parts=False, grid_size=None, n_jobs=1, **dissolve_kwargs)[source]

Dissolves overlapping geometries.

It takes a GeoDataFrame and dissolves, fixes and explodes geometries.

Parameters:
  • gdf (GeoDataFrame) – the GeoDataFrame that will be dissolved and exploded.

  • by (str | Sequence[str] | None) – Columns to dissolve by.

  • aggfunc (str | Callable | dict[str, str | Callable]) – How to aggregate the non-geometry colums not in “by”.

  • as_index (bool) – Whether the ‘by’ columns should be returned as index. Defaults to True to be consistent with geopandas.

  • index_parts (bool) – If False (default), the index after dissolve is respected. If True, an integer index level is added during explode.

  • grid_size (float | int | None) – Rounding of the coordinates. Defaults to None.

  • n_jobs (int) – Number of threads to use. Defaults to 1.

  • **dissolve_kwargs – additional keyword arguments passed to geopandas’ dissolve.

Return type:

GeoDataFrame

Returns:

A GeoDataFrame where overlapping geometries are dissolved.

dissexp_by_cluster(gdf, predicate='intersects', n_jobs=1, **dissolve_kwargs)[source]

Dissolves overlapping geometries through clustering with sjoin and networkx.

Works exactly like dissexp, but, before dissolving, the geometries are divided into clusters based on overlap (uses the function sgis.get_polygon_clusters). The geometries are then dissolved based on this column (and optionally other columns).

This might be many times faster than a regular dissexp, if there are many non-overlapping geometries.

Parameters:
  • gdf (GeoDataFrame) – the GeoDataFrame that will be dissolved and exploded.

  • predicate (str | None) – Spatial predicate to use.

  • n_jobs (int) – Number of threads to use. Defaults to 1.

  • **dissolve_kwargs – Keyword arguments passed to geopandas’ dissolve.

Return type:

GeoDataFrame

Returns:

A GeoDataFrame where overlapping geometries are dissolved.