首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更改KML中的标签id属性(Simplekml)

更改KML中的标签id属性(Simplekml)
EN

Stack Overflow用户
提问于 2021-04-30 05:11:35
回答 1查看 77关注 0票数 3

是否可以在simplekml中更改标记中的id属性?

代码语言:javascript
复制
import simplekml
kml = simplekml.Kml()
pnt = kml.newpoint(name='A Point')
pnt.coords = [(1.0, 2.0)]
kml.save("icon.kml")

这将生成以下文档

代码语言:javascript
复制
<?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 My Maps上的图标。我如何使用simplekml来做这件事?

EN

回答 1

Stack Overflow用户

发布于 2021-06-22 05:15:12

在入门文档https://simplekml.readthedocs.io/en/latest/gettingstarted.html中,id标签对于不同类型的对象是不同的生成方式,即'feat_1','feat_2','geom_0':

代码语言:javascript
复制
import simplekml
kml = simplekml.Kml()
pnt = kml.newpoint(name="A Point")
print kml.kml()

下面是生成的代码:

代码语言:javascript
复制
<?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。

代码语言:javascript
复制
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标记被设计为只读:

代码语言:javascript
复制
    @property
    def id(self):
        """The id string (read only)."""
        return self._id

一种可以改变这一点的hack-y方法类似于(在base.py中):

代码语言:javascript
复制
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:

代码语言:javascript
复制
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>

希望这能帮助你理解。不是一个句号的答案,但太长了,无法在评论中发表。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67325123

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档