GeoPandas – Creating LINESTRING from Two POINTs and Finding Mid-POINT

geodataframegeopandaslinestringpoints-to-line

I would like to create a LINESTRING from two POINT geometries and then determine the mid-POINT. So, from my original Pandas' DataFrame, with "x" and "y" columns, I created the following GeoPandas' DataFrame:

zone_short_edges = 
 id  vertex_id                    fr_point  \
1  A1                2  POINT (119.79008 28.35047)   
3  A1                4  POINT (122.85067 44.85106)   
5  A2                1  POINT (138.79141 26.48802)   
7  A2                3  POINT (141.73386 44.89716)   

                     to_point  seg_length  
1  POINT (122.85067 28.08433)    3.072140  
3  POINT (119.92314 44.71798)    2.930553  
5  POINT (141.92247 26.26168)    3.139230  
7  POINT (138.79141 44.89716)    2.942450 

where fr_point and to_point are dtype = geometry.

Now, I am facing two choices:

  1. Create a LINESTRING for each pair and then finding that linestrings midpoint.
  2. Finding the midpoint (what I actually want to the possibility to find all the POINT(s) that divide the segment into N segments of equal length).

I tried doing this:

geometry = [xy for xy in zip(zone_short_edges.fr_point, zone_short_edges.to_point)]
LineString([geometry]).wkt

but that was a clear failure.

Any ideas that might put me in the right direction or do I really need to go to my original DataFrame?

Best Answer

Try something like:

import geopandas as gpd
from shapely.geometry import LineString

df['line'] = df.apply(lambda row: LineString([row['fr_point'], row['to_point']]), axis=1) #Create a linestring column
df['midpoint'] = df.apply(lambda row: row['line'].centroid, axis=1) #Find centroid

df.head().to_clipboard()

enter image description here