带有Google Earth坐标的python中的距离计算

我有一个名为地标的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 ...之间的距离(在distance函数中保持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 )


HUX布斯
浏览 206回答 2
2回答

一只萌萌小番薯

import mathdef 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 dpos = [       (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)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python