Google Earth生成的文件格式是KML/KMZ,这里介绍如何解析和生成KML格式文件,KMZ格式可以在Google Earth中另存为KML格式。
更详细的了解KML可以查看Google 官方教程。KML用于Google Earth和Google Map中显示地理数据,使用包含嵌套的元素和属性的结构(基于标记),符合 XML 标准。KML基本格式:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Placemark>
<name>Simple placemark</name>
<description>Not a real place.</description>
<LineString>
<coordinates>
120.315681310226,39.95885231106518,0 120.3157179045997,39.9586874087592,0
</coordinates>
<coordinates>
120.3158141778864,39.95791322572761,0 120.3158808732796,39.95791590956513,0
</coordinates>
</LineString>
</Placemark>
</kml>
上述文件的解释:
pyKML用于生成、解析和验证KML文件。
Linux下使用conda:
$ conda install -c conda-forge pykml
使用pip:
$ pip install pykml
如果是Python 3.x,pyKML在导入parser的时候会出现以下问题,没有模块urllib2 。
In [1]: from pykml import parser
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-386e0923aa06> in <module>()
----> 1 from pykml import parser
~/miniconda3/lib/python3.6/site-packages/pykml/parser.py in <module>()
6 import sys
7 import os
----> 8 import urllib2
ModuleNotFoundError: No module named 'urllib2'
原因是Python 2.x的urllib 和urllib2 模块在Python 3.x中被合并在了urlllib中。这里使用url是为了验证KML文件的有效性。
打开文件~/miniconda3/lib/python3.6/site-packages/pykml/parser.py
替换第8行为from urllib.request import urlopen
。再次尝试导入,提示缺少库文件libiconv.so.2
。
In [2]: from pykml import parser
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-386e0923aa06> in <module>()
----> 1 from pykml import parser
~/miniconda3/lib/python3.6/site-packages/pykml/parser.py in <module>()
7 import os
8 from urllib.request import urlopen
----> 9 from lxml import etree, objectify
ImportError: libiconv.so.2: cannot open shared object file: No such file or directory
安装库$ conda install -c conda-forge libiconv
,导入成功!或者尝试按照lxml官方教程安装lxml模块。
factory.py模块的print语句改成python3.x风格,kml2pykml()方法最后一行改为:
print(write_python_script_for_kml_document(doc))
解析KML格式的字符串:
In [1]: from pykml import parser
In [2]: kml_str = '<kml xmlns="http://www.opengis.net/kml/2.2">' \ # 定义kml格式的字符
....: '<Document>' \
....: '<Folder>' \
....: '<name>sample folder</name>' \
....: '</Folder>' \
....: '</Document>' \
....: '</kml>'
In [9]: root = parser.fromstring(kml_str)
In [10]: print(root.Document.Folder.name.text)
sample folder
解析格式介绍中的KML文件,Placemark标签下有两条路径,从文件中获取两条路径中所有点的坐标:
In [11]: with open(KML_FILE, 'r') as f:
In [12]: kml = parser.parse(f).getroot()
In [13]: for each in kml.Placemark: # 遍历所有的Placemark
In [14]: print(each.LineString.coordinates)
或者使用 findall( ) 查找所有的Placemark1:
In [15]: placemarks = kml.findall('.//{http://www.opengis.net/kml/2.2}Placemark')
In [16]: for each in placemarks:
In [17]: print(each.LineString.coordinates)
生成和验证KML文件可参考pyKML官方教程。