Name

CG_PolygonRepair — Repair an invalid polygon or multipolygon.

Synopsis

geometry CG_PolygonRepair(geometry geom, text rule);

Description

Repair an invalid polygon or multipolygon using CGAL's 2D Polygon Repair algorithm. Returns a MultiPolygon.

The rule parameter controls the repair strategy:

  • EVEN_ODD (default) — areas covered an odd number of times are kept. Available with CGAL 6.0+.

  • NON_ZERO — areas with non-zero winding number are kept. Requires CGAL 6.1+.

  • UNION — union of all input polygons. Requires CGAL 6.1+.

  • INTERSECTION — intersection of all input polygons. Requires CGAL 6.1+.

Availability: 3.7.0 - requires SFCGAL >= 2.3.0 and CGAL >= 6.0.

This method needs SFCGAL backend.

This function supports Polyhedral surfaces.

Examples

Repair a self-intersecting bowtie polygon with the default EVEN_ODD rule.

Code
WITH data AS (
  SELECT 'POLYGON((0 0,2 2,2 0,0 2,0 0))'::geometry AS input_polygon
)
SELECT input_polygon AS input_polygon,
       CG_PolygonRepair(input_polygon) AS repaired_polygon
FROM data;
Output
POLYGON((0 0,2 2,2 0,0 2,0 0)) | MULTIPOLYGON(((0 0,1 1,0 2,0 0)),((1 1,2 0,2 2,1 1)))
Figure
Geometry figure for visual-cg-polygonrepair-01

With the UNION rule, all winding area is kept. This rule requires CGAL 6.1 or later.

Code
WITH data AS (
  SELECT 'POLYGON((0 0,2 2,2 0,0 2,0 0))'::geometry AS input_polygon
)
SELECT input_polygon AS input_polygon,
       CG_PolygonRepair(input_polygon, 'UNION') AS repaired_polygon
FROM data;
Output
POLYGON((0 0,2 2,2 0,0 2,0 0)) | MULTIPOLYGON(((0 0,1 1,0 2,0 0)))
Figure
Geometry figure for visual-cg-polygonrepair-02