为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

HUX布斯 2021-03-30 16:10:49
我有一个名为地标的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 # kmdLat = (lat2-lat1) # Make sure it's in radians, not degreesdLon = (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 minidomxmldoc = 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 回答

?
一只萌萌小番薯

TA贡献1795条经验 获得超7个赞

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)


查看完整回答
反对 回复 2021-04-05
  • 2 回答
  • 0 关注
  • 191 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信