Standard Toolkit
Packages@accelint/geoCoordinatesLatlonDecimal degrees

parseDecimalDegrees

Parses Decimal Degrees coordinate strings with validation for ranges and bearing indicators.

Parses a Decimal Degrees coordinate string with validation for ranges, bearing indicators, and format consistency.

Usage

import { parseDecimalDegrees } from '@accelint/geo';

// Parse with explicit format
const [coords, errors] = parseDecimalDegrees('37.7749° N / 122.4194° W', 'LATLON');
// coords: ['37.7749', 'N', '/', '122.4194', 'W']
// errors: []

Reference

function parseDecimalDegrees(
  input: string,
  format: 'LATLON' | 'LONLAT'
): [tokens: string[], errors: string[]]

Parameters

ParameterTypeDescription
inputstringRaw coordinate string to parse
format'LATLON' | 'LONLAT'Expected format ordering

Returns

Returns a tuple containing:

  • tokens: Array of parsed coordinate components (degrees, bearings, dividers)
  • errors: Array of validation error messages (empty if valid)

Validation Rules

Validates that:

  • Degree values are within acceptable ranges (±90 for latitude, ±180 for longitude)
  • Minutes indicator (') is not present (invalid for Decimal Degrees)
  • Seconds indicator (") is not present (invalid for Decimal Degrees)
  • Bearing indicators match the specified format (N/S for latitude, E/W for longitude)
  • Negative signs on degrees are converted to appropriate bearing directions

Examples

Example: Basic parsing

import { parseDecimalDegrees } from '@accelint/geo';

const [coords, errors] = parseDecimalDegrees('37.7749° N / 122.4194° W', 'LATLON');
// coords: ['37.7749', 'N', '/', '122.4194', 'W']
// errors: []

Example: Without degree symbols

import { parseDecimalDegrees } from '@accelint/geo';

const [coords, errors] = parseDecimalDegrees('45.5 N, 122.6 W', 'LATLON');
// coords: ['45.5', 'N', '/', '122.6', 'W']
// errors: []

Example: Negative values convert to bearings

import { parseDecimalDegrees } from '@accelint/geo';

const [coords, errors] = parseDecimalDegrees('-45.5 75.3', 'LATLON');
// coords: ['45.5', 'S', '/', '75.3', 'E']
// errors: []

Example: LONLAT format

import { parseDecimalDegrees } from '@accelint/geo';

const [coords, errors] = parseDecimalDegrees('180 E / 90 N', 'LONLAT');
// coords: ['180', 'E', '/', '90', 'N']
// errors: []

Example: Out of range latitude

import { parseDecimalDegrees } from '@accelint/geo';

const [coords, errors] = parseDecimalDegrees('91 / 0', 'LATLON');
// coords: []
// errors: ['[ERROR] Degrees value (91) exceeds max value (90).']

Example: Out of range longitude

import { parseDecimalDegrees } from '@accelint/geo';

const [coords, errors] = parseDecimalDegrees('180 , 90', 'LONLAT');
// coords: []
// errors: ['[ERROR] Degrees value (181) exceeds max value (180).']

Example: Invalid minutes indicator

import { parseDecimalDegrees } from '@accelint/geo';

const [coords, errors] = parseDecimalDegrees('40° 26\' N, 79° 58\' W', 'LATLON');
// coords: []
// errors: ['[ERROR] Minutes indicator (\') not valid in Decimal Degrees.']

Example: Invalid seconds indicator

import { parseDecimalDegrees } from '@accelint/geo';

const [coords, errors] = parseDecimalDegrees('40° 26" N, 79° 58" W', 'LATLON');
// coords: []
// errors: ['[ERROR] Seconds indicator (") not valid in Decimal Degrees.']

Example: Format mismatch

import { parseDecimalDegrees } from '@accelint/geo';

const [coords, errors] = parseDecimalDegrees('1 E / 2 N', 'LATLON');
// coords: []
// errors: ['[ERROR] Mismatched formats: "LATLON" expected, "LONLAT" found.']

Example: Flexible separators

import { parseDecimalDegrees } from '@accelint/geo';

// Comma separator
parseDecimalDegrees('45.5 N, 90.5 W', 'LATLON');

// Slash separator
parseDecimalDegrees('45.5 N / 90.5 W', 'LATLON');

// Space separator
parseDecimalDegrees('45.5 N 90.5 W', 'LATLON');

Good to know: The parser accepts various separators (comma, slash, or space) between coordinate components. Negative signs on degrees are automatically converted to appropriate bearing directions (S for negative latitude, W for negative longitude).

Good to know: Decimal Degrees format does NOT allow minutes (') or seconds (") indicators. Use parseDegreesDecimalMinutes or parseDegreesMinutesSeconds for those formats.

On this page