で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