nearest_neighbors(src_gdf: ~geopandas.geodataframe.GeoDataFrame, candidates_gdf: ~geopandas.geodataframe.GeoDataFrame, method: str, k_neighbors: int | None = None, radius: float | None = None, **kwargs) -> (<class 'numpy.ndarray'>, <class 'numpy.ndarray'>)[source]#

For each point in src_gdf, find the closest point in candidates_gdf and return them with their distances (in the crs coordinates).

Closest points are:

  • if method == k_neighbors: the k closest neighbors

  • if method == radius: the neighbors inside this radius (in the crs coordinates, better done with projected geometries)

  • src_gdf (gpd.GeoDataFrame) – Source geodataframe

  • candidates_gdf (gpd.GeoDataFrame) – Candidates geodataframe

  • method (str) – ‘k_neighbors’ or ‘radius’

  • k_neighbors (int) – Number of neighbors to be looked for

  • radius (float) – Radius in which to find the neighbors

  • **kwargs – Other args for the query


closest samples, distances

Return type:

(np.ndarray, np.ndarray)


>>> from sertit import geometry, vectors
>>> src ="src.shp")
>>> candidates ="candidates.shp")
>>> # There is only one point in the neighborhood of each src, the others are further than 100m
>>> # Radius method
>>> nearest_neighbors(src, candidates, method="radius", radius=100)
[array([13]) array([12]) array([0])], [array([39.62574458]) array([50.37121574]) array([90.98648454])]
>>> # k_neighbors method
>>> nearest_neighbors(src, candidates, method="k_neighbors", k_neighbors=1)
[array([13]) array([12]) array([0])], [array([39.62574458]) array([50.37121574]) array([90.98648454])]