2017-04-04 7 views
1

でKMLを解析:Extract Coordinates from KML BatchGeo File with Python点検し、これに似たpyKML

しかし、私は同様にそれを反復処理する方法として、データオブジェクトを検査する方法を知りたい、とcoordinatesを取得するために、すべてのPlacemark解析。

以下はKMLの外観です。<Placemark>タグが複数あります。

サンプルKMLデータ

<?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:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.opengis.net/kml/2.2 http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd http://www.google.com/kml/ext/2.2 http://code.google.com/apis/kml/schema/kml22gx.xsd"> 
<Document id="..."> 
    <name>...</name> 
    <Snippet></Snippet> 
    <Folder id="..."> 
    <name>...</name> 
    <Snippet></Snippet> 
    <Placemark id="..."> 
     <name>....</name> 
     <Snippet></Snippet> 
     <description>...</description> 
     <styleUrl>....</styleUrl> 
     <Point> 
     <altitudeMode>...</altitudeMode> 
     <coordinates> 103.xxx,1.xxx,0</coordinates> 
     </Point> 
    </Placemark> 
    <Placemark id="..."> 
     ... 
    </Placemark> 
    </Folder> 
    <Style id="..."> 
    <IconStyle> 
     <Icon><href>...</href></Icon> 
     <scale>0.250000</scale> 
    </IconStyle> 
    <LabelStyle> 
     <color>00000000</color> 
     <scale>0.000000</scale> 
    </LabelStyle> 
    <PolyStyle> 
     <color>ff000000</color> 
     <outline>0</outline> 
    </PolyStyle> 
    </Style> 
</Document> 
</kml> 

これは私が持っているもので、extract.py

from pykml import parser 
from os import path 

kml_file = path.join('list.kml') 

with open(kml_file) as f: 
    doc = parser.parse(f).getroot() 

print doc.Document.Folder.Placemark.Point.coordinates 

これは最初のcoordinates印刷します。

一般的なpythonの質問:
docを調べて、そのタイプについて調べ、それに含まれている値を印刷するにはどうすればよいですか?

タスクの質問: Placemarkをすべて反復処理してcoordinatesを取得するにはどうすればよいですか?

以下を試しましたが、何も印刷されていません。

for e in doc.Document.Folder.iter('Placemark'): 
    print e 

答えて

2

私は答えを見つけました。 Placemarkを解析するために

、これはコード

for e in doc.Document.Folder.Placemark: 
    coor = e.Point.coordinates.text.split(',') 

type(object)を使用して、オブジェクトの種類を見つけることです。

findall()iter()はしかし動作しなかった理由はわからないが:

doc.Document.Folder.findall('Placemark') 

for e in doc.Document.Folder.iter('Placemark'): 

の両方が空に戻りました。

更新findallの名前空間が欠落していました。

doc.findall('.//{http://www.opengis.net/kml/2.2}Placemark') 
関連する問題