Utm.parse constructor
Parses projected UTM coordinates from text, by default in the following
order: zone, hemisphere, easting, northing (ie. 31 N 448251 5411932).
Coordinate values in text are separated by delimiter (default is
whitespace).
If swapXY is true, then swaps x and y parsed from the text.
Use datum to set the datum for calculations with a reference ellipsoid
and datum transformation parameters.
If verifyEN is true it's validated that easting/northing is within
'normal' values (may be suppressed for extended coherent coordinates or
alternative datums e.g. ED50, see epsg.io/23029). The 'normal' values are
roughly 0..1000000m for easting, and 0..9329006m for northing in the
northern hemisphere and 1116914..10000000m in the southern hemisphere.
Throws FormatException if coordinates are invalid.
Examples:
// UTM coordinates with 2D position in zone 31N and WGS84 datum
// (easting 448251.0, northing 5411932.0).
final utmCoord = Utm.parse('31 N 448251 5411932');
// UTM coordinates with 3D position in zone 31N and WGS84 datum
// (easting 448251.0, northing 5411932.0, elevation 100.0).
final utmWithElev = Utm.parse('31 N 448251 5411932 100');
// UTM coordinates with 3D position and measure.
final utmWithElevM = Utm.parse('31 N 448251 5411932 100 5.0');
// With swapped x and y (=> easting 448251.0, northing 5411932.0).
final utmSwapped = Utm.parse('31 N 5411932 448251', swapXY: true);
Implementation
factory Utm.parse(
String text, {
Pattern? delimiter,
bool swapXY = false,
Datum datum = Datum.WGS84,
bool verifyEN = true,
}) {
final parts = text.trim().split(delimiter ?? RegExp(r'\s+'));
if (parts.length < 4 || parts.length > 6) {
throw FormatException('invalid UTM coordinate ‘$text’');
}
final lonZone = int.parse(parts[0]);
final hemisphere = parts[1];
final easting = double.parse(parts[swapXY ? 3 : 2]);
final northing = double.parse(parts[swapXY ? 2 : 3]);
final elev = parts.length >= 5 ? double.parse(parts[4]) : null;
final m = parts.length >= 6 ? double.parse(parts[5]) : null;
return Utm(
lonZone,
hemisphere,
easting,
northing,
elev: elev,
m: m,
datum: datum,
verifyEN: verifyEN,
);
}