[Geopandas error]fiona.errors.DriverError: '/vsimem/3563f91543824520abdaa032ab1a68da' not recognized as a supported file format

5.9k Views Asked by At

I wanted to read the .shp files by the file_uploader of streamlit.

  1. Get the list of shp files from the file_uploader of streamlit.
  2. Read the shp files using the geopandas.

Here's my code.

st.session_state.data_01 = st.file_uploader('Please choose a file.', accept_multiple_files=True, key='0').       
df = []
for d in st.session_state.data_01:
    df.append(gpd.read_file(d),encoding='utf-8')

And I got the error such like:

  File "/Users/icuh/Desktop/Eun/Web/life.py", line 17, in run_life
    df.append(gpd.read_file(d),encoding='utf-8')
  File "/Users/icuh/opt/anaconda3/envs/impacts_02/lib/python3.8/site-packages/geopandas/io/file.py", line 253, in _read_file
    return _read_file_fiona(
  File "/Users/icuh/opt/anaconda3/envs/impacts_02/lib/python3.8/site-packages/geopandas/io/file.py", line 294, in _read_file_fiona
    with reader(path_or_bytes, **kwargs) as features:
  File "/Users/icuh/opt/anaconda3/envs/impacts_02/lib/python3.8/site-packages/fiona/collection.py", line 555, in __init__
    super(BytesCollection, self).__init__(self.virtual_file, vsi=filetype, **kwds)
  File "/Users/icuh/opt/anaconda3/envs/impacts_02/lib/python3.8/site-packages/fiona/collection.py", line 162, in __init__
    self.session.start(self, **kwargs)
  File "fiona/ogrext.pyx", line 540, in fiona.ogrext.Session.start
  File "fiona/_shim.pyx", line 90, in fiona._shim.gdal_open_vector
fiona.errors.DriverError: '/vsimem/3563f91543824520abdaa032ab1a68da' not recognized as a supported file format.

Versions I use

  • python 3.8.6
  • geopandas 0.11.1
  • fiona 1.8.21
  • shapely 1.8.4
1

There are 1 best solutions below

1
Rob Raymond On

This is not a streamlit issue as such.

  • have simulated the error you stated with geopandas sample shape file
  • this fails when shape file has no extension .shp. Same error you reported
  • try again with an extension .shp. Different error, partner files missing (`.shx', '.prj', ...)
  • try again where all files are in same directory as .shp. Suceeds

Your upload capability needs to take into account that a shape file is a set of files (not a single file). Either ensure they are all uploaded into same directory. Alternatively zip them up and upload zip file. read_file() supports zip files.

import geopandas as gpd
import fiona
from pathlib import Path
import tempfile
import shutil

with tempfile.TemporaryDirectory() as tmpdirname:
    fn = Path(gpd.datasets.get_path("naturalearth_lowres"))
    tmp_file = Path(tmpdirname).joinpath(fn.stem)
    shutil.copy(fn, tmp_file)
    print(tmp_file) # temp file with no extension...
    try:
        gpd.read_file(tmp_file)
    except fiona.errors.DriverError as e:
        print(e)
        tmp_file.unlink()
        # now just the shape file
        tmp_file = Path(tmpdirname).joinpath(fn.name)
        shutil.copy(fn, tmp_file)

        try:
            gpd.read_file(tmp_file)
        
        except fiona.errors.DriverError as e:
            print(e)
            # now all the files that make up an ESRI shapefile
            for fn_ in fn.parent.glob("*"):
                print(fn_.name)
                shutil.copy(fn_, tmpdirname)
            gpd.read_file(tmp_file)
            # no exception :-)

output

/var/folders/3q/trbn3hyn0y91jwvh6gfn7ln40000gn/T/tmpa8x28emo/naturalearth_lowres
'/var/folders/3q/trbn3hyn0y91jwvh6gfn7ln40000gn/T/tmpa8x28emo/naturalearth_lowres' not recognized as a supported file format.
Unable to open /var/folders/3q/trbn3hyn0y91jwvh6gfn7ln40000gn/T/tmpa8x28emo/naturalearth_lowres.shx or /var/folders/3q/trbn3hyn0y91jwvh6gfn7ln40000gn/T/tmpa8x28emo/naturalearth_lowres.SHX. Set SHAPE_RESTORE_SHX config option to YES to restore or create it.
naturalearth_lowres.shx
naturalearth_lowres.cpg
naturalearth_lowres.shp
naturalearth_lowres.dbf
naturalearth_lowres.prj