ElementTree API的KML工具
项目描述
Keytree提供使用ElementTree API读取和写入KML的功能。
读取KML
KML Placemark元素可以适配Python地理接口,然后与Shapely等软件包一起使用。
>>> data = """<?xml version="1.0" encoding="UTF-8"?>
... <kml xmlns="http://www.opengis.net/kml/2.2">
... <Document>
... <Placemark id="pm_1">
... <name>point</name>
... <Snippet>Point test</Snippet>
... <Point>
... <coordinates>
... -122.364383,37.824664,0
... </coordinates>
... </Point>
... </Placemark>
... </Document>
... </kml>
... """
>>> from xml.etree import ElementTree
>>> doc = ElementTree.fromstring(data)
>>> kmlns = doc.tag.split('}')[0][1:]
>>> placemarks = doc.findall('*/{%s}Placemark' % kmlns)
>>> p0 = placemarks[0]
>>> import keytree
>>> f = keytree.feature(p0)
>>> print f.id, f.properties.name, f.properties.snippet
pm_1, point, Point test
>>>
>>> from shapely.geometry import shape
>>> s = shape(f.geometry)
>>> print s.buffer(1.5).exterior.length
9.4209934708642571
写入KML
提供Python地理接口的对象也可以转换为ElementTree API元素
>>> f = {
... 'id': 'pm_2',
... 'geometry': {
... 'type': 'Point',
... 'coordinates': (-122.364383, 37.824663999999999) },
... 'properties': {
... 'title': 'Feature 2',
... 'description': 'The second feature', }
keytree.element函数的第一个参数是XML上下文,创建的元素将与该元素具有相同的命名空间
>>> data = """<?xml version="1.0" encoding="UTF-8"?>
... <kml xmlns="http://www.opengis.net/kml/2.2">
... <Document>
... </Document>
... </kml>
... """
>>> doc = ElementTree.fromstring(data)
>>> elem = element(doc, f)
>>> print elem
<Element {http://www.opengis.net/kml/2.2}Placemark at ...>
>>> pprint(list(elem))
[<Element {http://www.opengis.net/kml/2.2}name at ...>,
<Element {http://www.opengis.net/kml/2.2}Snippet at ...>,
<Element {http://www.opengis.net/kml/2.2}description at ...>,
<Element {http://www.opengis.net/kml/2.2}Point at ...>]
创建的元素不会自动添加到KML上下文,必须将其附加到适当的文档或文件夹
>>> doc[0].append(elem)
>>> print etree.tostring(doc)
<ns0:kml xmlns:ns0="http://www.opengis.net/kml/2.2">
<ns0:Document>
<ns0:Placemark id="pm_2">
<ns0:name>Number 2</ns0:name>
<ns0:Snippet>Placemark number 2</ns0:Snippet>
<ns0:description />
<ns0:Point>
<ns0:coordinates>0.000000,0.000000,0.0</ns0:coordinates>
</ns0:Point>
</ns0:Placemark>
</ns0:Document>
</ns0:kml>
KML助手
keytree.kml模块包含一些有用的实用函数
>>> from keytree.kml import kml_ns, findall_placemarks
>>> print kml_ns(doc)
{http://www.opengis.net/kml/2.2}
>>> findall_placemarks(doc)
[<Element {http://www.opengis.net/kml/2.2}Placemark at ...>]
项目详情
下载文件
下载适用于您的平台文件。如果您不确定选择哪个,请了解更多关于 安装软件包 的信息。
源分发
keytree-1.1.0.tar.gz (7.6 kB 查看哈希值)
构建的发行版
keytree-1.1.0-py3-none-any.whl (8.8 kB 查看哈希值)
关闭
keytree-1.1.0.tar.gz 的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | b0db38f599317827bb4ff6de76d18bad95a594ea9c9e6cf44e98ba68aea86430 |
|
| MD5 | 49e148cb8e9eeed0936302c41c8f75e8 |
|
| BLAKE2b-256 | bc1e313b8a491799dcc1290e6c84b07c95866ebd196f17adbd3c0e8ae9d1a37a |
关闭
keytree-1.1.0-py3-none-any.whl 的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | 4696465136b502e1c6b49b4954ce2ad8938e001b7fccffe3017e5c922f209204 |
|
| MD5 | a6e7532d637288f7505b00565c22e745 |
|
| BLAKE2b-256 | 89dd82a7d556cba27d481006f28348aa551e61763e1b3065ada7179a29885eea |