用Google地球坐标计算python中的距离

时间:2013-06-23 21:00:59

标签: python python-3.x

您好我有一个名为Placemark的kml文件和位于Google地球上某个区域的4个节点(地标)。每个地标节点都有经度和纬度。通过上面的代码,我能够提取数据

(u'node0:', 21.78400936610002, 38.2874355527483)
(u'node1:', 21.78453228393861, 38.28690995466475)
(u'node2:', 21.7848823502596, 38.2869152766261)
(u'node3:', 21.78459887820567, 38.28740826552452)

我想要的是计算node0和node2,3,4之间的距离......(在距离函数中保持node0不变)然后打印结果。

我想要使用的功能是:

import math 

R = 6371 # km
dLat = (lat2-lat1) # Make sure it's in radians, not degrees
dLon = (lon2-lon1) # Idem 
a = math.sin(dLat/2) * math.sin(dLat/2) +
    math.cos(lat1) * math.cos(lat2) * 
    math.sin(dLon/2) * math.sin(dLon/2) 
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a)) 
d = R * c;

from xml.dom import minidom

xmldoc = minidom.parse("placemarks.kml")
kml = xmldoc.getElementsByTagName("kml")[0]
document = kml.getElementsByTagName("Document")[0]
placemarks = document.getElementsByTagName("Placemark")

for placemark in placemarks:
    nodename = placemark.getElementsByTagName("name")[0].firstChild.data
    lst = nodename.split(":")
    coords = placemark.getElementsByTagName("coordinates")[0].firstChild.data
    lst1 = coords.split(",")
    longitude = float(lst1[0])
    latitude = float(lst1[1])

def calc_distance(longitude, latitude)
    print(nodename + ":",longitude, latitude )

2 个答案:

答案 0 :(得分:0)

使用code from here

import math

def distance(origin, destination):
    lat1, lon1 = origin
    lat2, lon2 = destination
    radius = 6371 # km

    dlat = math.radians(lat2-lat1)
    dlon = math.radians(lon2-lon1)
    a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(lat1)) \
        * math.cos(math.radians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2)
    c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
    d = radius * c 

    return d

pos = [ 
      (u'node0:', 21.78400936610002, 38.2874355527483),
      (u'node1:', 21.78453228393861, 38.28690995466475),
      (u'node2:', 21.7848823502596, 38.2869152766261),
      (u'node3:', 21.78459887820567, 38.28740826552452)]

node0 = [p for p in pos if p[0] == 'node0:'][0]
dist=(distance(node0[1:3], p[1:3]) for p in pos if p[0] != 'node0:')
for d in dist: print(d)

答案 1 :(得分:0)

这是你的代码,经过一些修改,可以做你想做的事情:

# adapted from haversine.py <https://gist.github.com/rochacbruno/2883505>
# see also <http://en.wikipedia.org/wiki/Haversine_formula>
from math import atan2, cos, sin, sqrt, radians

def calc_distance(origin, destination):
    """great-circle distance between two points on a sphere
       from their longitudes and latitudes"""
    lat1, lon1 = origin
    lat2, lon2 = destination
    radius = 6371 # km. earth

    dlat = radians(lat2-lat1)
    dlon = radians(lon2-lon1)
    a = (sin(dlat/2) * sin(dlat/2) + cos(radians(lat1)) * cos(radians(lat2)) *
         sin(dlon/2) * sin(dlon/2))
    c = 2 * atan2(sqrt(a), sqrt(1-a))
    d = radius * c

    return d

from xml.dom import minidom

xmldoc = minidom.parse("placemarks.kml")
kml = xmldoc.getElementsByTagName("kml")[0]
document = kml.getElementsByTagName("Document")[0]
placemarks = document.getElementsByTagName("Placemark")

nodes = {}
for placemark in placemarks:
    nodename = placemark.getElementsByTagName("name")[0].firstChild.data[:-1]
    coords = placemark.getElementsByTagName("coordinates")[0].firstChild.data
    lst1 = coords.split(",")
    longitude = float(lst1[0])
    latitude = float(lst1[1])
    nodes[nodename] = (latitude, longitude)

## used for testing due to lack of kml file...
#nodes = {
#    u'node0': (21.78400936610002, 38.2874355527483),
#    u'node1': (21.78453228393861, 38.28690995466475),
#    u'node2': (21.7848823502596, 38.2869152766261),
#    u'node3': (21.78459887820567, 38.28740826552452)}

node0coords = nodes[u'node0']
for nodename in sorted(name for name in nodes if name != u'node0'):
    longitude, latitude = nodes[nodename]
    print('{}: ({:3.14f}, {:3.14f}), distance from node0: {:.3f} km'.format(
          nodename, longitude, latitude,
          calc_distance(node0coords, nodes[nodename])))

输出:

node1: (21.78453228393861, 38.28690995466475), distance from node0: 0.080 km
node2: (21.78488235025960, 38.28691527662610), distance from node0: 0.111 km
node3: (21.78459887820567, 38.28740826552452), distance from node0: 0.066 km