4

I am trying to read a multi-polygon GeoJSON file in GeoPandas, but I am getting the following error:

'ValueError: A LinearRing must have at least 3 coordinate tuples'

command used is gpd.read_file(layer_file)

What can I do to filter bad geometry? I converted the dataset from pbf to GeoJSON before reading with GeoPandas. The same GeoJSON file works with QGIS.

  • looks like one of your polygon geometry is missing a coordinate point or two. Have you tried saving with QGIS to geoJSON and then to geopandas. – ZrSiO4 Mar 27 '18 at 1:15
7

Figured it out after beating my head for a lot of time.

Change this line

gdf = gpd.read_file(layer_file)

to

#Read data
collection = list(fiona.open(layer_file,'r'))
df1 = pd.DataFrame(collection)

#Check Geometry
def isvalid(geom):
    try:
        shape(geom)
        return 1
    except:
        return 0
df1['isvalid'] = df1['geometry'].apply(lambda x: isvalid(x))
df1 = df1[df1['isvalid'] == 1]
collection = json.loads(df1.to_json(orient='records'))

#Convert to geodataframe
gdf = gpd.GeoDataFrame.from_features(collection)

don't forget to add these libraries

import json
import fiona
from shapely.geometry import shape 
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.