Polygon.from constructor

Polygon.from(
  1. Iterable<Iterable<Position>> rings, {
  2. Box? bounds,
})

A polygon geometry with one exterior and 0 to N interior rings.

An optional bounds can used set a minimum bounding box for a geometry.

Each ring in the polygon is represented by an Iterable<Position> instance.

An empty polygon has no rings.

For "normal" polygons the rings list must be non-empty. The first element is the exterior ring, and any other rings are interior rings (or holes). All rings must be closed linear rings. As specified by GeoJSON, they should "follow the right-hand rule with respect to the area it bounds, i.e., exterior rings are counterclockwise, and holes are clockwise".

Examples:

// a polygon (with an exterior ring only) from 2D positions
Polygon.from(
  [
    // an exterior ring with five (x, y) positions
    [
      [10.0, 20.0].xy,
      [12.5, 22.5].xy,
      [15.0, 25.0].xy,
      [11.5, 27.5].xy,
      [10.0, 20.0].xy,
    ],
  ],
);

// a polygon (with an exterior and one interior ring) from 2D positions
Polygon.from(
  [
    // an exterior ring with five (x, y) positions
    [
      [10.0, 20.0].xy,
      [12.5, 22.5].xy,
      [15.0, 25.0].xy,
      [11.5, 27.5].xy,
      [10.0, 20.0].xy,
    ],
    // an interior ring with four (x, y) positions
    [
      [12.5, 23.0].xy,
      [11.5, 24.0].xy,
      [12.5, 24.0].xy,
      [12.5, 23.0].xy,
    ],
  ],
);

// a polygon (with an exterior ring only) from 3D positions
Polygon.from(
  [
    // an exterior ring with five (x, y, z) positions
    [
      [10.0, 20.0, 30.0].xyz,
      [12.5, 22.5, 32.5].xyz,
      [15.0, 25.0, 35.0].xyz,
      [11.5, 27.5, 37.5].xyz,
      [10.0, 20.0, 30.0].xyz,
    ],
  ],
);

// a polygon (with an exterior ring only) from measured 2D positions
Polygon.from(
  [
    // an exterior ring with five (x, y, m) positions
    [
      [10.0, 20.0, 40.0].xym,
      [12.5, 22.5, 42.5].xym,
      [15.0, 25.0, 45.0].xym,
      [11.5, 27.5, 47.5].xym,
      [10.0, 20.0, 40.0].xym,
    ],
  ],
);

// a polygon (with an exterior ring only) from measured 3D positions
Polygon.from(
  [
    // an exterior ring with five (x, y, z, m) positions
    [
      [10.0, 20.0, 30.0, 40.0].xyzm,
      [12.5, 22.5, 32.5, 42.5].xyzm,
      [15.0, 25.0, 35.0, 45.0].xyzm,
      [11.5, 27.5, 37.5, 47.5].xyzm,
      [10.0, 20.0, 30.0, 40.0].xyzm,
    ],
  ],
);

Implementation

factory Polygon.from(
  Iterable<Iterable<Position>> rings, {
  Box? bounds,
}) =>
    Polygon(
      rings.map(PositionSeries.from).toList(growable: false),
      bounds: bounds,
    );