Polygon.parseCoords constructor

Polygon.parseCoords(
  1. Iterable<String> rings, {
  2. Pattern delimiter = ',',
  3. Coords type = Coords.xy,
  4. bool swapXY = false,
  5. bool singlePrecision = false,
})

Parses a polygon geometry from rings with linear rings formatted as texts containing coordinate values separated by delimiter.

Use the required optional type to explicitely set the coordinate type.

If swapXY is true, then swaps x and y for all positions in the result.

If singlePrecision is true, then coordinate values of positions are stored in Float32List instead of the Float64List (default).

Examples:

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

// a polygon (with an exterior and one interior ring) from 2D positions
Polygon.parseCoords(
  [
    // an exterior ring with values of five (x, y) positions
    '10.0,20.0,'
    '12.5,22.5,'
    '15.0,25.0,'
    '11.5,27.5,'
    '10.0,20.0',

    // an interior ring with values of four (x, y) positions
    '12.5,23.0,'
    '11.5,24.0,'
    '12.5,24.0,'
    '12.5,23.0'
  ],
  type: Coords.xy,
);

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

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

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

// a polygon (with an exterior ring only) from 2D positions using an
// alternative delimiter
Polygon.parseCoords(
  [
    // an exterior ring with values of five (x, y) positions
    '10.0;20.0;'
    '12.5;22.5;'
    '15.0;25.0;'
    '11.5;27.5;'
    '10.0;20.0'
  ],
  type: Coords.xy,
  delimiter: ';',
);

// a polygon (with an exterior ring only) from 2D positions with x before
// y
Polygon.parseCoords(
  [
    // an exterior ring with values of five (x, y) positions
    '20.0,10.0,'
    '22.5,12.5,'
    '25.0,15.0,'
    '27.5,11.5,'
    '20.0,10.0'
  ],
  type: Coords.xy,
  swapXY: true,
);

// a polygon (with an exterior ring only) from 2D positions with the
// internal storage using single precision floating point numbers
// (`Float32List` in this case)
Polygon.parseCoords(
  [
    // an exterior ring with values of five (x, y) positions
    '10.0,20.0,'
    '12.5,22.5,'
    '15.0,25.0,'
    '11.5,27.5,'
    '10.0,20.0'
  ],
  type: Coords.xy,
  singlePrecision: true,
);

Implementation

factory Polygon.parseCoords(
  Iterable<String> rings, {
  Pattern delimiter = ',',
  Coords type = Coords.xy,
  bool swapXY = false,
  bool singlePrecision = false,
}) {
  if (rings.isEmpty) {
    return Polygon.build(const []);
  } else {
    return Polygon(
      rings
          .map(
            (ring) => parsePositionSeriesFromTextDim1(
              ring,
              delimiter: delimiter,
              type: type,
              swapXY: swapXY,
              singlePrecision: singlePrecision,
            ),
          )
          .toList(growable: false),
    );
  }
}