Home > Article > Backend Development > How to Calculate Distance Between Two Points with Improved Accuracy Using GeoPy?
Determining Distance Based on Latitude and Longitude
In an attempt to calculate the distance between two points using latitude and longitude, you have stumbled upon a discrepancy in your code. While the haversine formula implemented in your script provides an approximate result, it has been deprecated in GeoPy 1.13 and beyond. For improved accuracy, consider employing geopy.distance.distance().
Utilizing Geopy for Precise Distance Calculation
To utilize the Vincenty distance, which accounts for the Earth's ellipsoid shape, you can leverage geopy.distance. For instance, the following code snippet demonstrates how to calculate the distance between two points:
import geopy.distance coords_1 = (52.2296756, 21.0122287) coords_2 = (52.406374, 16.9251681) print(geopy.distance.geodesic(coords_1, coords_2).km)
This code will accurately compute the distance in kilometers using the WGS-84 ellipsoid as the default model. Geopy also offers alternative distance units such as miles and various other measures.
The above is the detailed content of How to Calculate Distance Between Two Points with Improved Accuracy Using GeoPy?. For more information, please follow other related articles on the PHP Chinese website!