我似乎无法打开从 ( http://www.vdstech.com/usa-data.aspx ) 下载的 zip3.zip 形状文件
这是我的代码:
import geopandas as gpd
data = gpd.read_file("data/zip3.shp")
这给了我错误:
CPLE_AppDefinedError: b'Recode from CP437 to UTF-8 failed with the error: "Invalid argument".'
最佳答案
根据我的 answer在 this问题,您的数据集似乎包含非 UTF 字符。如果您遇到类似的问题,使用 encoding-"utf-8"
可能无济于事,因为 Fiona 的 open()
调用仍然会失败。
如果其他解决方案不起作用,我建议解决此问题的两个解决方案是:
在 GIS 编辑器(如 QGis)上打开您的 shapefile,然后再次保存它,确保您选择
Encoding
选项为“UTF-8”。之后调用gpd.read_file("data/zip3.shp")
应该没有问题。您还可以使用 GDAL 在 Python 中实现这种格式更改,方法是读取您的 shapefile 并再次保存它。这将有效地将编码更改为 UTF-8,因为这是 docs 中指示的默认编码。对于
CreateDataSource()
方法。为此,请尝试以下代码片段: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) #proper closing ds = layer = ds2 = layer2 = None
关于python - 无法使用 GeoPandas 打开形状文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48305400/