# @conveyal/lonlat

> Lon/lat normalization

Latest version **1.4.1** (published 2021-07-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install @conveyal/lonlat
pnpm add @conveyal/lonlat
yarn add @conveyal/lonlat
bun add @conveyal/lonlat
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.4.1 |
| Published | 2021-07-28 |
| First published | 2016-10-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 146.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | Conveyal |
| Maintainers | trevorgerhardt, mattwigway, landonreed, david.emory, evansiroky |
| Keywords | lat, lon, lng, coordinate, point |

## Links

- npm: https://www.npmjs.com/package/@conveyal/lonlat
- Repository: https://github.com/conveyal/lonlat
- Issues: https://github.com/conveyal/lonlat/issues
- npm.io page: https://npm.io/package/@conveyal/lonlat

## Alternatives

- [messageformat](https://npm.io/package/messageformat.md) — 329.7K weekly downloads
- [@mintlify/scraping](https://npm.io/package/@mintlify/scraping.md) — 294.8K weekly downloads
- [@mintlify/previewing](https://npm.io/package/@mintlify/previewing.md) — 209.5K weekly downloads
- [@mintlify/prebuild](https://npm.io/package/@mintlify/prebuild.md) — 209.5K weekly downloads
- [@mintlify/link-rot](https://npm.io/package/@mintlify/link-rot.md) — 206.3K weekly downloads

## Recent versions

- 1.4.1 (latest) — 2021-07-28
- 1.4.0 — 2018-10-19
- 1.3.0 — 2017-03-09
- 1.2.0 — 2017-03-08
- 1.1.2 — 2017-02-25
- 1.1.1 — 2017-02-15
- 1.1.0 — 2016-12-23
- 1.0.0 — 2016-10-29

## README

# lonlat

[![NPM version][npm-image]][npm-url]
[![Build status][travis-image]][travis-url]

[npm-image]: https://img.shields.io/npm/v/@conveyal/lonlat.svg?maxAge=2592000&style=flat-square

[npm-url]: https://www.npmjs.com/package/@conveyal/lonlat

[travis-image]: https://img.shields.io/travis/conveyal/lonlat.svg?style=flat-square

[travis-url]: https://travis-ci.org/conveyal/lonlat

Lon/lat normalization cause...**sigh**.

No one has agreed on a standard way of representing lon/lat. This is a small normalization library. Use this to convert all outside input before processing internally and convert to an external format right when it's being output.

## API

<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

#### Table of Contents

*   [lonlat.types.input](#lonlattypesinput)
*   [lonlat.types.output](#lonlattypesoutput)
    *   [Properties](#properties)
*   [lonlat.types.point](#lonlattypespoint)
    *   [Properties](#properties-1)
*   [lonlat.types.InvalidCoordinateException](#lonlattypesinvalidcoordinateexception)
*   [conveyal/lonlat](#conveyallonlat)
    *   [Parameters](#parameters)
    *   [Examples](#examples)
    *   [fromCoordinates](#fromcoordinates)
        *   [Parameters](#parameters-1)
        *   [Examples](#examples-1)
    *   [fromLatlng](#fromlatlng)
        *   [Parameters](#parameters-2)
        *   [Examples](#examples-2)
    *   [fromPoint](#frompoint)
        *   [Parameters](#parameters-3)
        *   [Examples](#examples-3)
    *   [fromString](#fromstring)
        *   [Parameters](#parameters-4)
        *   [Examples](#examples-4)
    *   [fromLatFirstString](#fromlatfirststring)
        *   [Parameters](#parameters-5)
        *   [Examples](#examples-5)
    *   [isEqual](#isequal)
        *   [Parameters](#parameters-6)
        *   [Examples](#examples-6)
    *   [print](#print)
        *   [Parameters](#parameters-7)
        *   [Examples](#examples-7)
    *   [toCoordinates](#tocoordinates)
        *   [Parameters](#parameters-8)
        *   [Examples](#examples-8)
    *   [toLeaflet](#toleaflet)
        *   [Parameters](#parameters-9)
        *   [Examples](#examples-9)
    *   [toPoint](#topoint)
        *   [Parameters](#parameters-10)
        *   [Examples](#examples-10)
    *   [toString](#tostring)
        *   [Parameters](#parameters-11)
        *   [Examples](#examples-11)
    *   [toLatFirstString](#tolatfirststring)
        *   [Parameters](#parameters-12)
        *   [Examples](#examples-12)
    *   [toPixel](#topixel)
        *   [Parameters](#parameters-13)
        *   [Examples](#examples-13)
    *   [fromPixel](#frompixel)
        *   [Parameters](#parameters-14)
        *   [Examples](#examples-14)
*   [PIXELS_PER_TILE](#pixels_per_tile)
*   [PIXELS_PER_TILE](#pixels_per_tile-1)
*   [longitudeToPixel](#longitudetopixel)
    *   [Parameters](#parameters-15)
    *   [Examples](#examples-15)
*   [latitudeToPixel](#latitudetopixel)
    *   [Parameters](#parameters-16)
    *   [Examples](#examples-16)
*   [MAX_LAT](#max_lat)
*   [pixelToLongitude](#pixeltolongitude)
    *   [Parameters](#parameters-17)
    *   [Examples](#examples-17)
*   [pixelToLatitude](#pixeltolatitude)
    *   [Parameters](#parameters-18)
    *   [Examples](#examples-18)

### lonlat.types.input

(type)

An unknown type of input.  Must be one of the following:

*   array: An array with 2 elements.  The first is longitude, the second is latitude.
*   string: A string in the format `longitude,latitude`
*   Object (x, y coordinates): An object with `x` and `y` properties.
    `x` represents longitude and `y` represents latitude.
*   Object (lat, lon): An object with latitude and longitude properties.
    The properties can be named various things.
    For longitude any of the following are valid: `lon`, `lng` or `longitude`
    For latitude any of the following are valid: `lat` or `latitude`

Type: ([Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array) | [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) | [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object))

### lonlat.types.output

(type)

Standardized lon/lat object.

Type: [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)

#### Properties

*   `lat` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** 
*   `lon` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** 

### lonlat.types.point

(type)

Object with x/y number values.

Type: [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)

#### Properties

*   `x` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** 
*   `y` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** 

### lonlat.types.InvalidCoordinateException

(exception type)

An error that is thrown upon providing invalid coordinates.

Type: [Error](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error)

### conveyal/lonlat

Parse an unknown type of input.

#### Parameters

*   `unknown` **[lonlat.types.input](#lonlattypesinput)** 

#### Examples

```javascript
var lonlat = require('@conveyal/lonlat')

// Object with lon/lat-ish attributes
var position = lonlat({ lon: 12, lat: 34 })         // { lon: 12, lat: 34 }
position = lonlat({ lng: 12, lat: 34 })             // { lon: 12, lat: 34 }
position = lonlat({ longitude: 12, latitude: 34 })  // { lon: 12, lat: 34 }
position = lonlat({ lng: 12, latitude: 34 })        // { lon: 12, lat: 34 }

// coordinate array
position = lonlat([12, 34])   // { lon: 12, lat: 34 }

// string
position = lonlat('12,34')   // { lon: 12, lat: 34 }

// object with x and y attributes
position = lonlat({ x: 12, y: 34 })   // { lon: 12, lat: 34 }

// the following will throw errors
position = lonlat({ lon: 999, lat: 34 })   // Error: Invalid longitude value: 999
position = lonlat({ lon: 12, lat: 999 })   // Error: Invalid latitude value: 999
position = lonlat({})                      // Error: Invalid latitude value: undefined
position = lonlat(null)                    // Error: Value must not be null or undefined
```

*   Throws **[lonlat.types.InvalidCoordinateException](#lonlattypesinvalidcoordinateexception)** 

Returns **[lonlat.types.output](#lonlattypesoutput)** 

#### fromCoordinates

<b>aliases:</b> fromGeoJSON<br>

Tries to parse from an array of coordinates.

##### Parameters

*   `coordinates` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)** An array in the format: \[longitude, latitude]

##### Examples

```javascript
var lonlat = require('@conveyal/lonlat')

var position = lonlat.fromCoordinates([12, 34])   // { lon: 12, lat: 34 }
position = lonlat.fromGeoJSON([12, 34])           // { lon: 12, lat: 34 }
```

*   Throws **[lonlat.types.InvalidCoordinateException](#lonlattypesinvalidcoordinateexception)** 

Returns **[lonlat.types.output](#lonlattypesoutput)** 

#### fromLatlng

<b>aliases:</b> fromLeaflet<br>

Tries to parse from an object.

##### Parameters

*   `lonlat` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** An object with a `lon`, `lng` or `longitude` attribute and a `lat` or `latitude` attribute

##### Examples

```javascript
var lonlat = require('@conveyal/lonlat')

var position = lonlat.fromLatlng({ longitude: 12, latitude: 34 })   // { lon: 12, lat: 34 }
position = lonlat.fromLeaflet({ lng: 12, lat: 34 })                 // { lon: 12, lat: 34 }
```

*   Throws **[lonlat.types.InvalidCoordinateException](#lonlattypesinvalidcoordinateexception)** 

Returns **[lonlat.types.output](#lonlattypesoutput)** 

#### fromPoint

Tries to parse from an object.

##### Parameters

*   `point` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** An object with a `x` attribute representing `longitude`
    and a `y` attribute representing `latitude`

##### Examples

```javascript
var lonlat = require('@conveyal/lonlat')

var position = lonlat.fromPoint({ x: 12, y: 34 })   // { lon: 12, lat: 34 }
```

*   Throws **[lonlat.types.InvalidCoordinateException](#lonlattypesinvalidcoordinateexception)** 

Returns **[lonlat.types.output](#lonlattypesoutput)** 

#### fromString

<b>aliases:</b> fromLonFirstString<br>

Tries to parse from a string where the longitude appears before the latitude.

##### Parameters

*   `str` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** A string in the format: `longitude,latitude`

##### Examples

```javascript
var lonlat = require('@conveyal/lonlat')

var position = lonlat.fromString('12,34')          // { lon: 12, lat: 34 }
var position = lonlat.fromLonFirstString('12,34')  // { lon: 12, lat: 34 }
```

*   Throws **[lonlat.types.InvalidCoordinateException](#lonlattypesinvalidcoordinateexception)** 

Returns **[lonlat.types.output](#lonlattypesoutput)** 

#### fromLatFirstString

Tries to parse from a string where the latitude appears before the longitude.

##### Parameters

*   `str` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** A string in the format: `latitude,longitude`

##### Examples

```javascript
var lonlat = require('@conveyal/lonlat')

var position = lonlat.fromLatFirstString('12,34') // { lon: 34, lat: 12 }
```

*   Throws **[lonlat.types.InvalidCoordinateException](#lonlattypesinvalidcoordinateexception)** 

Returns **[lonlat.types.output](#lonlattypesoutput)** 

#### isEqual

Determine if two inputs are equal to each other

##### Parameters

*   `lonlat1` **[lonlat.types.input](#lonlattypesinput)** 
*   `lonlat2` **[lonlat.types.input](#lonlattypesinput)** 
*   `epsilon` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** The maximum acceptable deviation to be considered equal. (optional, default `0`)

##### Examples

```javascript
var lonlat = require('@conveyal/lonlat')

var isEqual = lonlat.isEqual('12,34', [12, 34])   // true
```

*   Throws **[lonlat.types.InvalidCoordinateException](#lonlattypesinvalidcoordinateexception)** 

Returns **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** 

#### print

##### Parameters

*   `input` **[lonlat.types.input](#lonlattypesinput)** 
*   `fixed` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** The number of decimal places to round to. (optional, default `5`)

##### Examples

```javascript
var lonlat = require('@conveyal/lonlat')

var pretty = lonlat.print('12.345678,34')   // '12.34568, 34.00000'
```

*   Throws **[lonlat.types.InvalidCoordinateException](#lonlattypesinvalidcoordinateexception)** 

Returns **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** A string with in the format `longitude,latitude` rounded to
the number of decimal places as specified by `fixed`

#### toCoordinates

<b>aliases:</b> toGeoJSON<br>

Translates to a coordinate array.

##### Parameters

*   `input` **[lonlat.types.input](#lonlattypesinput)** 

##### Examples

```javascript
var lonlat = require('@conveyal/lonlat')

var coords = lonlat.toCoordinates('12,34')   // [12, 34]
```

*   Throws **[lonlat.types.InvalidCoordinateException](#lonlattypesinvalidcoordinateexception)** 

Returns **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)** An array in the format \[longitude, latitude]

#### toLeaflet

Translates to [Leaflet LatLng](http://leafletjs.com/reference-1.0.3.html#latlng) object.
This function requires Leaflet to be installed as a global variable `L` in the window environment.

##### Parameters

*   `input` **[lonlat.types.input](#lonlattypesinput)** 

##### Examples

```javascript
var lonlat = require('@conveyal/lonlat')

var position = lonlat.toLeaflet({ lat: 12, long: 34 })   // Leaflet LatLng object
```

*   Throws **[lonlat.types.InvalidCoordinateException](#lonlattypesinvalidcoordinateexception)** 

Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** A Leaflet LatLng object

#### toPoint

Translates to point Object.

##### Parameters

*   `input` **[lonlat.types.input](#lonlattypesinput)** 

##### Examples

```javascript
var lonlat = require('@conveyal/lonlat')

var point = lonlat.toPoint('12,34')   // { x: 12, y: 34 }
```

*   Throws **[lonlat.types.InvalidCoordinateException](#lonlattypesinvalidcoordinateexception)** 

Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** An object with `x` and `y` attributes representing latitude and longitude respectively

#### toString

<b>aliases:</b> toLonFirstString<br>

Translates to coordinate string where the longitude appears before latitude.

##### Parameters

*   `input` **[lonlat.types.input](#lonlattypesinput)** 

##### Examples

```javascript
var lonlat = require('@conveyal/lonlat')

var str = lonlat.toString({ lat: 12, longitude: 34 })          // '34,12'
var str = lonlat.toLonFirstString({ lat: 12, longitude: 34 })  // '34,12'
```

*   Throws **[lonlat.types.InvalidCoordinateException](#lonlattypesinvalidcoordinateexception)** 

Returns **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** A string in the format 'longitude,latitude'

#### toLatFirstString

Translates to coordinate string where the latitude appears before longitude.

##### Parameters

*   `input` **[lonlat.types.input](#lonlattypesinput)** 

##### Examples

```javascript
var lonlat = require('@conveyal/lonlat')

var str = lonlat.toLatFirstString({ lat: 12, longitude: 34 })  // '12,34'
```

*   Throws **[lonlat.types.InvalidCoordinateException](#lonlattypesinvalidcoordinateexception)** 

Returns **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** A string in the format 'longitude,latitude'

#### toPixel

Convert a coordinate to a pixel.

##### Parameters

*   `input` **[lonlat.types.input](#lonlattypesinput)** 
*   `zoom` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** 

##### Examples

```javascript
var pixel = lonlat.toPixel({lon: -70, lat: 40}, 9) //= {x: 40049.77777777778, y:49621.12736343896}
```

*   Throws **[lonlat.types.InvalidCoordinateException](#lonlattypesinvalidcoordinateexception)** 
*   Throws **[Error](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error)** If latitude is above or below `MAX_LAT`
*   Throws **[Error](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error)** If `zoom` is undefined.

Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** An object with `x` and `y` attributes representing pixel coordinates

#### fromPixel

From pixel.

##### Parameters

*   `pixel` **[lonlat.types.point](#lonlattypespoint)** 
*   `zoom` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** 

##### Examples

```javascript
var ll = lonlat.fromPixel({x: 40000, y: 50000}, 9) //= {lon: -70.13671875, lat: 39.1982053488948}
```

Returns **[lonlat.types.output](#lonlattypesoutput)** 

### PIXELS_PER_TILE

Pixel conversions and constants taken from
<https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Implementations>

### PIXELS_PER_TILE

Pixels per tile.

### longitudeToPixel

Convert a longitude to it's pixel value given a `zoom` level.

#### Parameters

*   `longitude` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** 
*   `zoom` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** 

#### Examples

```javascript
var xPixel = lonlat.longitudeToPixel(-70, 9) //= 40049.77777777778
```

Returns **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** pixel

### latitudeToPixel

Convert a latitude to it's pixel value given a `zoom` level.

#### Parameters

*   `latitude` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** 
*   `zoom` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** 

#### Examples

```javascript
var yPixel = lonlat.latitudeToPixel(40, 9) //= 49621.12736343896
```

Returns **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** pixel

### MAX_LAT

Maximum Latitude for valid Mercator projection conversion.

### pixelToLongitude

Convert a pixel to it's longitude value given a zoom level.

#### Parameters

*   `x` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** 
*   `zoom` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** 

#### Examples

```javascript
var lon = lonlat.pixelToLongitude(40000, 9) //= -70.13671875
```

Returns **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** longitude

### pixelToLatitude

Convert a pixel to it's latitude value given a zoom level.

#### Parameters

*   `y` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** 
*   `zoom` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** 

#### Examples

```javascript
var lat = lonlat.pixelToLatitude(50000, 9) //= 39.1982053488948
```

Returns **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** latitude

---
_Source: https://npm.io/package/@conveyal/lonlat · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
