1

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

(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 )
4

2 回答 2

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
于 2013-06-24T02:14:20.237 回答
0

使用此处的代码

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)
于 2013-06-23T21:30:50.307 回答