I have a database of 2000 points, which is in CSV format containing latitude and longitude. I want to find the distance between all these points, and then show these points in a map by using pyQGIS; I have seen one example using Fiona but I want to do this in pyQGIS.
I have used following code:
def offset(iterable): prev = None for elem in iterable: yield prev, elem prev = elem import csv import math def haversine(lon1, lat1, lon2, lat2): """ Calculate the great circle distance between two points on the earth (specified in decimal degrees). Source: http://gis.stackexchange.com/a/56589/15183 """ # convert decimal degrees to radians lon1, lat1, lon2, lat2 = map(math.radians, [lon1, lat1, lon2, lat2]) # haversine formula dlon = lon2 - lon1 dlat = lat2 - lat1 a = math.sin(dlat/2)**2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon/2)**2 c = 2 * math.asin(math.sqrt(a)) km = 6367 * c return km with open('1.csv', 'rb') as f1: reader1 = csv.reader(f1) header1 = reader1.next() with open('2.csv', 'rb') as f2: reader2 = csv.reader(f2) header2 = reader2.next() for row1 in offset(header1): for row2 in offset(header2): floats1 = map(float, row1[1:]) floats2 = map(float, row2[1:]) print(floats1) print(floats2) print haversine(floats1,floats1,floats2,floats2)
Traceback (most recent call last): File "<input>", line 13, in <module> IndexError: list index out of range
My .csv file contains following data with about 2000 data pointt:
ID LAT LONG 1 12.953578 77.592453 2 12.953511 77.592445 3 12.953145 77.593147 4 12.951835 77.594612