在simplekml中使用共享样式时改变单个点的样式

1 投票
1 回答
1381 浏览
提问于 2025-04-17 21:19

我正在使用simplekml来绘制一些数据。

因为我可能会有很多点,所以我使用了共享样式(sharedstyle),但问题是我无法在每个点之间设置不同的样式参数,比如图标的大小(icon scale)或气泡样式的文本(balloonstyle text)。

所以,我有了我的共享样式:

kml = simplekml.Kml()
    fol = kml.newfolder(name="Eventos")
    sharedstyle = simplekml.Style()
    sharedstyle.labelstyle.color = 'ffffffff'
    sharedstyle.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/shapes/target.png'
    sharedstyle.iconstyle.color = 'ff0000ff'
    sharedstyle.balloonstyle.bgcolor = simplekml.Color.lightgreen
    sharedstyle.balloonstyle.textcolor = simplekml.Color.rgb(0, 0, 255)

我在一个循环中读取数据,并为每个需要创建的点调用这个方法:

def add_to_kml(folder, event, style):

    coord = (event.lon, event.lat)
    label = event.date.isoformat()
    pnt = folder.newpoint(name="{0}".format(label), coords=[coord])
    pnt.style = style
    pnt.style.iconstyle.scale = event.scale
    pnt.style.balloonstyle.text = "{0}, \n {1}".format(event.label, event.geo_ref)

但是,每个绘制的点都有相同的图标大小和气泡样式文本(都是最后添加的点的样式)。

有没有办法在使用共享样式的同时修改一些样式数据呢?

1 个回答

0

只有在非常有限的情况下。样式并不是层级关系,而是互相替代的。不过……

气泡样式中的 文本 可以使用一些变量,这些变量指向它所引用的元素(比如 $[name], $[description], $[address], $[id], $[Snippet])。所以你可以把自己的信息放在描述里,然后在气泡中使用这些信息。

希望这对你有帮助!

撰写回答