Commits

Thejesh GN  committed 5a2b066

added initial code for calculating distance between points.

  • Participants
  • Parent commits 96a8a75

Comments (0)

Files changed (2)

File gis/distance_bw_points/getpoints.py

+'''Get the points within a range of points.'''
+__version__ = "0.0.0"
+__author__ = "[http://thejeshgn.com Thejesh GN]"
+import getopt, sys, csv, math
+
+
+kmPerLat = 60.00721 * 1.15078 * 1.609344
+kmPerLong = 60.10793 * 1.15078 * 1.609344
+rad = math.pi / 180.0
+R = 6371 # radius of earth in km
+
+def usage():
+	print "python getpoints.py input_csv distance_in_km"	
+	print "Example:"	
+	print "python getpoints.py points.csv 2"	
+
+def main():
+    try:
+        opts, args = getopt.getopt(sys.argv[1:], "ho:v", ["help", "output="])
+    except getopt.GetoptError, err:
+        print str(err) 
+        usage()
+        sys.exit(2)
+    output = None
+    verbose = False
+    for o, a in opts:
+        if o == "-v":
+            print __version__
+        elif o in ("-h", "--help"):
+            usage()
+            sys.exit()
+        else:
+            assert False, "unhandled option"
+
+# Implementing form Haversine formula
+# http://williams.best.vwh.net/avform.htm#Dist
+# http://en.wikipedia.org/wiki/Haversine_formula
+def distance(src_lat, src_long, dst_lat, dst_long):
+	dlat = (dst_lat - src_lat) * rad
+	dlon = (dst_long - src_long) * rad
+	lat1 = src_lat*rad;
+	lat2 = dst_lat*rad;
+	a = math.sin(dlat/2) * math.sin(dlat/2) + math.sin(dlon/2) * math.sin(dlon/2) * math.cos(lat1) * math.cos(lat2); 
+ 	c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a)); 
+ 	d = R * c;
+ 	print d
+ 	return d
+
+
+if __name__ == "__main__":
+	main()
+	csvReader = csv.reader(open(sys.argv[1], 'rb'), delimiter=',', quotechar='"')
+	for src_row in csvReader:
+		csvReader2 = csv.reader(open(sys.argv[1], 'rb'), delimiter=',', quotechar='"')	
+		csvWriter = csv.writer(open(src_row[0]+src_row[1]+'.csv', 'wb'), delimiter=',', quotechar='"')
+		for dst_row in csvReader2:
+			d = distance(float(src_row[0]), float(src_row[1]), float(dst_row[0]), float(dst_row[1]))
+			if d <= float(sys.argv[2]):
+				csvWriter.writerow([dst_row[0], dst_row[1], d ])
+
+

File gis/distance_bw_points/points.csv

+12.9715987,77.5945627
+12.94634,77.604675
+12.95437,77.570343
+12.99184,77.57171