Skip to content

parsePosition

Signature

js
parsePosition(pattern)

Description

Parses a string containing two coordinate values separated by a delimiter (,, ;, or |) and returns a GeoJSON position [longitude, latitude]. Each part is parsed independently using parseCoordinate, and the axis of each value is inferred using guessCoordinateAxis.

If both axes can be determined unambiguously, the coordinates are reordered to [longitude, latitude] regardless of the input order. If neither axis can be determined (both values are in the [-90, 90] range with no direction hint), two candidate positions are returned.

Returns null if the pattern cannot be parsed.

Parameters

NameTypeRequiredDescription
patternstringyesA non-empty string containing two coordinate values separated by ,, ;, or `

Returns

TypeDescription
number[]A GeoJSON position [longitude, latitude]
number[][]Two candidate positions if the axis of both values is ambiguous
nullIf the pattern cannot be parsed or does not contain exactly two values

Throws

Throws if pattern is not a non-empty string.

Examples

js
// Latitude, longitude with direction symbols
parsePosition('48.8566N, 2.3522E')
// [2.3522, 48.8566]
js
// Longitude first
parsePosition('2.3522E, 48.8566N')
// [2.3522, 48.8566]
js
// Plain decimal, unambiguous — one value > 90 is treated as longitude
parsePosition('48.8566, 120.5')
// [120.5, 48.8566]
js
// Semicolon delimiter
parsePosition('48.8566N; 2.3522E')
// [2.3522, 48.8566]
js
// Pipe delimiter
parsePosition('48.8566N | 2.3522E')
// [2.3522, 48.8566]
js
// South and West directions produce negative values
parsePosition('34.6037S, 58.3816W')
// [-58.3816, -34.6037]
js
// Ambiguous — both values in [-90, 90], no direction hint
parsePosition('45, 12')
// [[45, 12], [12, 45]]
js
// Invalid — only one part
parsePosition('48.8566')
// null
js
// Invalid — unparseable coordinate
parsePosition('abc, def')
// null

isValidPosition

Signature

js
isValidPosition (value)

Description

Returns true if value is a valid GeoJSON position: an array of 2 or 3 finite numbers where longitude is in [-180, 180] and latitude is in [-90, 90]. Altitude, if present, must be a finite number.

Parameters

NameTypeRequiredDescription
value*yesThe value to check

Returns

TypeDescription
booleantrue if the value is a valid GeoJSON position

Examples

js
isValidPosition([2.3522, 48.8566])        // true
isValidPosition([2.3522, 48.8566, 100])   // true
isValidPosition([181, 48.8566])           // false — longitude out of range
isValidPosition([2.3522, 91])             // false — latitude out of range
isValidPosition([2.3522])                 // false — too few elements
isValidPosition(null)                     // false

is3DPosition

Signature

js
is3DPosition (position)

Description

Returns true if position is a valid GeoJSON position with an altitude coordinate (3 elements).

Parameters

NameTypeRequiredDescription
positionnumber[]yesA valid GeoJSON position

Returns

TypeDescription
booleantrue if the position has 3 coordinates

Throws

Throws if position is not a valid GeoJSON position.

Examples

js
is3DPosition([2.3522, 48.8566, 100])  // true
is3DPosition([2.3522, 48.8566])       // false
is3DPosition(null)                    // throws

isSamePosition

Signature

js
isSamePosition (position1, position2, options)

Description

Returns true if two GeoJSON positions are equal within the given decimal precision. By default only longitude and latitude are compared; altitude comparison can be enabled via consider3D.

Parameters

NameTypeRequiredDescription
position1number[]yesA valid GeoJSON position
position2number[]yesA valid GeoJSON position
optionsobjectnoComparison options
options.precisionnumbernoDecimal places for comparison (default: 10)
options.consider3DbooleannoWhether to include altitude in comparison (default: false)

Returns

TypeDescription
booleantrue if the positions are equal within the given precision

Throws

Throws if either position is not a valid GeoJSON position, or if options is invalid.

Examples

js
isSamePosition([2.3522, 48.8566], [2.3522, 48.8566])
// true

isSamePosition([2.3522, 48.8566, 100], [2.3522, 48.8566, 200])
// true — altitude ignored by default

isSamePosition([2.3522, 48.8566, 100], [2.3522, 48.8566, 200], { consider3D: true })
// false — altitudes differ

isSamePosition([2.352, 48.856], [2.353, 48.857], { precision: 2 })
// true — equal at 2 decimal places

isSamePosition([2.352, 48.856], [2.353, 48.857], { precision: 3 })
// false — differ at 3 decimal places

truncatePosition

Signature

js
truncatePosition (position, precision)

Description

Truncates each coordinate of a position to the given number of decimal places. Supports both 2D and 3D positions. The operation is performed in place — the original array is mutated and returned.

Parameters

NameTypeRequiredDescription
positionnumber[]yesA valid GeoJSON position [lon, lat] or [lon, lat, alt]
precisionnumbernoNumber of decimal places to keep, in range [0, 8] (default: 7)

Returns

TypeDescription
number[]The mutated position

Throws

Throws if position is not a valid position. Throws if precision is not in range [0, 8].

Examples

js
// Default precision (7)
truncatePosition([2.352212345, 48.856612345])
// [2.3522123, 48.8566123]
js
// Custom precision
truncatePosition([2.352212345, 48.856612345], 3)
// [2.352, 48.857]
js
// 3D position
truncatePosition([2.352212345, 48.856612345, 100.123456789])
// [2.3522123, 48.8566123, 100.1234568]
js
// Mutates in place
const position = [2.352212345, 48.856612345]
const result = truncatePosition(position, 3)
result === position // true