使用simplekml添加点图标报错:'Kml'对象无'style'属性
解决simplekml添加自定义图标报错问题
你遇到的AttributeError是因为直接操作Kml根对象的style属性,但这个对象本身并没有该属性。正确的逻辑是:样式属于具体的地理要素(比如你创建的点),需要通过点对象来配置样式,或者先创建独立样式再绑定到点上。
下面是两种可行的修正方案:
方案1:先创建样式再绑定到点
import simplekml # 初始化KML实例 points_kml = simplekml.Kml() # 创建样式对象并设置图标链接 custom_style = points_kml.newstyle() custom_style.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/paddle/ltblu-circle.png' # 创建点并关联样式 point = points_kml.newpoint(name="Kirstenbosch", coords=[(18.432314,-33.988862)]) point.style = custom_style # 保存生成的KML文件 points_kml.save("botanicalgarden.kml")
方案2:直接给点配置样式(更简洁)
import simplekml points_kml = simplekml.Kml() # 创建点后直接操作其样式属性 point = points_kml.newpoint(name="Kirstenbosch", coords=[(18.432314,-33.988862)]) point.style.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/paddle/ltblu-circle.png' points_kml.save("botanicalgarden.kml")
两种方案都能生成带指定图标的单一点KML文件,核心是明确样式是依附于具体地理要素的,而非KML根对象。
内容的提问来源于stack exchange,提问作者Marcelo Mello
相关产品推荐
相关产品推荐

