1.1.3 • Published 8 years ago

formatcoords v1.1.3

Weekly downloads
5,949
License
ISC
Repository
github
Last release
8 years ago

formatcoords Build Status

npm.io

A simple and flexible tool to format decimal lat/lon coordinates into degrees/minutes/seconds formats (like DMS), using a moment.js-like API.

For geo hipsters that think that 48° 54′ 16.016″ S 71° 0′ 56.250″ W looks way more awesome than -48.9044488,-71.015625.

install: node/browserify

npm install formatcoords

use

var formatcoords = require('formatcoords');
formatcoords(40.76,-73.984).format();
//40° 45′ 36.000″ N 73° 59′ 2.400″ W

parsing

The formatcoords(coord1[, coord2, lonlat]) method accepts decimal lat/lon coordinates in several formats :

  • formatcoords([lat, lon]): an array of floats.
  • formatcoords([lon, lat], true): an array of floats. First longitude, then latitude. Useful for use with GeoJSON, for example.
  • formatcoords(lat, lon) : floats.
  • formatcoords(lon, lat, true) : floats. First longitude, then latitude. Useful for use with GeoJSON, for example.
  • formatcoords('latlon') : latlon is a string in "lat,lon" format.
  • formatcoords({lat: lat, lng: lon} an object with lat and lng properties (Leaflet LatLng object)

formatting

var coords = formatcoords(27.725499,-18.024301);
coords.format(format, {options})

Default output format is DMS (degrees minutes seconds), with a space to separate lat and lon, and 5 decimal places :

coords.format()
//27° 43′ 31.796″ N 18° 1′ 27.484″ W

Available short formats:

TokenOutput
degrees minutes seconds (DMS)FFf27° 43′ 31.796″ N 18° 1′ 27.484″ W
degrees decimal minutesFf27° 43.529933333333′ N -18° 1.4580666666667′ W
decimal degreesf27.725499° N 18.024301° W

Custom formats:

The following values are available for both latitudes and longitudes:

TokenOutput
degreesD27
degrees with unitDD27°
decimal degreesd27.725499
decimal degrees with unitdd27.725499°
minutesM7
minutes with unitMM7′
decimal minutesm7.63346
decimal minutes with unitmm7.63346′
decimal secondss31.796
decimal seconds with unitss31.796″
directionXN,S, E,W
minus sign (west of Greenwich and south of equator)--

Custom format example

coord.format('-D M s');
//-35 16 55.20000 149 7 43.26240
options
Option NameDescriptionDefault & type
latLonSeparatorThe separator to use between the lat and lon values' ' string
decimalPlacesThe number of decimal places to return5 number

Custom format example with additional options

coord.format('DD MM ss X', {latLonSeparator: ', ',  decimalPlaces: 0);
//35° 43′ 49″ S, 86° 1′ 55″ E

Browser support

IE <= 8 not supported.

DMS to decimal

Use parse-dms.