【Python】simplekml如何设置样式(解决simplekml样式不生效)

原文作者:我辈李想
版权声明:文章原创,转载时请务必加上原文超链接、作者信息和本声明。



前言

simplekml是一个用于创建kml文件的库,里面提供了丰富的功能和样式,这里介绍如何设置相关样式,解决样式不显示的问题。


一、基本样式

常用

kml = simplekml.Kml(name='tianyi map')
pnt = kml.newpoint(name="shanghai", coords=[[121.4222, 31.218123]])
# lon, lat, optional height
pnt.description = "描述信息: 东方"
pnt.style.labelstyle.color = simplekml.Color.red  # 设置字体颜色  'ffff0000'
pnt.style.labelstyle.scale = 2                    # 设置字体大小
pnt.style.labelstyle.scale = 0.0                  # 隐藏字体 name
pnt.iconstyle.icon.href = ''  # 取消图标
pnt.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/shapes/placemark_circle.png'  # 设置图标
pnt.style.iconstyle.scale = 3  # 设置图片的大小
kml.save(file_kml)

二、共享样式

import simplekml

kml = simplekml.Kml()
style = simplekml.Style()  # 或者 simplekml.StyleMap()
kml.style = style  # 或者 kml.stylemap = stylemap

# 设置Style样式
style.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/shapes/placemark_circle.png'
style.linestyle.width = 3
style.polystyle.color = simplekml.Color.rgb(255, 255, 0)

# 设置StyleMap样式
stylemap.normalstyle.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/shapes/placemark_circle.png'
stylemap.normalstyle.linestyle.width = 3
stylemap.normalstyle.polystyle.color = simplekml.Color.rgb(255, 255, 0)
stylemap.highlightstyle.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/shapes/placemark_circle_highlight.png'
stylemap.highlightstyle.linestyle.width = 5
stylemap.highlightstyle.polystyle.color = simplekml.Color.rgb(255, 0, 0)

point = kml.newpoint(name='Point', coords=[(0,0)])
point.style = style  # 或者 point.stylemap = stylemap

三、全局样式

simplekml全局设置需要设置kml.document.style或kml.document.stylemap,如果设置kml.style将导致样式失效(这一点网上课程几乎很少介绍,需要小心)。

1.结果

错误结果

在这里插入图片描述

正确结果

在这里插入图片描述

2.代码

kml = simplekml.Kml()
kml.document.name = "doc"

# 设置StyleMap样式
stylemap = simplekml.StyleMap()
stylemap.normalstyle.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png'
stylemap.normalstyle.linestyle.width = 3
stylemap.normalstyle.polystyle.color = simplekml.Color.rgb(255, 255, 0)
stylemap.highlightstyle.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png'
stylemap.highlightstyle.linestyle.width = 5
stylemap.highlightstyle.polystyle.color = simplekml.Color.rgb(255, 0, 0)
kml.document.stylemap = stylemap
s_folder = kml.newfolder(name="test")
s_folder.stylemap = stylemap

coords = [[110.6183975164221, 19.46456470187252], [110.9040907171277, 19.46494114209082], [110.9039351250598, 19.7273780493985], [110.6177784661397, 19.72699612707156]]
s_polygon = s_folder.newpolygon(name=zf.get("ProductID"), outerboundaryis=coords, tessellate="1")
s_polygon.stylemap = stylemap
kml.save("test.kml")
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我辈李想

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值