I am trying to compute the area of the intersection of two polygons as follows:

inter = geomap_ilot.sjoin(df_final_quartiers_arrond)

def f(frame):
    """A function to calculate overlap percentage""" 
    interpct = 100*frame.g1.intersection(frame.g2).area/frame.g1.area
    return area

inter['pct'] = inter.apply(f, axis=1)

I get an error as follows:

TopologyException: Input geom 0 is invalid: Self-intersection at -7.7398160754704346 33.535735521746396

I checked the validity of polygons g1 and g2 and they are all 1 which means they are valid. Then, I tried the buffer(0) function but I am still getting the same error:


Any other suggestion on how I can fix the problem?

  • what is the aim of adding a grid_size? what is the difference with buffer? Both geometries/CRS are 4326 so the areas are indeed in degrees. However, since this is a ratio of areas, the units drop. The result will be the same regardless of the units used I think
    – bravopapa
    Feb 27 at 12:02
  • 1
    I tried the grid_size approach but got this error TypeError: BaseGeometry.intersection() got an unexpected keyword argument 'grid_size'
    – bravopapa
    Feb 27 at 12:22
  • 1
    I tried this adjustment: frame['g1']=frame['g1'].buffer(0.00001) frame['g2']=frame['g2'].buffer(0.00001) interpct = 100*frame.g1.intersection(frame.g2).area/frame.g1.area the function worked fine. It is strange no?
    – bravopapa
    Feb 27 at 12:37
  • 1
    Buffering sometimes work. In QGIS or PostGIS I usually try repairing the geometries, snap them to grid, and/or buffering. Most times it solves the problem
    – BERA
    Feb 27 at 12:54
  • It would be useful if you could share the input polygons giving this issue... possibly/probably there is a bug in geos involved, and then it can be solved.
    – Pieter
    Apr 24 at 6:03


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.