如何用Python simplekml实现KML文件夹仅显示单选按钮
问题描述
我需要从无人机记录的CSV点数据(每行含时间、经度、纬度、海拔)生成Google Earth可用的KML文件。现有代码通过simplekml库创建「点集合」和「飞行航线」两个文件夹,但设置simplekml.ListItemType.radiofolder后,文件夹内子项也出现单选按钮。我希望仅文件夹显示单选按钮,子项无此按钮,请问如何通过simplekml实现该效果?
附现有代码:
''' the command line : python test_kml.py ''' import csv import simplekml kml = simplekml.Kml() # read csv file inputfile = csv.reader(open("csv_file.csv", 'r')) # list where to store coords points = [] # create folder for points fol_points = kml.newfolder(name="The points") # create points for row in inputfile: coord = (float(row[1]), float(row[2]), float(row[3])) pnt = fol_points.newpoint(name=row[0], coords=[coord]) points.append(coord) pnt.style.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/shapes/placemark_circle.png' pnt.altitudemode = simplekml.AltitudeMode.relativetoground # create folder for linestring fol_line = kml.newfolder(name="The line") #create line ls = fol_line.newlinestring(name='the LineString') ls.coords = points ls.style.linestyle.width = 3 ls.altitudemode = simplekml.AltitudeMode.relativetoground ls.extrude = 0 #try to put radiobutton on folder fol_line.style.liststyle.listitemtype = simplekml.ListItemType.radiofolder fol_points.style.liststyle.listitemtype = simplekml.ListItemType.radiofolder #save kml file kml.save("csv_file.kml")
解决方案
问题根源是KML的样式会向下继承,当给父文件夹设置radiofolder后,其子元素会自动继承这个列表样式,导致子项也出现单选按钮。解决方法是给每个子元素(点和线串)单独设置列表项类型为默认的check,覆盖继承的样式。
修改后的代码如下:
''' the command line : python test_kml.py ''' import csv import simplekml kml = simplekml.Kml() # read csv file inputfile = csv.reader(open("csv_file.csv", 'r')) # list where to store coords points = [] # create folder for points fol_points = kml.newfolder(name="The points") # create points for row in inputfile: coord = (float(row[1]), float(row[2]), float(row[3])) pnt = fol_points.newpoint(name=row[0], coords=[coord]) points.append(coord) pnt.style.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/shapes/placemark_circle.png' pnt.altitudemode = simplekml.AltitudeMode.relativetoground # 给每个点设置默认列表项类型,覆盖父文件夹的单选样式 pnt.style.liststyle.listitemtype = simplekml.ListItemType.check # create folder for linestring fol_line = kml.newfolder(name="The line") #create line ls = fol_line.newlinestring(name='the LineString') ls.coords = points ls.style.linestyle.width = 3 ls.altitudemode = simplekml.AltitudeMode.relativetoground ls.extrude = 0 # 给线串设置默认列表项类型,覆盖父文件夹的单选样式 ls.style.liststyle.listitemtype = simplekml.ListItemType.check # 设置文件夹为单选文件夹 fol_line.style.liststyle.listitemtype = simplekml.ListItemType.radiofolder fol_points.style.liststyle.listitemtype = simplekml.ListItemType.radiofolder #save kml file kml.save("csv_file.kml")
关键修改说明
- 在创建每个点之后,添加
pnt.style.liststyle.listitemtype = simplekml.ListItemType.check,将点的列表项类型设为默认的复选框样式,不再继承父文件夹的单选样式。 - 对线串做同样处理,添加
ls.style.liststyle.listitemtype = simplekml.ListItemType.check,确保线串也使用默认的列表项样式。 - 父文件夹的
radiofolder设置保留,这样两个文件夹会显示单选按钮,而内部的子元素则显示正常的复选框(无单选按钮)。
内容的提问来源于stack exchange,提问作者Toto1515
相关产品推荐
相关产品推荐

