# Find in between coordinates for the given latitude and longitude from a CSV file

I have a CSV file which has the latitude and longitude values for the start and the end coordinates as follows. In these the first lon and lat values are the start point then the second coordinates are the end points for one value. Then the third point is the start and the forth is the end and so on. The csv is in the following format

``````lat    lon
1.25   6.45
2.45   5.36
3.67   9.67
3.56   6.25
``````

The code which I tried is:

``````def main(interval,azimuth,lat1,lng1,lat2,lng2):
'''returns every coordinate pair inbetween two coordinate
pairs given the desired interval'''

d = getPathLength(lat1,lng1,lat2,lng2)
remainder, dist = math.modf((d / interval))
counter = float(interval)
coords = []
coords.append([lat1,lng1])
for distance in range(0,int(dist)):
coord = getDestinationLatLong(lat1,lng1,azimuth,counter)
counter = counter + float(interval)
coords.append(coord)
coords.append([lat2,lng2])
return coords

if __name__ == "__main__":
#point interval in meters
interval = 10
#direction of line in degrees
#start point

lat1 =  18.750000000
lng1 =     85.416666667
#end point
lat2 =  17.650000000
lng2 =  83.250000000
azimuth = calculateBearing(lat1,lng1,lat2,lng2)
#print(azimuth)
azimuth
coords = main(interval,azimuth,lat1,lng1,lat2,lng2)
#print(coords)
coords
``````

This code takes the coordinates manually but I need it to take values from CSV and generate the output as CSV.

• you can use the built-in `csv module` for this. Jul 19, 2019 at 10:06
• Can you show us a part of the csv you have? Jul 19, 2019 at 10:27
• yes....I have now added the part of the csv file@ umbe1987 Jul 19, 2019 at 11:27

Although I've not tested it, this script could work or at least give you an idea of what you should do in order to achieve what you want.

Hope it helps you:

``````import csv

# adjust as you need (the spaces between your lat/long coords in each row)
my_delimiter = '   '

#open input csv in read mode
input_csv = open('yourfile.csv', 'r', newline='')

# open output csv in append mode
output_csv = open('yourfile.csv', 'a', newline='')

coordwriter = csv.writer(output_csv, delimiter=my_delimiter)

# iterate over each row in the input csv as a list of srings
# e.g. first line is: [1.25, 6.45]
next(row) # skip first line (headers)
# do this for the start coordinates
if count % 2 == 0:
lat1 = float(row[0])
lng1 = float(row[1])
# this should be your end coordinates
if not count % 2 == 0:
lat2 = float(row[0])
lng2 = float(row[1])
azimuth = calculateBearing(lat1,lng1,lat2,lng2)
coords = main(interval,azimuth,lat1,lng1,lat2,lng2)

# write the coords in the output csv
coordwriter.writerow(coords)
``````
• i am getting the error as TypeError: must be real number, not str.......@umbe1987 Jul 21, 2019 at 7:57
• I edited the code to cast the coordinates retrieved from the csv as strings to float numbers. Please, feel free to try the updated code and report any error back. Cheers. Jul 22, 2019 at 7:09
• hey i am getting the ValueError: could not convert string to float: 'lat' now@umbe1987 Jul 22, 2019 at 8:10
• The code I provided skips the first row in your csv and then assumes that the rest of the lines are made of numbers (as in your provided example). The error you get makes me think that yo might have other stuff (like strings, as 'lat') wihin your lines. If it's like that, then the code could not work. Jul 22, 2019 at 8:16
• ya the lat and lon values are string@umbe1987 Jul 22, 2019 at 9:09