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...

eouti
  • 435
  • 2
  • 6
  • 15
  • I know you said you don't want to reproject the shapefile but it is quite easy to do so. Here is a sample script how to do it: http://gis.stackexchange.com/questions/61303/python-ogr-transform-coordinates-from-meter-to-decimal-degrees – ustroetz May 27 '13 at 23:52
  • http://stackoverflow.com/questions/27928/how-do-i-calculate-distance-between-two-latitude-longitude-points – urcm May 28 '13 at 06:31

1 Answers1

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
bananafish
  • 652
  • 5
  • 13
  • 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 Mar 31 '14 at 16:30