I've got two methods for calculating the distance between georeferenced coordinates in python:
from pyproj import Proj
import math
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(math.radians, [lon1, lat1, lon2, lat2])
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = math.sin(dlat / 2) ** 2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon / 2) ** 2
c = 2 * math.asin(math.sqrt(a))
km = 6371 * c
return km
def calc_distance_convert_utm(lat1, lon1, lat2, lon2):
myProj = Proj("+proj=utm +zone=42, +north +ellps=WGS84 +datum=WGS84 +units=m +no_defs")
# convert to utm
utm_x1, utm_y1 = myProj(lat1, lon1)
utm_x2, utm_y2 = myProj(lat2, lon2)
diff_x = abs(utm_x1 - utm_x2)
diff_y = abs(utm_y1 - utm_y2)
distance = math.sqrt(diff_x**2 + diff_y**2)
return distance
Which I call with the following values:
lat1 = 34.866527
lon1 = 69.674606
lat2 = 34.864990
lon2 = 69.657655
print "approximation method: ", calc_distance(lat1, lon1, lat2, lon2)
print "converting to utm method: ", calc_distance_convert_utm(lat1, lon1, lat2, lon2)
However, If I compare the results, I get two different values:
approximation method: 1.55593476881
converting to utm method: 1928.21537269
Note, that the first method returns the distance in kilometers while the second returns it in meters. I've compared the result with distance calculators which you can find online, and it seems that the first method (approximation method) is the "more correct" answer as this is the value most online calculators return. I wonder, why the second method (converting to utm first) does not return a more similar result (something like 1555.9347...). I have a difference of almost 0.5 km which seems pretty much to me.
Did I do anything wrong? Any help is appreciated! Thanks