6

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:

points[points.geometry.within(polygons.unary_union)]

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

polygons.unary_union

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

gene
  • 54,868
  • 3
  • 110
  • 187
Tnk
  • 61
  • 1
  • 2

1 Answers1

10

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, :]
user2856
  • 65,736
  • 6
  • 115
  • 196