10 examples of 'python calculate distance between two coordinates' in Python

Every line of 'python calculate distance between two coordinates' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
46def distance(x1, y1, x2, y2):
47 """Get the distance between two points."""
48 return ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5
37def _calculate_distance(latlon1, latlon2) :
38 """Calculates the distance between two points on earth.
39 """
40 lat1, lon1 = latlon1
41 lat2, lon2 = latlon2
42 R = 6371 # radius of the earth in kilometers
43 dlon = lon2 - lon1
44 dlat = lat2 - lat1
45 a = np.sin(dlat/2)**2 + np.cos(lat1) * np.cos(lat2) * (np.sin(dlon/2))**2
46 c = 2 * np.pi * R * np.arctan2( np.sqrt(a), np.sqrt(1-a) ) / 180
47 return c
10def distance(x1, y1, x2, y2):
11 """Return the distance between the points (x1, y1) and (x2, y2)"""
12 t1 = (x1 - x2)
13 t2 = (y1 - y2)
14 return math.sqrt(t1 * t1 + t2 * t2)
60def distance(x1, y1, x2, y2):
61 """
62 l2 distance
63 """
64
65 return math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))
22def distance(a,b,x,y):
23 return ((a - x)**2 + (b - y)**2)**0.5
53def _py_distance(point1, point2):
54 '''
55 Calculating great-circle distance
56 (see https://en.wikipedia.org/wiki/Great-circle_distance)
57 '''
58 lon1, lat1 = (radians(coord) for coord in point1)
59 lon2, lat2 = (radians(coord) for coord in point2)
60
61 dlon = fabs(lon1 - lon2)
62 dlat = fabs(lat1 - lat2)
63
64 numerator = sqrt(
65 (cos(lat2)*sin(dlon))**2 +
66 ((cos(lat1)*sin(lat2)) - (sin(lat1)*cos(lat2)*cos(dlon)))**2)
67
68 denominator = (
69 (sin(lat1)*sin(lat2)) +
70 (cos(lat1)*cos(lat2)*cos(dlon)))
71
72 c = atan2(numerator, denominator)
73 return EARTH_MEAN_RADIUS*c
156def calcDistance(a,b): #calculate distance between two points.
157 try:
158 x1, y1 = a
159 x2, y2 = b
160 return math.hypot(x2 - x1, y2 - y1)
161 except Exception as e:
162 print("unable to calculate distance")
207def calculate(self):
208 lat1, lng1 = map(radians, self.a)
209 lat2, lng2 = map(radians, self.b)
210
211 sin_lat1, cos_lat1 = sin(lat1), cos(lat1)
212 sin_lat2, cos_lat2 = sin(lat2), cos(lat2)
213
214 delta_lng = lng2 - lng1
215 cos_delta_lng, sin_delta_lng = cos(delta_lng), sin(delta_lng)
216
217 central_angle = acos(sin_lat1 * sin_lat2 +
218 cos_lat1 * cos_lat2 * cos_delta_lng)
219
220 # From http://en.wikipedia.org/wiki/Great_circle_distance:
221 # Historically, the use of this formula was simplified by the
222 # availability of tables for the haversine function. Although this
223 # formula is accurate for most distances, it too suffers from
224 # rounding errors for the special (and somewhat unusual) case of
225 # antipodal points (on opposite ends of the sphere). A more
226 # complicated formula that is accurate for all distances is: (below)
227
228 d = atan2(sqrt((cos_lat2 * sin_delta_lng) ** 2 +
229 (cos_lat1 * sin_lat2 -
230 sin_lat1 * cos_lat2 * cos_delta_lng) ** 2),
231 sin_lat1 * sin_lat2 + cos_lat1 * cos_lat2 * cos_delta_lng)
232
233 self.radians = d
242def distance(x1,y1,z1,x2,y2,z2):
243 a=x2-x1
244 b=y2-y1
245 c=z2-z1
246 return math.sqrt(a*a+b*b+c*c)
12def euclidean_distance(x1, x2, y1, y2):
13 """ Calculate Euclidean distance from (x1,y1) to (x2,y2). """
14
15 return math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)

Related snippets