-1

所以我正在尝试编写一个程序(没有依赖项),它将从谷歌地图(见下文)读取 KML 文件以获取坐标并将它们存储在矩阵中以供以后数据操作。

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
<Document>
  <name>Driving directions to Commercial St/A1202</name>
  <description><![CDATA[]]></description>
  <Style id="style1">
    <IconStyle>
      <Icon>
        <href></href>
      </Icon>
    </IconStyle>
  </Style>
  <Style id="style2">
    <LineStyle>
      <color>73FF0000</color>
      <width>5</width>
    </LineStyle>
  </Style>
  <Style id="style3">
    <IconStyle>
      <Icon>
        <href></href>
      </Icon>
    </IconStyle>
  </Style>
  <Placemark>
    <name>From: Unknown road</name>
    <styleUrl>#style1</styleUrl>
    <Point>
      <coordinates>-0.168942,51.520180,0.000000</coordinates>
    </Point>
  </Placemark>
  <Placemark>
    <name>Driving directions to Commercial St/A1202</name>
    <styleUrl>#style2</styleUrl>
    <ExtendedData>
      <Data name="_SnapToRoads">
        <value>true</value>
      </Data>
    </ExtendedData>
    <LineString>
      <tessellate>1</tessellate>
      <coordinates>
        -0.168942,51.520180,0.000000
        -0.167752,51.520447,0.000000
        -0.167371,51.520481,0.000000
      </coordinates>
    </LineString>
  </Placemark>
  <Placemark>
    <name>To: Commercial St/A1202</name>
    <styleUrl>#style3</styleUrl>
    <Point>
      <coordinates>-0.073247,51.516960,0.000000</coordinates>
    </Point>
  </Placemark>
</Document>
</kml>

虽然这对于像上面这样的小文件来说并不是低效的,但我有 500+KB 的文件需要稍后解析!那么关于获取这些坐标并将它们存储为矩阵的有效方法(不涉及需要安装的“非标准”导入)的任何建议?

4

1 回答 1

1

所以,以下应该做你。通过依赖项,我假设您的意思是在 Python 附带的标准库之外(我不确定在没有标准库 tbh 的情况下如何安装 python)......

import xml.etree.ElementTree as ET

print 'Starting'

tree = ET.parse('sample_data.xml')
root = tree.getroot()

for child in root.findall('.//{http://earth.google.com/kml/2.2}coordinates'):
    print child.text

如果您真的不能使用 ElementTree,那么您遇到的问题比 XML 处理的效率更大。

顺便说一句......两分钟(或更少)的谷歌搜索会让你得到这个答案。

于 2013-10-12T12:30:29.283 回答