I am trying to find the points from a geopandas frame that are inside the polygons from another geopandas frame. The code that I am using is the following:


Most of the times it works, but for one file it gives me this error:

TopologyException: Input geom 0 is invalid: Ring Self-intersection at or near point -71.126795046686823 42.767412284376533 at -71.126795046686823 42.767412284376533

this error is raised when I do


My understanding is that (at least) one of the polygons is invalid.

I don't mind if I drop some polygons, I don't need to be 100% accurate. The question is: How do I drop the rows from a geopandas frame that are raising the TopologyException

The polygon dataframe is coming from: https://docs.digital.mass.gov/dataset/massgis-data-2016-land-coverland-use

The particular shp file with problems: http://download.massgis.digital.mass.gov/shapefiles/lclu2016/LCLU_R02C18.zip


You can check for valid geometries using is_valid.

You could use the buffer(0) trick (1, 2) to repair the invalid geometries:

gdf = gpd.read_file(shp)

invalid_geom = ~gdf['geometry'].is_valid
gdf.loc[invalid_geom, 'geometry'] = gdf.loc[invalid_geom, 'geometry'].buffer(0)

If you prefer to drop them:

gdf = gdf.loc[gdf['geometry'].is_valid, :]
  • Very good and useful answer, I wish the OP understood how to accept it. – umbe1987 Mar 2 at 8:19

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.