Question

I'm trying to create a kml from a xlsx file, but when xlsx contains utf-8 it's not working.

I saw simplekml documentation and it's marked as solved, but I can't make it work.

I've tried setting the encode to ascii, also used django's smart_str and smart_unicode, but nothing worked until now.

I'm reading the file using openpyxl

def create_kml(input_file,sheet_name,output_file,lat_column = 0,lng_column = 1):

    kml = simplekml.Kml()
    wb_read = load_workbook(input_file)
    sh = wb_read.get_sheet_by_name(sheet_name)

    properties = []
    for c in sh.rows[0]:
        properties.append(c.value.encode('utf-8'))

    for p,row in enumerate(sh.rows[1:]):
        for k,c in enumerate(row):
            if k==0:
                coord_tuple = (row[lng_column].value,row[lat_column].value)
                pnt = kml.newpoint(name = 'Point %s' % p, coords =[coord_tuple])
            #TODO: It's not working with unicode and utf-8
            if k != lat_column and k != lng_column:
                if type(row[k].value) == unicode or type(row[k].value) == str:
                    pnt.extendeddata.newdata(properties[k],row[k].value.encode('utf-8'))

    kml.save(output_file)

Traceback:

    Traceback (most recent call last):
    File "<console>", line 1, in <module>
    File "C:/Users/Fernando Alves/Dropbox/Projetos/utils.py", line 296, in create_kml
    kml.save(output_file)
    File "C:\Python27\lib\site-packages\simplekml\kml.py", line 285, in save
    out = self._genkml(format)
    File "C:\Python27\lib\site-packages\simplekml\kml.py", line 198, in _genkml
    kml_str = self._feature.__str__()
    File "C:\Python27\lib\site-packages\simplekml\featgeom.py", line 418, in __str__
    buf.append(feat.__str__())
    File "C:\Python27\lib\site-packages\simplekml\featgeom.py", line 414, in __str__
    buf.append(super(Feature, self).__str__())
    File "C:\Python27\lib\site-packages\simplekml\base.py", line 54, in __str__
    buf.append(u("<{0}>{1}</{0}>").format(var, val))  # Enclose the variable's __str__ with its name
    UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 33: ordinal not in range(128)
Was it helpful?

Solution

I am dealing with this problem myself, but it occurs in some tab delimited data that I am using to build placemarks in KML using simplekml. The primary cause of the problem is that the so-called tab delimited text contains some high order characters. They are being copied into the placemark's description field as they are parsed. When I save the KML file, this error occurs.

There is a discussion of this problem from two years ago in the simplekml issues forum here: http://code.google.com/p/simplekml/issues/detail?id=10

Interestingly, the solution described there as accepted for release (response #6), is not in the current version of simplekml available via pip. Further, when I applied the patch locally it made no difference. Same unicode decode error occurs in base.py.

I can solve my specific occurrence by adding code to filter the tab delimited text prior to using it in simplekml assignments and methods. To summarize, simplekml requires ascii compatible text characters in its strings.

OTHER TIPS

I found the solution, although you have to change the simplekml library...

These are the files you want to change:

/usr/local/lib/python2.7/dist-packages/simplekml/base.py
/usr/local/lib/python2.7/dist-packages/simplekml/kml

/usr/local/lib/python2.7/dist-packages/simplekml/base.py

  • In the first file you have to remove every occurrence of the function u. It exists in lines: 46, 54, 257, 259

/usr/local/lib/python2.7/dist-packages/simplekml/kml

  • In the second file you have to change the line 209 from this: kml_str = xml.dom.minidom.parseString(xml_str.encode("utf-8")) into this: kml_str = xml.dom.minidom.parseString(xml_str)

  • Also you have to remove every occurrence of the function u.


If the problem still exists follow the debugger to remove every occurrence of the function u and it should work fine!

I tried to change the u function, but it did not work... If you find a solution for this please let us know!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top