Point geometry operations

Functions for point geometries.

snap_all(points, to, *, distance_col=None)[source]

Snaps points to the nearest geometry.

It takes a GeoDataFrame of points and snaps them to the nearest geometry in a second GeoDataFrame. Adds a distance column if specified.

Parameters:
  • points (GeoDataFrame | GeoSeries) – The GeoDataFrame of points to snap.

  • to (GeoDataFrame | GeoSeries) – The GeoDataFrame to snap to.

  • distance_col (str | None) – Name of column with the snap distance. Defaults to None.

Return type:

GeoDataFrame | GeoSeries

Returns:

A GeoDataFrame or GeoSeries with the points snapped to the nearest point in the ‘to’ GeoDataFrame or GeoSeries.

Notes

If there are geometries equally close to the points, one geometry will be chosen as the snap geometry. This will usually only happen with constructed data like grids or in the examples below.

The snap point might be in between vertices of lines and polygons. Convert the ‘to’ geometries to multipoint before snapping if the snap points should be vertices.

Examples:

Create som points.

>>> from sgis import snap_all, to_gdf
>>> points = to_gdf([(0, 0), (1, 1)])
>>> points
                geometry
0  POINT (0.00000 0.00000)
1  POINT (1.00000 1.00000)
>>> to = to_gdf([(2, 2), (3, 3)])
>>> to["snap_idx"] = to.index
>>> to
                geometry  snap_idx
0  POINT (2.00000 2.00000)            0
1  POINT (3.00000 3.00000)            1

Snap all points to closest geometry in ‘to’.

>>> snap_all(points, to)
                geometry  snap_distance
0  POINT (2.00000 2.00000)       2.828427
1  POINT (2.00000 2.00000)       1.414214
snap_within_distance(points, to, max_distance, *, distance_col=None)[source]

Snaps points to nearest geometry if within given distance.

It takes a GeoDataFrame of points and snaps them to the nearest geometry in a second GeoDataFrame if the snap distance is less than ‘max_distance’. Adds a distance column if specified.

Parameters:
  • points (GeoDataFrame | GeoSeries) – The GeoDataFrame of points to snap.

  • to (GeoDataFrame | GeoSeries) – The GeoDataFrame to snap to.

  • max_distance (int | float) – The maximum distance to snap to.

  • distance_col (str | None) – Name of column with the snap distance. Defaults to ‘snap_distance’. Set to None to not get any distance column. This will make the function a bit faster.

Return type:

GeoDataFrame | GeoSeries

Returns:

A GeoDataFrame or GeoSeries with the points snapped to the nearest point in the ‘to’ GeoDataFrame or GeoSeries.

Notes

If there are geometries equally close to the points, one geometry will be chosen as the snap geometry. This will usually only happen with constructed data like grids or in the examples below.

The snap point might be in between vertices of lines and polygons. Convert the ‘to’ geometries to multipoint before snapping if the snap points should be vertices.

Examples:

Create som points.

>>> from sgis import snap_within_distance, to_gdf
>>> points = to_gdf([(0, 0), (1, 1)])
>>> points
                geometry
0  POINT (0.00000 0.00000)
1  POINT (1.00000 1.00000)
>>> to = to_gdf([(2, 2), (3, 3)])
>>> to
                geometry
0  POINT (2.00000 2.00000)
1  POINT (3.00000 3.00000)

Snap ‘points’ to closest geometry in ‘to’ if distance is less 1, 2 and 3.

>>> snap_within_distance(points, to, 1)
                geometry snap_distance
0  POINT (0.00000 0.00000)          <NA>
1  POINT (1.00000 1.00000)          <NA>
>>> snap_within_distance(points, to, 2)
                    geometry snap_distance
0  POINT (0.00000 0.00000)          <NA>
1  POINT (2.00000 2.00000)      1.414214
>>> snap_within_distance(points, to, 3)
                geometry snap_distance
0  POINT (2.00000 2.00000)      2.828427
1  POINT (2.00000 2.00000)      1.414214