代码之家  ›  专栏  ›  技术社区  ›  maurobio

如何在Python中从KML文件中获取坐标列表

  •  0
  • maurobio  · 技术社区  · 8 年前

    <?xml version="1.0"?><kml xmlns="http://earth.google.com/kml/2.1">
    <Document>
    <name>Test KML</name>
    <description><![CDATA[<p>This is a test version.</p>]]></description>
    <Style id="spstyle7">
        <IconStyle>
            <color>ff4DF6D8</color>
            <Icon><href>http://maps.google.com/mapfiles/kml/paddle/wht-blank.png</href></Icon>
        </IconStyle>
        <LineStyle>
            <color>ff4DF6D8</color>
            <width>4</width>
        </LineStyle>
    </Style>
    <Folder>
        <name>Track1</name>
        <visibility>0</visibility>
                <name>Test1</name>
                <description><![CDATA[test1]]></description>
                <Placemark>
                    <name>test1</name>
                    <description><![CDATA[test1]]></description>
                    <MultiGeometry>
                <LineString>
                            <tessellate>true</tessellate>
                            <altitudeMode>clampToGround</altitudeMode>
                            <coordinates>
                                11.000,4.000 11.000,3.000
                            </coordinates>
                        </LineString>
                <LineString>
                            <tessellate>true</tessellate>
                            <altitudeMode>clampToGround</altitudeMode>
                            <coordinates>
                                11.000,4.000 12.000,4.000
                            </coordinates>
                        </LineString>
                <LineString>
                            <tessellate>true</tessellate>
                            <altitudeMode>clampToGround</altitudeMode>
                            <coordinates>
                                12.000,5.000 12.000,4.000
                            </coordinates>
                        </LineString>
                    </MultiGeometry>
                </Placemark>
    </Folder>
    <Style id="spstyle7">
        <IconStyle>
            <color>ff4DF6D8</color>
            <Icon><href>http://maps.google.com/mapfiles/kml/paddle/wht-blank.png</href></Icon>
        </IconStyle>
        <LineStyle>
            <color>ff4DF6D8</color>
            <width>4</width>
        </LineStyle>
    </Style>
    <Folder>
        <name>Track2</name>
        <visibility>0</visibility>
                <name>Test2</name>
                <description><![CDATA[test2]]></description>
                <Placemark>
                    <name>test2</name>
                    <description><![CDATA[test2]]></description>
                    <MultiGeometry>
                <LineString>
                            <tessellate>true</tessellate>
                            <altitudeMode>clampToGround</altitudeMode>
                            <coordinates>
                                8.000,8.000 8.000,7.000
                            </coordinates>
                        </LineString>
                <LineString>
                            <tessellate>true</tessellate>
                            <altitudeMode>clampToGround</altitudeMode>
                            <coordinates>
                                8.000,7.000 11.000,6.000
                            </coordinates>
                        </LineString>
                <LineString>
                            <tessellate>true</tessellate>
                            <altitudeMode>clampToGround</altitudeMode>
                            <coordinates>
                                9.000,1.000 10.000,1.000
                            </coordinates>
                        </LineString>
                    </MultiGeometry>
                </Placemark>
    </Folder>
    </Document>
    </kml>
    

    协调 标记,可以放入列表或列表列表(每个文件夹一个)。

    首先,我编写了以下代码:

    import xml.etree.ElementTree as ET
    tree = ET.parse("test.kml")
    root = tree.getroot()
    results = root.findall('Folder')
    textnumbers = [r.find('Placemark/LineString/coordinates').text for r in results]
    print textnumbers
    

    文件夹 名称,代码如下:

    for folder in root.findall('Folder'):
        name = folder.find('name')
        print name
    

    我还得到一个空字符串。为什么解析器没有找到 标签有什么提示吗?

    提前感谢您提供的任何帮助。

    2 回复  |  直到 8 年前
        1
  •  2
  •   maurobio    8 年前

    事实上,我在这里找到了一个很好的解决方案: https://gis.stackexchange.com/questions/89543/get-points-from-a-kml-linestring

    相应地修改我的代码:

    import xml.etree.ElementTree as ET
    tree = ET.parse("test.kml")
    root = tree.getroot()
    
    lineStrings = tree.findall('.//{http://earth.google.com/kml/2.1}LineString')
    
    for attributes in lineStrings:
        for subAttribute in attributes:
            if subAttribute.tag == '{http://earth.google.com/kml/2.1}coordinates':
                print subAttribute.tag, subAttribute.text
    

    其他可能的解决方案(未经测试)也可在此处找到: https://programmingadvent.blogspot.com.br/2013/06/kmzkml-file-parsing-with-python.html http://gsp.humboldt.edu/olm_2016/courses/GSP_318/04_3_2_Parsing_XML.html

        2
  •  0
  •   Tomasz Plaskota    8 年前

    你的根是 kml Document 假设的节点

    当你这么做的时候 tree.getroot() 你抓住 节点。在这种情况下,如果您只是将代码更改为包含 文件

    实际上,唯一需要更改的行应该是:

    result = root.findall('Document/Folder')