7

I am trying to get senseful results in my Python script. I want to measure distance between features in a shapefile. I'm working with the SRS: GCS_WGS_1984, so my units are in degrees. But for the user, it would be better to have these units in meters, for more comprehension.
How can I convert degrees into meters? I'd like to not reproject my shapefile...

2

1 Answer 1

11

Use the Haversine Formula to calculate distance between two points. Here is a simple Python version that returns the distance in kilometers:

from math import cos, sin, asin, sqrt, radians

def calc_distance(lat1, lon1, lat2, lon2):
    """
    Calculate the great circle distance between two points
    on the earth (specified in decimal degrees)
    """
    # convert decimal degrees to radians
    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
    # haversine formula
    dlon = lon2 - lon1
    dlat = lat2 - lat1
    a = sin(dlat / 2) ** 2 + cos(lat1) * cos(lat2) * sin(dlon / 2) ** 2
    c = 2 * asin(sqrt(a))
    km = 6371 * c
    return km
1
  • 2
    -1 Calculating a distance between points is not the problem I'm trying to solve. Essentially, I need a formula for (lat, lon, heading, radius) -> (lat, lon)
    – Adam Matan
    Commented Mar 31, 2014 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.