fix_polygon

Geometric functions > fix_polygon

syntax

  • fix_polygon(polygon_data_item)

description

fix_polygon(polygon_data_item) is fix_winding_order followed by geometry repair: it derives the ring roles from their nesting, applies the clockwise-outer / counter-clockwise-hole convention, and then repairs whatever is still topologically invalid using GEOS MakeValid. The result has the same domain unit and values unit as the argument.

This is the “fix geometry” of the pair: use it when the input has self-intersecting rings, holes that stick out of their shell, or overlapping outer rings — the things ring reordering alone cannot settle. A bow-tie, for instance, comes out as the two separate lobes it describes.

Unlike fix_winding_order, fix_polygon does move coordinates: repairing a self-intersection means introducing the intersection point. That is why the two are separate operators rather than one with a flag — after fix_polygon you cannot assume the vertices are the ones you put in. Repairs are reported as warnings, so it is visible which features were changed and why.

The repair only runs on the elements that need it. Chaining geos_polygon(fix_winding_order(g)) in configuration would pay for MakeValid on every row; fix_polygon knows from the nesting pass which elements are already valid and passes those straight through.

applies to

conditions

  1. The composition type of the polygon_data_item needs to be polygon.
  2. Only dpoint coordinates are supported. MakeValid introduces intersection points that an integer coordinate grid cannot represent, so integer point types are deliberately not accepted; use fix_winding_order, which never introduces a point and works for every coordinate type.

since version

20.18

example

attribute<dpoint> geometry_fixed (poly, district) := fix_polygon(district/geometry);
district/geometry area(…, m2) area(geometry_fixed, m2)
bow-tie, crossing at centre 0 50
outer CW, lake CW (flipped) 140 68

see also