Given a set of points (with meters coordinates), I am trying to create lines between them. But I don't want to create lines of each pair of points, Iwant to create thoses lines if the distance between two points is inferior or equal to a threshold value (let's say 1000m).

And from there, I am quite lost. The distance() function only returns a GeoSerie of the points with themselves, so all my values are at 0.0. I am also facing issues with the buffer() function, not knowing how to properly use the GeoSerie result once computed.

From what I tested so far:

  • intersection() returns a GeoSerie with only one reference,
  • sjoin does not work with GeoSeries
  • converting the buffer GeoSerie to a GeoDataFrame results in AttributeError: No geometry data set yet (expected in column 'geometry'

Any leads about how to achieve that?


1 Answer 1


Assuming you have one geoseries with Points (for example you want to compare Point 1 to Point 2, Point 1 to Point 3,... ):

import geopandas as gpd
from itertools import combinations
from shapely.geometry import LineString

df = gpd.read_file(r"C:\data\vk_riks_Sweref_99_TM_shape\vagk\riks\js_riks.shp")

series = df.geometry
max_distance = 15000

points_within = []
for i1,i2 in combinations(range(0,series.shape[0]-1),2):  #Iterate over all pairs/combinations of indices
    if 0<series[i1].distance(series[i2])<=max_distance:

geoms = [LineString(pointpair) for pointpair in points_within]
df2 = gpd.GeoDataFrame(geometry=gpd.GeoSeries(geoms))
df2.crs = 'epsg:3006' #Adjust

enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.