3

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.

sakurashinken
  • 3,940
  • 8
  • 34
  • 67
  • Can you explain why is `.buffer(0)` not sufficient. What is the limitation you find to it in your problem? – Eskapp Jun 20 '19 at 18:24
  • It will remove repeated points but not loops – sakurashinken Jun 20 '19 at 21:48
  • Shapely Polygons do not support loops as far as I know (if by loops you mean self-intersecting polygons). Did you check MultiPolygon in shapely? With this, you could have the loops represented as separate polygons and then do whichever processing you need. Each polygon in the MultiPolygon is valid. A graphical example would help a lot to understand your question. – Eskapp Jun 21 '19 at 12:30

1 Answers1

2

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)
Mike T
  • 41,085
  • 18
  • 152
  • 203