How to Calculate Distance Between Two Points with Improved Accuracy Using GeoPy?

Mary-Kate Olsen
Release: 2024-11-05 13:53:02
Original
695 people have browsed it

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)
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template