0

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).

1
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

  • i dont want the plot for the coordinates,I need the in-between cordinates to be saved in the csv.@saph_top – arpita Jul 21 at 6:14
  • can u help me with these @PMK – arpita Jul 21 at 6:16
  • @arpita the plots were really for demonstrative purposes. all the points are in the dataframe. edited the code so its saves to csv – saph_top Jul 21 at 8:44
  • It provides me the coordinates but does not consider the radius of the earth while iproviding the in-between cordinates, can u help me with these .....@saph_top – arpita Jul 21 at 9:00
  • gis.stackexchange.com/questions/157693/… @saph_top – arpita Jul 21 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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