Packages@accelint/geoCoordinatesMgrs
parseMGRS
Parses Military Grid Reference System (MGRS) coordinate strings into latitude/longitude values.
Parses Military Grid Reference System (MGRS) coordinate strings and converts them to latitude/longitude decimal degrees with detailed validation.
Usage
import { parseMGRS } from '@accelint/geo';
// Parse MGRS coordinate
const [coords, errors] = parseMGRS(null, '31U BF 12345 67890');
// coords: ['48.123456', 'N', '/', '11.234567', 'E']
// errors: []Reference
function parseMGRS(
_format: any,
input: string
): [tokens: string[], errors: string[]]Parameters
| Parameter | Type | Description |
|---|---|---|
_format | any | Unused format parameter (MGRS has fixed format) |
input | string | MGRS coordinate string to parse (e.g., '31U BF 12345 67890') |
Returns
Returns a tuple containing:
tokens: Array with latitude/longitude values and bearing indicatorserrors: Array of validation error messages (empty if valid)
MGRS Format
MGRS coordinates consist of:
- Grid Zone Designation (2-3 chars): UTM zone number (01-60) + latitude band letter (C-X, excluding I and O)
- 100K Square Identification (2 chars): Two-letter square identifier (A-Z, excluding I and O)
- Numerical Location: Easting and northing values within the 100km square
Expected format: DDZ AA DDD DDD
Example: 31U BF 12345 67890
Validation Rules
Validates that:
- UTM zone number is between 1 and 60
- Latitude band letter is valid (C-H, J-N, P-X)
- 100K meter square identification uses valid letters (A-H, J-N, P-Z)
- Numerical location (easting and northing) are positive numbers
Examples
Example: Valid MGRS coordinate
import { parseMGRS } from '@accelint/geo';
const [coords, errors] = parseMGRS(null, '31U BF 12345 67890');
// coords: ['48.123456', 'N', '/', '11.234567', 'E']
// errors: []Example: New York City coordinates
import { parseMGRS } from '@accelint/geo';
const [coords, errors] = parseMGRS(null, '18T WL 80000 00000');
// coords: ['40.7128', 'N', '/', '73.9352', 'W']
// errors: []Example: Invalid UTM zone
import { parseMGRS } from '@accelint/geo';
const [coords, errors] = parseMGRS(null, '99Z AA 12345 67890');
// coords: []
// errors: ['[ERROR] Invalid UTM zone number (99) found in grid zone designation; expected format DDZ AA DDD DDD.']Example: Invalid latitude band
import { parseMGRS } from '@accelint/geo';
const [coords, errors] = parseMGRS(null, '31I AA 12345 67890');
// coords: []
// errors: ['[ERROR] Invalid Latitude band letter (I) found in grid zone designation; expected format DDZ AA DDD DDD.']Example: Invalid 100K square identification
import { parseMGRS } from '@accelint/geo';
const [coords, errors] = parseMGRS(null, '31U OI 12345 67890');
// coords: []
// errors: ['[ERROR] Invalid 100K m square identification (OI) found; expected format DDZ AA DDD DDD.']Example: Invalid numerical location
import { parseMGRS } from '@accelint/geo';
const [coords, errors] = parseMGRS(null, '31U AA 0 0');
// coords: []
// errors: ['[ERROR] Invalid numerical location (0,0) found; expected format DDZ AA DDD DDD.']Example: Missing components
import { parseMGRS } from '@accelint/geo';
const [coords, errors] = parseMGRS(null, '31U');
// coords: []
// errors: ['[ERROR] Invalid 100K m square identification () found; expected format DDZ AA DDD DDD.']Example: Empty input
import { parseMGRS } from '@accelint/geo';
const [coords, errors] = parseMGRS(null, '');
// coords: []
// errors: ['[ERROR] No input provided; expected format DDZ AA DDD DDD.']Good to know: MGRS uses the geodesy library for conversion. The coordinate system automatically handles UTM zone calculations and converts to precise latitude/longitude values.
Good to know: Letters I and O are excluded from MGRS to avoid confusion with digits 1 and 0. Valid latitude bands exclude polar regions (A, B, Y, Z are not used in standard MGRS).
Related
- parseUTM - Parse Universal Transverse Mercator coordinates
- parseDecimalDegrees - Parse Decimal Degrees coordinates
- createCoordinate - Create coordinate with automatic parsing
- coordinateSystems - Available coordinate systems