b'Recode from ANSI 1252 to UTF-8 failed with the error: "Invalid argument".' geopandas python
b'Recode from ANSI 1252 to UTF-8 failed with the error: "Invalid argument".' geopandas python
我正在尝试将 shapefile 读入 GeoDataFrame。
通常我只是这样做并且有效:
import pandas as pd
import geopandas as gpd
from shapely.geometry import Point
df = gpd.read_file("wild_fires/nbac_2016_r2_20170707_1114.shp")
但是这次它给我错误:b'Recode from ANSI 1252 to UTF-8 failed with the error: "Invalid argument".'
完整错误:
---------------------------------------------------------------------------
CPLE_AppDefinedError Traceback (most recent call last)
<ipython-input-14-adcad0275d30> in <module>()
----> 1 df_wildfires_2016 = gpd.read_file("wild_fires/nbac_2016_r2_20170707_1114.shp")
/usr/local/lib/python3.6/site-packages/geopandas/io/file.py in read_file(filename, **kwargs)
19 """
20 bbox = kwargs.pop('bbox', None)
---> 21 with fiona.open(filename, **kwargs) as f:
22 crs = f.crs
23 if bbox is not None:
/usr/local/lib/python3.6/site-packages/fiona/__init__.py in open(path, mode, driver, schema, crs, encoding, layer, vfs, enabled_drivers, crs_wkt)
163 c = Collection(path, mode, driver=driver, encoding=encoding,
164 layer=layer, vsi=vsi, archive=archive,
--> 165 enabled_drivers=enabled_drivers)
166 elif mode == 'w':
167 if schema:
/usr/local/lib/python3.6/site-packages/fiona/collection.py in __init__(self, path, mode, driver, schema, crs, encoding, layer, vsi, archive, enabled_drivers, crs_wkt, **kwargs)
151 if self.mode == 'r':
152 self.session = Session()
--> 153 self.session.start(self)
154 elif self.mode in ('a', 'w'):
155 self.session = WritingSession()
fiona/ogrext.pyx in fiona.ogrext.Session.start (fiona/ogrext2.c:8432)()
fiona/_err.pyx in fiona._err.GDALErrCtxManager.__exit__ (fiona/_err.c:1861)()
CPLE_AppDefinedError: b'Recode from ANSI 1252 to UTF-8 failed with the error: "Invalid argument".'
一段时间以来,我一直在努力弄清楚为什么会出现错误,但似乎找不到答案。
数据取自这个网页我只下载了2016年的link:http://cwfis.cfs.nrcan.gc.ca/datamart/download/nbac?token=78e9bd6af67f71204e18cb6fa4e47515
有人能帮我吗?谢谢你。
您的 shapefile 似乎包含导致 Fiona.open()
调用失败的非 UTF 字符(geopandas 使用 Fiona 打开文件)。
我解决这个错误的方法是打开 Shapefile(例如使用 QGis),然后选择 save as
,并将 Encoding
选项指定为 "UTF-8":
这样做之后,我在调用 df = gpd.read_file("convertedShape.shp")
时没有错误。
无需使用 QGis 或类似工具即可执行此操作的另一种方法是再次读取并保存 Shapefile(有效地转换为所需格式)。使用 OGR,您可以执行以下操作:
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
这样也可以在转换后使用 df = gpd.read_file("convertedShape.shp")
成功打开。希望这会有所帮助。
由于您安装了 GDAL,我建议使用 CLI 将文件转换为 UTF-8:
ogr2ogr output.shp input.shp -lco ENCODING=UTF-8
对我来说就像一个魅力。比QGIS或Python快很多,而且可以在集群环境下应用。
with fiona.open(file, encoding="UTF-8") as f:
对我有用。
作为 , you can pass fiona arguments through geopandas read_file 的扩展:
df = gpd.read_file("filename", encoding="utf-8")
我正在尝试将 shapefile 读入 GeoDataFrame。
通常我只是这样做并且有效:
import pandas as pd
import geopandas as gpd
from shapely.geometry import Point
df = gpd.read_file("wild_fires/nbac_2016_r2_20170707_1114.shp")
但是这次它给我错误:b'Recode from ANSI 1252 to UTF-8 failed with the error: "Invalid argument".'
完整错误:
---------------------------------------------------------------------------
CPLE_AppDefinedError Traceback (most recent call last)
<ipython-input-14-adcad0275d30> in <module>()
----> 1 df_wildfires_2016 = gpd.read_file("wild_fires/nbac_2016_r2_20170707_1114.shp")
/usr/local/lib/python3.6/site-packages/geopandas/io/file.py in read_file(filename, **kwargs)
19 """
20 bbox = kwargs.pop('bbox', None)
---> 21 with fiona.open(filename, **kwargs) as f:
22 crs = f.crs
23 if bbox is not None:
/usr/local/lib/python3.6/site-packages/fiona/__init__.py in open(path, mode, driver, schema, crs, encoding, layer, vfs, enabled_drivers, crs_wkt)
163 c = Collection(path, mode, driver=driver, encoding=encoding,
164 layer=layer, vsi=vsi, archive=archive,
--> 165 enabled_drivers=enabled_drivers)
166 elif mode == 'w':
167 if schema:
/usr/local/lib/python3.6/site-packages/fiona/collection.py in __init__(self, path, mode, driver, schema, crs, encoding, layer, vsi, archive, enabled_drivers, crs_wkt, **kwargs)
151 if self.mode == 'r':
152 self.session = Session()
--> 153 self.session.start(self)
154 elif self.mode in ('a', 'w'):
155 self.session = WritingSession()
fiona/ogrext.pyx in fiona.ogrext.Session.start (fiona/ogrext2.c:8432)()
fiona/_err.pyx in fiona._err.GDALErrCtxManager.__exit__ (fiona/_err.c:1861)()
CPLE_AppDefinedError: b'Recode from ANSI 1252 to UTF-8 failed with the error: "Invalid argument".'
一段时间以来,我一直在努力弄清楚为什么会出现错误,但似乎找不到答案。
数据取自这个网页我只下载了2016年的link:http://cwfis.cfs.nrcan.gc.ca/datamart/download/nbac?token=78e9bd6af67f71204e18cb6fa4e47515
有人能帮我吗?谢谢你。
您的 shapefile 似乎包含导致 Fiona.open()
调用失败的非 UTF 字符(geopandas 使用 Fiona 打开文件)。
我解决这个错误的方法是打开 Shapefile(例如使用 QGis),然后选择 save as
,并将 Encoding
选项指定为 "UTF-8":
这样做之后,我在调用 df = gpd.read_file("convertedShape.shp")
时没有错误。
无需使用 QGis 或类似工具即可执行此操作的另一种方法是再次读取并保存 Shapefile(有效地转换为所需格式)。使用 OGR,您可以执行以下操作:
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
这样也可以在转换后使用 df = gpd.read_file("convertedShape.shp")
成功打开。希望这会有所帮助。
由于您安装了 GDAL,我建议使用 CLI 将文件转换为 UTF-8:
ogr2ogr output.shp input.shp -lco ENCODING=UTF-8
对我来说就像一个魅力。比QGIS或Python快很多,而且可以在集群环境下应用。
with fiona.open(file, encoding="UTF-8") as f:
对我有用。
作为
df = gpd.read_file("filename", encoding="utf-8")