pythonpython-3.xgeopandasshapefile

geopandas.read_file of a shapefile gives error if crs parameter is specified


All, I use ESRI World Countries Generalized shape file, that is available here using GeoPandas

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

The CRS I got is EPSG:3857, yet once I add the CRS to the gpd.read_file as the following

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

I got the following error /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(

Do you know why I get this error, and does it mean the file is not read correctly?

Thanks


Solution

  • Since geopandas 1.0, another, faster, underlying library is used by default to read files in geopandas: pyogrio. Some more info can be found here: fiona vs pyogrio. When this new library is used, the crs parameter is not supported.

    The easiest solution for this specific case is to just remove the crs='EPSG:3857' parameter as is is useless anyway as the crs is already read correctly?

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

    If you want to read a shapefile that doesn't have a .prj file, or a wrong one, you can e.g. use the set_crs function of the GeoDataFrame to set or overrule the crs after reading the file:

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