0

I am a complete newbie to this as you will see. 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                    from \
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    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 = geomtry.

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

I also tried the solution provided here: https://stackoverflow.com/a/66494934/5363686

But, both failed.

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

1 Answers1

1

if your goal is to get the midpoint then you can use some mathematics instead. It will also save you some running time I believe... I haven't touched python-gis in a while but maybe this will help you.
pseudo code:

# lat1,lat2,lon1,lon2 should be in radian
Bx = cos(lat2) * cos(lon2-lon1);
By = cos(lat2) * sin(lon2-lon1);

latMid = atan2(sin(lat1) + sin(lat2), 
             sqrt( (cos(lat1)+Bx)*(cos(lat1)+Bx) + By*By ) );
lonMid = lon1 + atan2(By, cos(lat1) + Bx);

you didn't specify your coordinate system so the calculations could be wrong, a quick search should give you the right solution. Also, this exchange could help you as well How to calculate the midpoint of several geolocations in python

Shaked
  • 93
  • 1
  • 9