从ansi1252到UTF8的b'Recode失败,错误为:“无效参数”。'geopandas python

2024-10-03 09:08:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试着把形状文件读入地理数据框。在

通常我只需要这样做,它就会起作用:

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".'

完全错误:

^{pr2}$

我一直想弄清楚为什么有一段时间会出错,但似乎找不到答案。在

数据是从这个网页获得的我只下载了2016年的链接:http://cwfis.cfs.nrcan.gc.ca/datamart/download/nbac?token=78e9bd6af67f71204e18cb6fa4e47515

有人能帮我吗?谢谢您。在


Tags: 文件数据fromimportpandasas错误地理
3条回答
with fiona.open(file, encoding="UTF-8") as f:

为我工作。在

作为this answer的扩展,可以通过geopandas read_file传递fiona参数:

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

似乎您的SuffEFILE包含非UTF字符,这些字符导致^ {CD1>}调用失败(GeopDaas使用菲奥娜打开文件)。在

解决这个错误的方法是打开Shapefile(例如使用QGis),然后选择save as,并将Encoding选项指定为“UTF-8”:

enter image description here

这样做之后,我在调用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")成功打开。希望这有帮助。在

相关问题 更多 >