无法使用 GeoPandas 打开形状文件

Fre*_* R. 6 python shapefile geopandas

我似乎无法打开从 ( http://www.vdstech.com/usa-data.aspx )下载的 zip3.zip 形状文件

这是我的代码:

import geopandas as gpd
data = gpd.read_file("data/zip3.shp")
Run Code Online (Sandbox Code Playgroud)

这给了我错误:

CPLE_AppDefinedError: b'Recode from CP437 to UTF-8 failed with the error: "Invalid argument".'
Run Code Online (Sandbox Code Playgroud)

Dar*_*nus 4

根据我对这个问题的回答,您的数据集似乎包含非 UTF 字符。如果您遇到类似的问题,很可能使用不会有帮助,因为菲奥娜的呼叫仍然会失败。encoding-"utf-8"open()

如果其他解决方案不起作用,我建议解决此问题的两个解决方案是:

  1. 在 GIS 编辑器(如 QGis)上打开 shapefile,然后再次保存,确保选择Encoding“UTF-8”选项。此后您调用时应该没有问题gpd.read_file("data/zip3.shp")

  2. 您还可以使用 GDAL 在 Python 中通过读取 shapefile 并再次保存来实现此格式更改。这将有效地将编码更改为 UTF-8,因为这是该方法的文档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 
    
    Run Code Online (Sandbox Code Playgroud)