PositionSeries.from constructor

PositionSeries.from(
  1. Iterable<Position> source, {
  2. Coords? type,
})

A series of positions as a view backed by source containing Position objects.

If given type is null then the coordinate type of source positions is resolved from those positions (a type returned is such that it's valid for all positions). For an empty source the Coords.xy type is used if type is missing.

If source is List<Position> then it's used directly as a source for a new PositionSeries object. If source is Iterable<Position> then items are iterated and copied to a list that is used as a source.

See Position for description about supported coordinate values.

Examples:

// a series of 2D positions
PositionSeries.from(
  [
    Position.create(x: 10.0, y: 20.0),
    Position.create(x: 12.5, y: 22.5),
    Position.create(x: 15.0, y: 25.0),
  ],
  type: Coords.xy,
);

// a series of 3D positions
PositionSeries.from(
  [
    Position.create(x: 10.0, y: 20.0, z: 30.0),
    Position.create(x: 12.5, y: 22.5, z: 32.5),
    Position.create(x: 15.0, y: 25.0, z: 35.0),
  ],
  type: Coords.xyz,
);

// a series of measured 2D positions
PositionSeries.from(
  [
    Position.create(x: 10.0, y: 20.0, m: 40.0),
    Position.create(x: 12.5, y: 22.5, m: 42.5),
    Position.create(x: 15.0, y: 25.0, m: 45.0),
  ],
  type: Coords.xym,
),

// a series of measured 3D positions
PositionSeries.from(
  [
    Position.create(x: 10.0, y: 20.0, z: 30.0, m: 40.0),
    Position.create(x: 12.5, y: 22.5, z: 32.5, m: 42.5),
    Position.create(x: 15.0, y: 25.0, z: 35.0, m: 45.0),
  ],
  type: Coords.xyzm,
);

Implementation

factory PositionSeries.from(Iterable<Position> source, {Coords? type}) {
  // ensure a list data structure
  final data =
      source is List<Position> ? source : source.toList(growable: false);

  if (type != null) {
    return _PositionArray.view(data, type: type);
  } else if (data.isEmpty) {
    return _PositionArray.view(data, type: Coords.xy);
  } else {
    var is3D = true;
    var isMeasured = true;

    for (final elem in data) {
      final type = elem.coordType;
      is3D &= type.is3D;
      isMeasured &= type.isMeasured;
      if (!is3D && !isMeasured) break;
    }

    return _PositionArray.view(
      data,
      type: Coords.select(is3D: is3D, isMeasured: isMeasured),
    );
  }
}