我有一个名为地标的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 )
一只萌萌小番薯
相关分类