[GIS] Getting latitudes and longitudes in between start and end coordinates and spaced at equal distance

coordinate systemcsvlatitude longitudepythonpython 3

I have a csv file which has the latitude and longitudes values , I need to find the coordinates in between the start and the end latitude and longitude which must be equally spaced at the interval of 10 meters.(to refer here)

The csv file is as follows:

 id latitude longitude
  0   2.456     4.566
  1   3.457     6.455
  2   4.890     7.787
  3   5.679     9.852

From the given csv the start lat and long values are of id 0 and end coordinates are of id 1 .then for second time the start coordinates must be of id 2 and the end point must be of id 3 and so on. These must iteratively take the values from the csv and find the in-between coordinates from the start and end latitude and longitude values.

The output must be in this format:

 latitude longitude in_between_lat in_between_lon
   2.456     4.566     2.456          4.566
                       2.466          4.576
                       2.476          4.586
                       2.486          4.596
   3.457     6.455     3.457          6.455
   4.809     7.787     4.809          7.787 
                       4.819          7.797
                       4.829          7.807
   5.679     9.852     5.679          9.852

From these we can conclude the in-between coordinates between start point 2.456, 4.566 and end points 3.457,6.455 and so on (here the values taken for the in-between lat and long are random).

Best Answer

import geopandas as gp
import shapely
import pandas as pd
%matplotlib inline

#your original data
d = [(2.456,4.566),
(3.457,6.455),
(4.890,7.787),
(5.679,9.852)]

#making a dataframe and then geodataframe
df = pd.DataFrame(data=d, columns=['LAT', 'LON'])
gf = gp.GeoDataFrame(df)

#create a shapely point feature from lat and lon
gf['point'] = gf.apply(lambda x: shapely.geometry.Point(x['LON'], x['LAT']), axis=1)

#create following point needed by shifting rows
gf['shift_point'] = gf['point'].shift(-1)

#drop end row without a next value
gf = gf.dropna()

#create a line between current point and next point
gf['line'] = gf.apply(lambda x: shapely.geometry.LineString([x['point'], x['shift_point']]), axis=1)

#set crs and then covert to projected coordinate system as we'll be dealing meters
gf.crs = {'init' :'epsg:4326'}
gf = gf.set_geometry('line')

gf = gf.to_crs(epsg=3857)

gf.plot()

#get the length of the line value
gf['length'] = gf.line.length
gf['length_10'] = gf['length'] / 10

#iterate through the dataframe, bad practice but hey ho
#here we generate a point on each line, that is "lp" distance along the line
#this lp value increases with each iteration
#the range amount will be dependent on the length of the line / by the step size
#every step the interpolation will increase by 10
#append the indexes and point values to lists

ix = []
basket = []

for i,r in tqdm(gf.iterrows()):
    lp = 0
    for v in range(0, 10 + 1):
        lp += r.length_10
        ix.append(i)
        basket.append(r.line.interpolate(lp))

#we then create another dataframe from these two lists and then join back onto the original line dataframe on the index
joined = gf.join(pd.DataFrame({'interpolations':basket}, index=ix))
joined = joined.set_geometry('interpolations')
joined = joined.to_crs(epsg=4326)
joined.plot()

joined['int_x'] = joined.interpolations.x
joined['int_y'] = joined.interpolations.y

joined = joined[['LAT', 'LON', 'int_x', 'int_y']]
joined.to_csv('path.csv')

First .plot() to show lines

Points set at a much more distant interpolation than 10m, to get an idea