python - 更改 KML (Simplekml) 中的标签 id 属性
问题描述
是否可以在 simplekml 中更改标签中的 id 属性?
import simplekml
kml = simplekml.Kml()
pnt = kml.newpoint(name='A Point')
pnt.coords = [(1.0, 2.0)]
kml.save("icon.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">
<Document id="1">
<Folder id="2">
<Style id="5">
<IconStyle id="6">
<colorMode>normal</colorMode>
<scale>1</scale>
<heading>0</heading>
<Icon id="7">
<href>https://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png</href>
</Icon>
</IconStyle>
</Style>
<Placemark id="4">
<name>A Point</name>
<styleUrl>#5</styleUrl>
<Point id="3">
<coordinates>1.0,2.0,0.0</coordinates>
</Point>
</Placemark>
</Folder>
</Document>
</kml>
请注意,在某些标签中,id 属性看起来好像是从某个 simplekml 索引生成的。我需要将分配给<Style id="5">
标签的 ID 更改为<Style id="icon-1532-0288D1-nodesc-normal">
这将更改 Google 我的地图上的图标。如何使用 simplekml 做到这一点?
解决方案
看起来在(入门)文档中,https: //simplekml.readthedocs.io/en/latest/gettingstarted.html ,不同类型的对象生成的 id 标记不同,即“feat_1”、“feat_2”、 “geom_0”:
import simplekml
kml = simplekml.Kml()
pnt = kml.newpoint(name="A Point")
print kml.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">
<Document id="feat_1">
<Placemark id="feat_2">
<name>A Point</name>
<Point id="geom_0">
<coordinates>0.0, 0.0, 0.0</coordinates>
</Point>
</Placemark>
</Document>
</kml>
我查看了源代码,看起来至少在 1.3.2 版本中,他们摆脱了这一点,并通过计数来生成标签 ID。
class Kmlable(object):
"""Enables a subclass to be converted into KML."""
_globalid = 0
_currentroot = None
_compiling = False
_namespaces = ['xmlns="http://www.opengis.net/kml/2.2"', 'xmlns:gx="http://www.google.com/kml/ext/2.2"']
def __init__(self):
self._id = str(Kmlable._globalid)
Kmlable._globalid += 1
try:
from collections import OrderedDict
self._kml = OrderedDict()
except ImportError:
self._kml = {}
出于某种原因,这些 id 标签被设计为只读:
@property
def id(self):
"""The id string (read only)."""
return self._id
你可以改变它的一种 hack-y 方法是(在 base.py 中):
class Kmlable(object):
"""Enables a subclass to be converted into KML."""
#_globalid = 0
_globalid = 'your_string_here'
_currentroot = None
_compiling = False
_namespaces = ['xmlns="http://www.opengis.net/kml/2.2"', 'xmlns:gx="http://www.google.com/kml/ext/2.2"']
def __init__(self):
self._id = str(Kmlable._globalid)
#Kmlable._globalid += 1
try:
from collections import OrderedDict
self._kml = OrderedDict()
except ImportError:
self._kml = {}
但这将为您在 kml 文档中拥有的每个对象设置相同的 id:
import simplekml
kml = simplekml.Kml()
pnt = kml.newpoint(name="A Point")
print(kml.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">
<Document id="your_string_here">
<Placemark id="your_string_here">
<name>A Point</name>
<Point id="your_string_here">
<coordinates>0.0, 0.0, 0.0</coordinates>
</Point>
</Placemark>
</Document>
</kml>
希望这有助于您的理解。不是一个完整的答案,但是太长了,无法在评论中发布。
推荐阅读
- azure-devops - 使用 Regex 在 Azure DevOps 构建管道上选择测试项目
- php - 管道函数的意外输出
- html - Bootstrap 下拉菜单在 iPhone 上的 Safari 中不起作用
- php - 在通过 phpmailer 发送变量之前等于一个变量的循环
- python - 为什么我的 CNN 模型在本地 CPU 上的训练时间比托管选项要少?
- amazon-web-services - AWS - 由于位置限制,无法通过 HTTPS 端点访问 S3 存储桶
- google-app-engine - 当我重新部署我的 google 项目 ID 时,我在 gcloud.app.deploy 中看到一个错误,提示 Insufficient permissions to create Google Cloud Storage bucket
- x86 - 实模式下的分段地址冲突到相同的物理地址
- sql - 如何汇总来自同一表的不同组项目的前 1 条记录?
- javascript - TypeError: app.use() 需要一个中间件函数