Unable to open geojson file with geopandas, getting TypeError

1.5k views Asked by At

I'm trying to open a geojson file into geopandas but getting the following error message:

 Traceback (most recent call last):
 File "C:\Users\arobe\Anaconda3\envs\test_env\lib\site-packages\geopandas\io\file.py", line 95, in read_file
 gdf = GeoDataFrame.from_features(f_filt, crs=crs, columns=columns)
 File "C:\Users\arobe\Anaconda3\envs\test_env\lib\site-packages\geopandas\geodataframe.py", line 283, in from_features
 for f in features_lst:
 File "fiona/ogrext.pyx", line 1369, in fiona.ogrext.Iterator.__next__
 File "fiona/ogrext.pyx", line 232, in fiona.ogrext.FeatureBuilder.build
 TypeError: startswith first arg must be bytes or a tuple of bytes, not str

The solutions here geopandas cannot read a geojson properly_ have not worked for me and I've tried all manner of encodings. The data is from UK MSOA dataset (https://geoportal.statistics.gov.uk/datasets/f341dcfd94284d58aba0a84daf2199e9_2/geoservice?page=720).

The data downloads fine and works ok in Tableau. It also looks to be ok when opened in Notepad++ so it doesn't appear to be a data issue but I'm new to this so really don't know what I'm doing!

Any help would be much appreciated.

Code snippet:

 gdf=gpd.read_file("https://opendata.arcgis.com/datasets/f341dcfd94284d58aba0a84daf2199e9_2.geojson")
 print(gdf.head(10))

 gdf.to_file("msoa.geojson", driver='GeoJSON')

 gdf2=gpd.read_file("msoa.geojson"
             ,driver='GeoJSON'
             )

 print(gdf2.head(10))
2

There are 2 answers

2
swatchai On BEST ANSWER

I run your code on Linux using geopandas v0.8.1, fiona v1.8.17. All OK. The simple plot is as follows.

gb-plot

0
A Rob4 On

It transpires that there was, for some reason, some strange characters in the downloaded data. I manually removed them as they were for an area I wasn't interested in and it now works. The solution was found here https://github.com/geopandas/geopandas/issues/621