Skip to main content

parseCoordinates

Reads a pair of coordinates from text a person typed, or returns null when the text is not one. It never guesses: text that is out of range, ambiguous, or has anything left over is refused, so a street number, a postcode or a place name goes on to a place search.

This is a function exported by @mapcairn/core, checked against version 0.2.0.

Signature​

import { parseCoordinates } from "@mapcairn/core";

function parseCoordinates(text: string, hemispheres?: Hemispheres, words?: CoordinateWords): LatLng | null;

Description​

What it reads, with the latitude first unless the letters say otherwise:

  • decimal degrees: -26.2041, 28.0473, -26.2041 28.0473, 26.2041° S, 28.0473° E
  • degrees, minutes and seconds: 26°12'14.8"S 28°02'50.3"E, with the usual symbols or the words degrees, minutes and seconds, and decimal minutes when there are no seconds
  • hemisphere letters (N, S, E, W or the words) before or after a value, in either order; a negative sign cannot be combined with a letter. Pass hemispheres (hemispheresFor(catalogue)) to read the letters of the language in use as well, so "26.20410° S, 28.04730° O" reads back in Afrikaans; N, S, E and W are always read. Pass words (coordinateWordsFor(catalogue)) to read that language's whole words for north, south, east, west, degree, minute and second too ("26.2041 suid, 28.0473 oos"); the English words are always read
  • one comma or semicolon between the two values, or white space alone
  • a leading "coordinates" or "coords", so the label of a coordinate result reads back as the same place to about a metre

Every value must carry a mark that says it is a degree value: a decimal point with digits after it, or a degree sign or the word degrees. A bare whole number is not enough, so "12 34", "E1 6" and a postcode or house number go on to the place search. A minus sign (hyphen, a dash or the Unicode minus) starts a value only when it does not touch a number before it, so "02-123" and "12-14" are not pairs.

Other scripts and locales are read too: digits of any script, the Unicode minus and other dashes, Arabic and full-width separators and white space such as the narrow no-break space are rewritten as ASCII first, and bidirectional marks are dropped (see normaliseCoordinateText). A thousands separator is read as white space, since no coordinate has a digit group.

A comma is a decimal mark in two layouts only, and only in text with no decimal point (see withDecimalCommas): values separated by a semicolon (26,2041; 28,0473), or by white space with a comma in every number and no other comma (26,2041 28,0473). A comma between digits is never a thousands separator, so 26,28 and 1,234 alone are not coordinates. Anywhere else a comma separates the two values. Latitude must be within 90 degrees and longitude within 180. Text longer than MAX_COORDINATE_TEXT_LENGTH is refused unread, and the rest is read in a single pass, so the cost grows in step with the text and no hostile input can make it slow.

Parameters​

  • text (string)
  • hemispheres? (Hemispheres)
  • words? (CoordinateWords)

Returns​

LatLng | null: The position, rounded to seven decimals, or null.

See also​

Source: packages/core/src/coordinates/parseCoordinates.ts:517