createFromObject<R extends Position> static method
R
createFromObject<R extends Position>(
- Object position, {
- required CreatePosition<
R> to, - Coords? type,
Creates a position of R
from position
(of R
or Iterable<num>
).
If position
is R
and with compatible coordinate type already, then
it's returned. Other Position
instances are copied as R
.
If position
is Iterable<num>
, then a position instance is created
using the factory function to
. Supported coordinate value combinations:
(x, y), (x, y, z), (x, y, m) and (x, y, z, m).
Use an optional type
to explicitely set the coordinate type. If not
provided and an iterable has 3 items, then xyz coordinates are assumed.
Otherwise throws FormatException
.
Implementation
static R createFromObject<R extends Position>(
Object position, {
required CreatePosition<R> to,
Coords? type,
}) {
if (position is Position) {
if (position is R && (type == null || type == position.coordType)) {
// position is of R and with compatiable coord type
return position;
} else {
if (type == null) {
// create a copy with same coordinate values
return position.copyTo(to);
} else {
// create a copy with z and m selected if coord type suggests so
return to.call(
x: position.x,
y: position.y,
z: type.is3D ? position.z : null,
m: type.isMeasured ? position.m : null,
);
}
}
} else if (position is Iterable<num>) {
// create position from iterable of num values
return buildPosition(position, to: to, type: type);
}
throw invalidCoordinates;
}