Latitude/Longitude Format Converter


Features

This calculator parses latitude and longitude coordinates in a variety of formats and outputs a selection of widely used formats. It also provides links to the coordinates on various internet maps: Open Street Map, Google Maps, and Bing Maps.

Try It Out

Here are a variety of global coordinates to demonstrate the cabalibities and variety of accepted input formats. Click on any of the following to see that the given input is parsed:

How It Works

The heart of this converter is a regular expression which matches a coordinate in pretty much any format:
(([NEWS]?)[ \t]*(-?)([0-9]{1,3})(?:[ \t\:°]+([0-9]{1,2}))?(?:[ \t\:\']+([0-9]{1,2}))?(\.[0-9]+)?[ \t°\'\"]*)([NEWS]?)
To break that down:
  1. This regex is applied twice to the input, once to pull out the latitude and a second time to pull out the longitude.
  2. Each coordinate is converted from its input format into decimal degrees.
    1. Seconds are divided by 60 to get minutes
    2. Minutes are divided by 60 to get degrees
  3. Which coordinate is latitude and which is longitute is taken from their positions if not specified by cardinal directions
  4. Further checks on the input are performed:
    1. Degrees are within bounds (-180 to 180 for longitude, -90 to 90 for latitude)
    2. One coordinate is latitude and the other longitude (not two "south" coordinates, for example)
    3. The latitude coordinate is before the longitude coordinate (the two are swapped if they are in the wrong order)
  5. The number of significant figures in the input are calculated so that the output can use the same accuracy as the input
  6. The coordinates are outputted in various formats
    1. Fractional degrees are multiplied by 60 to get minutes
    2. Fractional minutes are multiplied by 60 to get seconds
    3. Decimals are rounded to the same number of significant figures as were in the input
    4. A formatting system is used to replace letters in a format string to easily create various formats
      • D: Decimal degrees
      • d: Degrees without the decimal
      • M: Decimal minutes
      • m: Minutes without the decimal
      • S: Decimal seconds
      • s: Seconds without the decimal
      • g: A negative sign (if present)
      • N: The cardinal direction symbol ("N", "S", "E", or "W")
    So for example "D° N" would represent a decimal degrees format and "gd:m:S" would represent a degrees, minutes, seconds format.

License

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.


More converters, calculators, and other JavaScript goodies
ostermiller.org (site index)

Copyright Stephen Ostermiller 2020-2020