2013-10-09 24 views
-1

所以我試圖編寫一個程序(不需要依賴),它將從Google Maps中讀取KML文件(見下文)以獲得座標並存儲他們在一個矩陣中進行數據處理。比線條讀取(Python中的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文件可以在以後解析!那麼對於有效的方法(不涉及「非標準」進口需要安裝)的任何建議,只需獲取這些座標並將它們存儲爲Matrix?

+0

你現有的「低效率」代碼是什麼樣的? – geocodezip

+0

正如我在標題中所述。逐行檢查以查看它是否在座標標記中。然後只要在標籤之前分配值。 – BrownE

+0

我讀了標題。仍然沒有看到任何代碼。 – geocodezip

回答

1

所以,下面應該做你。通過依賴我假設你的意思是Python自帶(我不知道你將如何有一個Python安裝不TBH標準庫)標準庫之外....

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處理的效率要大。

另外...兩分鐘(或更少)的谷歌搜索會讓你回答這個問題。