0

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")
  • The error simply means, somewhere an operation like this `word= 5; word.count()` is taking place. which is obviously not correct. I would advise you to debug the code and locate the line of code the error happens on. – AzyCrw4282 Mar 21 '20 at 22:09
  • this is the whole code. The funny thing is that it works outside a loop, it only breaks once I try to loop. –  Mar 21 '20 at 22:47
  • That simply means that data in the dictionary is of the wrong type at some point. What is in 'Data'? – AzyCrw4282 Mar 21 '20 at 22:49
  • I'll edit and put a fragment of data –  Mar 21 '20 at 23:10
  • Do see my answer and let me know if you need any help. – AzyCrw4282 Mar 22 '20 at 00:15

3 Answers3

0

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.

AzyCrw4282
  • 7,222
  • 5
  • 19
  • 35
  • Tried several ways of iterating the nested dict, same problem. I really appreciate your help, btw, you're very kind –  Mar 22 '20 at 07:02
  • if this answer helped solve your problem, please mark it as accepted by clicking the check mark next to the answer. see [here](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) for more information – AzyCrw4282 Mar 22 '20 at 09:46
0

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

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

Manu
  • 11
  • 1