0

嗨,我有一个名为 Placemark 的 kml 文件和 4 个节点(地标),它们位于 Google 地球上的一个区域中。每个地标节点都有一个经度和一个纬度。

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
    <name>placemarks.kml</name>
    <StyleMap id="m_ylw-pushpin">
        <Pair>
            <key>normal</key>
            <styleUrl>#s_ylw-pushpin</styleUrl>
        </Pair>
        <Pair>
            <key>highlight</key>
            <styleUrl>#s_ylw-pushpin_hl</styleUrl>
        </Pair>
    </StyleMap>
    <Style id="s_ylw-pushpin_hl">
        <IconStyle>
            <scale>1.3</scale>
            <Icon>
                <href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>
            </Icon>
            <hotSpot x="20" y="2" xunits="pixels" yunits="pixels"/>
        </IconStyle>
    </Style>
    <Style id="s_ylw-pushpin">
        <IconStyle>
            <scale>1.1</scale>
            <Icon>
                <href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>
            </Icon>
            <hotSpot x="20" y="2" xunits="pixels" yunits="pixels"/>
        </IconStyle>
    </Style>
    <Folder>
        <name>placemarks</name>
        <open>1</open>
        <Placemark>
            <name>node0</name>
            <LookAt>
                <longitude>21.78832062146911</longitude>
                <latitude>38.28791526390673</latitude>
                <altitude>0</altitude>
                <heading>0.001539813336055052</heading>
                <tilt>44.99941206765101</tilt>
                <range>990.2435222326291</range>
                <gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
            </LookAt>
            <styleUrl>#m_ylw-pushpin</styleUrl>
            <Point>
                <coordinates>21.78400936610002,38.2874355527483,67.51641688336248</coordinates>
            </Point>
        </Placemark>
        <Placemark>
            <name>node1</name>
            <LookAt>
                <longitude>21.78832062146911</longitude>
                <latitude>38.28791526390673</latitude>
                <altitude>0</altitude>
                <heading>0.001539813336055052</heading>
                <tilt>44.99941206765101</tilt>
                <range>990.2435222326291</range>
                <gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
            </LookAt>
            <styleUrl>#m_ylw-pushpin</styleUrl>
            <Point>
                <coordinates>21.78453228393861,38.28690995466475,67.51641688336248</coordinates>
            </Point>
        </Placemark>
        <Placemark>
            <name>node2</name>
            <LookAt>
                <longitude>21.78832062146911</longitude>
                <latitude>38.28791526390673</latitude>
                <altitude>0</altitude>
                <heading>0.001539813336055052</heading>
                <tilt>44.99941206765101</tilt>
                <range>990.2435222326291</range>
                <gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
            </LookAt>
            <styleUrl>#m_ylw-pushpin</styleUrl>
            <Point>
                <coordinates>21.7848823502596,38.2869152766261,67.51641688336248</coordinates>
            </Point>
        </Placemark>
        <Placemark>
            <name>node3</name>
            <LookAt>
                <longitude>21.78832062146911</longitude>
                <latitude>38.28791526390673</latitude>
                <altitude>0</altitude>
                <heading>0.001539813336055052</heading>
                <tilt>44.99941206765101</tilt>
                <range>990.2435222326291</range>
                <gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
            </LookAt>
            <styleUrl>#m_ylw-pushpin</styleUrl>
            <Point>
                <coordinates>21.78459887820567,38.28740826552452,67.51641688336248</coordinates>
            </Point>
        </Placemark>
    </Folder>
</Document>
</kml>

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

通过使用下面的代码:(我需要修改它才能得到下面的输出)

 # 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)

Ang 得到如下输出:

node1: (21.78453228393861, 38.28690995466475), distance from node0: 
node2: (21.78488235025960, 38.28691527662610), distance from node0: 
node3: (21.78459887820567, 38.28740826552452), distance from node0: 
4

1 回答 1

0

在您收集从 KML 文件中提取的节点的代码中,当您从 XML 中提取节点时,节点的名称始终是相同的,因此当您尝试将其存储在字典nodes中时,最终只能得到一个点。这是一个更正的脚本(只有最后一部分,需要修复),我认为它可以满足您的要求(如果不是这种情况,请告诉我们您需要其他东西):

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

for n in nodes:
    if n == 'node0':
        continue
    print n, nodes[n], 'distance from node0:', calc_distance(nodes['node0'], nodes[n])

这是输出:

node1 (38.28690995466475, 21.78453228393861) distance from node0: 0.074152874049
node3 (38.28740826552452, 21.78459887820567) distance from node0: 0.0515409901388
node2 (38.2869152766261, 21.7848823502596) distance from node0: 0.0956671636024
于 2013-06-24T11:31:42.390 回答