[GIS] Unicode Decode Error for shapefile in Python

encodinggeojsonpythonshapefileunicode

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

Best Answer

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!

Related Question