[GIS] returning a LineString on selecting its midpoint

linestringpointpythonshapely

I have a list of shapely LineString,

[<shapely.geometry.linestring.LineString object at 0x000001614D750CC0>, <shapely.geometry.linestring.LineString object at 0x000001614D750D68>, <shapely.geometry.linestring.LineString object at 0x000001614D750D30>]

and List of point geometry which are the mid-points of the Linestrings;

[<shapely.geometry.point.Point object at 0x000001614DC0D518>, <shapely.geometry.point.Point object at 0x000001614DC0D4E0>, <shapely.geometry.point.Point object at 0x000001614DCF9CC0>]

How to return Linestring on selecting its midpoint from the second list?

Best Answer

if your linestring are not crossing each other on the mid points you can simply use intersects method from shapely

for line in lines:
     for point in points:
          if line.intersects(point):
                # you have a match

if the do intersect you can get first the mid points from the linestrings using shapely's method interpolate

mid_point = line.interpolate(0.5, normalized = True)

then you use intersects , note that these methods require that your points are exactly the mid points of your lines, if you're not sure about it you can use a buffer with a tolerance distance ( also available in shapely )