4

I have Python script for converting shapefiles to Geojson. My code works well if I use some usa data, but when I want to use some other encoding, it gives me this error:

Traceback (most recent call last): File "C:\Python33\lib\tkinter__init__.py", line 1475, in call return self.func(*args)
File "C:/Users/name/PycharmProjects/desktopApp/core.py", line 105, in shp2geojson for sr in reader.shapeRecords():
File "C:\Python33\lib\shapefile.py", line 553, in shapeRecords for rec in zip(self.shapes(), self.records())]
File "C:\Python33\lib\shapefile.py", line 525, in records r = self.__record() File "C:\Python33\lib\shapefile.py", line 501, in __record value = u(value) File "C:\Python33\lib\shapefile.py", line 60, in u return v.decode('utf-8')

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe1 in position 10: invalid continuation byte

How to fix this problem?

  import shapefile
  import json

  def shp2geojson():

        filename = askopenfilename()
        reader = shapefile.Reader(filename)
        fields = reader.fields[1:]
        field_names = [field[0] for field in fields]            
        buffer = []

        for sr in reader.shapeRecords():
            atr = dict(zip(field_names, sr.record))
            geom = sr.shape.__geo_interface__
            buffer.append(dict(type="Feature", \
                               geometry=geom, properties=atr))


        geojson = open('shp.json', "w")
        geojson.write(dumps({"type": "FeatureCollection", \
                             "features": buffer}, indent=2) + "\n")
        geojson.close()
2
  • At which line of your code do you get that error? If you have special characters in your buffer, the print command will throw this error.
    – HavardMoe
    Commented Aug 28, 2014 at 10:37
  • Nice function. No need for forward slashes there: line continuation is implicit within parentheses.
    – sgillies
    Commented Aug 28, 2014 at 17:32

1 Answer 1

4

I figured it out so I'm posting the answer here:

geojson = open('shp.json', "w", encoding='utf-8')

Add

encoding='utf-8'

to this line while creating json file. Works great!

2
  • 1
    Great you were able to answer your own question. It usually takes 2 days on GIS.SE be allowed to accept your own answer, but please do so when available to close the thread. Thanks! Commented Aug 28, 2014 at 14:57
  • More details about the Unicode changes in Python 3 at python-notes.curiousefficiency.org/en/latest/python3/….
    – sgillies
    Commented Aug 28, 2014 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.