b'Recode from ANSI 1252 to UTF-8 failed with the error: “Invalid argument”.' geopandas python

喜夏-厌秋 提交于 2019-12-05 06:40:18

Seems that your shapefile contains non-UTF characters that causes the Fiona.open() call to fail (geopandas uses Fiona to open files).

What I did that solved this error was to open the Shapefile (with QGis for example), then selecting save as, and specifying the Encoding option as "UTF-8":

After doing this, I got no error when calling df = gpd.read_file("convertedShape.shp").


Another way to do this without having to use QGis or similar, is to read and save your Shapefile again (effectively converting to the desired format). With OGR you can do something like this:

from osgeo import ogr

driver = ogr.GetDriverByName("ESRI Shapefile")
ds = driver.Open("nbac_2016_r2_20170707_1114.shp", 0) #open your shapefile
#get its layer
layer = ds.GetLayer()

#create new shapefile to convert
ds2 = driver.CreateDataSource('convertedShape.shp')
#create a Polygon layer, as the one your Shapefile has
layer2 = ds2.CreateLayer('', None, ogr.wkbPolygon)
#iterate over all features of your original shapefile
for feature in layer:
   #and create a new feature on your converted shapefile with those features
   layer2.CreateFeature(feature)

ds = layer = ds2 = layer2 = None

This also enabled to successfully open with df = gpd.read_file("convertedShape.shp") after conversion. Hope this helps.

with fiona.open(file, encoding="UTF-8") as f:

worked for me.

As an extension to this answer, you can pass fiona arguments through geopandas read_file:

df = gpd.read_file("filename", encoding="utf-8")

Since you have GDAL installed, I recommend converting the file to UTF-8 using the CLI:

ogr2ogr output.shp input.shp -lco ENCODING=UTF-8

Worked like a charm for me. It's much faster than QGIS or Python and can be applied in a cluster environment.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!