使用simplekml从字典创建KML时出错

问题描述 投票:0回答:2

我正在使用库simplekml创建kml。当创建一个单个的时,它就像一个超级按钮,但是当尝试为每个字典条目创建一个kml时,返回我找不到的错误。数据具有这种格式:

{12: {900: [(-5.4529673, 4.46),
   (-3.4529799, 40.454),
   (-3.495, 33),
   (-3.45471, 40.437)]},
29: {900: [(-3.452....}

脚本看起来像这样:

import simplekml
kml = simplekml.Kml()

for key, value in data.items():
    pol = kml.newpolygon(name = key)
    pol.outerboundaryis = data[key][900]
    pol.innerboundaryis = []
    print(pol.outerboundaryis)
    pol.style.linestyle.color = simplekml.Color.green
    pol.style.linestyle.width = 5
    pol.style.polystyle.color = simplekml.Color.changealphaint(100, simplekml.Color.green)
    print(pol.name)
    kml.save(str(pol.name) +".kml")

返回此错误:

AttributeError: 'int' object has no attribute 'count'

我一直使用kml.save('key'+“。kml”)将边界转换为字符串...总是相同的问题。我不知道这是什么Int,我开始觉得这是库本身的问题吗?请,谢谢

P.E:还尝试遍历enst字典,产生相同的错误:

import simplekml
kml = simplekml.Kml()

for key, value in data.items():
    for key2, value2 in value.items():
        pol = kml.newpolygon(name = key)
        pol.outerboundaryis = value2
        pol.innerboundaryis = []
        print(pol.outerboundaryis)
        pol.style.linestyle.color = simplekml.Color.green
        pol.style.linestyle.width = 5
        pol.style.polystyle.color = simplekml.Color.changealphaint(100, simplekml.Color.green)
        kml.save(str(pol.name) +".kml")
python kml simplekml
2个回答
0
投票

因此,正如您所说的那样,由于您没有遍历data字典的所有元素,因此可以在循环外运行。

问题是这里保存的数据。

{12: {900: [(-5.4529673, 4.46),
   (-3.4529799, 40.454),
   (-3.495, 33),
   (-3.45471, 40.437)]},
29: {900: [(-3.452....}

不能与此语法for key, value in data.items():一起使用,因为它仅接受key -> value对,并且您的数据由字典列表组成。

有关正确使用here的更多信息,请阅读for key, value in data.items():

要遍历字典列表,请参阅here并将该想法合并到您的代码中。


0
投票

结束创建函数,更易于使用

def kmlprinter(coordenadas):

kml = simplekml.Kml()
pol = kml.newpolygon(name="laputetxemadrequeparioaloscuñadosdeSO")
pol.outerboundaryis = coordenadas.values()
pol.innerboundaryis = []
pol.style.linestyle.color = simplekml.Color.green
pol.style.linestyle.width = 5
pol.style.polystyle.color = simplekml.Color.changealphaint(100, simplekml.Color.green)
kml.save("1.kml")  
© www.soinside.com 2019 - 2024. All rights reserved.