代码之家  ›  专栏  ›  技术社区  ›  Kernel

如果指定了crs参数,shapefile的geopandas.read_file将出错

  •  1
  • Kernel  · 技术社区  · 5 月前

    所有, 我使用ESRI世界国家通用形状文件,该文件可用 here 使用GeoPandas

    shp_file =gpd.read_file('World_Countries/World_Countries_Generalized.shp')
    print(shp_file.crs)
    

    我得到的CRS是 EPSG:3857 ,但是一旦我将CRS添加到gpd.read_file中,如下所示

    shp_file1 =gpd.read_file('../../Downloads/World_Countries/World_Countries_Generalized.shp',crs='EPSG:3857')
    

    我得到了以下错误 /opt/anaconda3/envs/geo_env/lib/python3.12/site-packages/pyogrio/raw.py:198: RuntimeWarning: driver ESRI Shapefile does not support open option CRS return ogr_read(

    你知道我为什么会遇到这个错误吗?这是否意味着文件读取不正确?

    谢谢

    1 回复  |  直到 5 月前
        1
  •  1
  •   Pieter    5 月前

    自geopandas 1.0以来,默认情况下使用另一个更快的底层库来读取geopandas中的文件: pyogrio 。更多信息可以在这里找到: fiona vs pyogrio 当使用这个新库时 crs 参数不受支持。

    对于这种特定情况,最简单的解决方案是删除 crs='EPSG:3857' 参数原样无论如何都是无用的,因为crs已经被正确读取了?

    shp_file1 = gpd.read_file('../../Downloads/World_Countries/World_Countries_Generalized.shp')
    

    如果你想读取一个没有.prj文件或错误的shapefile,你可以使用 set_crs 功能 GeoDataFrame 设定或推翻 crs 读取文件后:

    shp_file2 = gpd.read_file('wrong_crs.shp').set_crs(crs='EPSG:3857', allow_override=True)