【问题标题】:More efficient than line by line reading (of KML in Python)比逐行阅读(Python中的KML)更有效
【发布时间】:2013-10-09 08:38:48
【问题描述】:

所以我正在尝试编写一个程序(没有依赖项),该程序将从 Google 地图(见下文)读取 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 的文件需要稍后解析!那么关于获取这些坐标并将它们存储为矩阵的有效方法(不涉及需要安装的“非标准”导入)的任何建议?

【问题讨论】:

  • 你现有的“低效”代码是什么样的?
  • 正如我在标题中所述。逐行检查它是否在坐标标签中。然后在 标签之前赋值。
  • 我看了标题。仍然没有看到任何代码。

标签: python performance kml data-manipulation


【解决方案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 处理效率更大。

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

【讨论】:

    猜你喜欢
    • 2016-02-10
    • 2016-03-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-08
    • 1970-01-01
    • 2020-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多