Error while creating KML from a dict using simplekml

661 Views Asked by At

I'm creating kml using the library simplekml. When creating a single one, it works like a charm, but when trying to create one kml per dict entry, returns an error I could not locate. The data have this kind of format:

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

And the script looks like this:

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")

Returns this error:

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

I've been converting the boundaries to strings, using kml.save('key' +".kml")...always the same problem. I don't know what is an Int in all this, I'm starting to thing this is a problem from the library itself? Please and thank you

P.E: Also tried iterating over the enst dict, yielded the same error:

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")
3

There are 3 best solutions below

0
On

The issue is naming the polygon i.e pol = kml.newpolygon(name = key). Since the key is of type int it needs to be converted to string.

pol = kml.newpolygon(name = str(key))

2
On

So as you said it would work outside a loop since you are not iterating through the entire elements of the data dictionary.

The problem is the data held in here.

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

This cannot be used with this syntax for key, value in data.items(): since this only accepts key -> value pairs and your data consists of lists of dictionary.

Read more here on the correct use of the for key, value in data.items():.

To iterate through lists of dictionary, see here and incorporate that idea into your code.

0
On

Ended creating a function, easier to use

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")