python - 无法使用 GeoPandas 打开形状文件

标签 python shapefile geopandas

我似乎无法打开从 ( 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".'

最佳答案

根据我的 answerthis问题,您的数据集似乎包含非 UTF 字符。如果您遇到类似的问题,使用 encoding-"utf-8" 可能无济于事,因为 Fiona 的 open() 调用仍然会失败。

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

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

  2. 您还可以使用 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/

相关文章:

python - 匹配字符串中的多个模式

PHP - 将 xy .shp 坐标转换为 Google map lat/lng

r - 使用R操作shapefile属性表

python - Plotly:如何为离散分类变量设置等值线图颜色?

jquery - 最好的Python库来清理标签(不安全),并保留我认为安全的标签

python - 确定在 groupby 之后的时间范围内是否出现两种类型

r - 从空间多边形数据框中绘制一个多边形

python - Pandas : difference() methode between polygon and points

python - 在 Folium HeatMapWithTime 中显示日期、id 列和其他列

python - 如何引发自定义字典 ​​KeyError 消息