Search code examples
pythonpostgisshapely

How to run st_makevalid in shapely or similar pure python library?


Is there a method in shapely or a similar library that is exactly equivalent to postGIS method st_makevalid? As it stands, using .buffer(0) as recommended here is not enough. Are there any pure python implementations of this function? As it stands, I can access the postgis functions directly by binding from ctypes, but this is a cumbersome solution as I must compile and install from source. A better solution would be very welcome.


Solution

  • Shapely >=1.8 has this feature. See the docs here.

    from shapely.geometry import Polygon
    from shapely.validation import make_valid
    
    invalid_poly = Polygon([(0, 2), (0, 1), (2, 0), (0, 0), (0, 2)])
    valid_poly = make_valid(invalid_poly)