# python 经纬度求两点距离、三点面积操作

## 先给出半正失公式(haversine formula):

r是地球半径，d/r表示两点在圆上的弧度θ。

## 通过整理两个式子可得：

### 具体的python代码实现如下：

import math
class cal_distance(object):
def __init__(self,**kwargs):
self.lat1 = kwargs.get('lat1')
self.lon1 = kwargs.get('lon1')
self.lat2 = kwargs.get('lat2')
self.lon2 = kwargs.get('lon2')

def twopoint_distance(self):
R=6371.393
c=2*math.atan2(math.sqrt(a),math.sqrt(1-a))
return R*c

return deg*(math.pi/180)

from cal_distance import cal_distance
def run():
point1_lat = 39.2186266952
point2_lat = 39.08579871
point1_lon = 117.8175961241
point2_lon = 117.7040162
Distance = cal_distance(lat1=point1_lat,lon1=point1_lon1,lat2=point2_lat,lon2=point2_lon)
distance = Distance.twopoint_distance()
print distance

if __name__=='__main__':
run()

### 下面给出类的定义部分：

import math
class cal_area(object):
def __init__(self,**kwargs):
self.lat1 = kwargs.get('lat1')
self.lon1 = kwargs.get('lon1')
self.lat2 = kwargs.get('lat2')
self.lon2 = kwargs.get('lon2')
self.lat3 = kwargs.get('lat3')
self.lon3 = kwargs.get('lon3')

def twopoint_distance(self,lat1,lon1,lat2,lon2):
R=6371.393
c=2*math.atan2(math.sqrt(a),math.sqrt(1-a))
return R*c

return deg*(math.pi/180)

def area(self):
distance12=self.twopoint_distance(self.lat1,self.lon1,self.lat2,self.lon2)
distance13=self.twopoint_distance(self.lat1,self.lon1,self.lat3,self.lon3)
distance23=self.twopoint_distance(self.lat2,self.lon2,self.lat3,self.lon3)
p=self.half_perimeter(distance12,distance23,distance13)
s=math.sqrt(p*(p-distance12)*(p-distance23)*(p-distance13))
return s

def half_perimeter(a,b,c):
return (a+b+c)/2

## Python Haversine公式计算两点（经纬度坐标）距离

import math
def LLs2Dist(lat1, lon1, lat2, lon2):
R = 6371
dLat = (lat2 - lat1) * math.pi / 180.0
dLon = (lon2 - lon1) * math.pi / 180.0
a = math.sin(dLat / 2) * math.sin(dLat / 2) + math.cos(lat1 * math.pi / 180.0) * math.cos(lat2 * math.pi / 180.0) * math.sin(dLon / 2) * math.sin(dLon / 2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
dist = R * c
return dist

x1 = 37.779388
y1 = -122.423246
x2 = 32.719464
y2 = -117.220406
dist = LLs2Dist(y1, x1, y2, x2)
print dist

642.185478152