Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@conveyal/lonlat

Package Overview
Dependencies
Maintainers
5
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@conveyal/lonlat - npm Package Compare versions

Comparing version 1.3.0 to 1.4.0

yarn.lock

145

index.js

@@ -26,2 +26,11 @@ /**

/**
* (type)
*
* Object with x/y number values.
* @typedef {Object} lonlat.types.point
* @property {number} x
* @property {number} y
*/
/**
* (exception type)

@@ -171,4 +180,4 @@ *

*
* @param {input} lonlat1
* @param {input} lonlat2
* @param {lonlat.types.input} lonlat1
* @param {lonlat.types.input} lonlat2
* @param {number} [epsilon=0] The maximum acceptable deviation to be considered equal.

@@ -190,3 +199,3 @@ * @return {boolean}

/**
* @param {input} input
* @param {lonlat.types.input} input
* @param {number} [fixed=5] The number of decimal places to round to.

@@ -293,2 +302,132 @@ * @return {string} A string with in the format `longitude,latitude` rounded to

/**
* Pixel conversions and constants taken from
* https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Implementations
*/
/**
* Pixels per tile.
*/
var PIXELS_PER_TILE = module.exports.PIXELS_PER_TILE = 256
// 2^z represents the tile number. Scale that by the number of pixels in each tile.
function zScale (z) {
return Math.pow(2, z) * PIXELS_PER_TILE
}
// Converts from degrees to radians
function toRadians (degrees) {
return degrees * Math.PI / 180
}
// Converts from radians to degrees.
function toDegrees (radians) {
return radians * 180 / Math.PI
}
/**
* Convert a longitude to it's pixel value given a `zoom` level.
*
* @param {number} longitude
* @param {number} zoom
* @return {number} pixel
* @example
* var xPixel = lonlat.longitudeToPixel(-70, 9) //= 40049.77777777778
*/
function longitudeToPixel (longitude, zoom) {
return (longitude + 180) / 360 * zScale(zoom)
}
module.exports.longitudeToPixel = longitudeToPixel
/**
* Convert a latitude to it's pixel value given a `zoom` level.
*
* @param {number} latitude
* @param {number} zoom
* @return {number} pixel
* @example
* var yPixel = lonlat.latitudeToPixel(40, 9) //= 49621.12736343896
*/
function latitudeToPixel (latitude, zoom) {
const latRad = toRadians(latitude)
return (1 -
Math.log(Math.tan(latRad) + (1 / Math.cos(latRad))) /
Math.PI) / 2 * zScale(zoom)
}
module.exports.latitudeToPixel = latitudeToPixel
/**
* Maximum Latitude for valid Mercator projection conversion.
*/
var MAX_LAT = toDegrees(Math.atan(Math.sinh(Math.PI)))
/**
* Convert a coordinate to a pixel.
*
* @param {lonlat.types.input} input
* @param {number} zoom
* @return {Object} An object with `x` and `y` attributes representing pixel coordinates
* @throws {lonlat.types.InvalidCoordinateException}
* @throws {Error} If latitude is above or below `MAX_LAT`
* @throws {Error} If `zoom` is undefined.
* @example
* var pixel = lonlat.toPixel({lon: -70, lat: 40}, 9) //= {x: 40049.77777777778, y:49621.12736343896}
*/
module.exports.toPixel = function toPixel (input, zoom) {
var ll = normalize(input)
if (ll.lat > MAX_LAT || ll.lat < -MAX_LAT) {
throw new Error('Pixel conversion only works between ' + MAX_LAT + 'N and -' + MAX_LAT + 'S')
}
return {
x: longitudeToPixel(ll.lon, zoom),
y: latitudeToPixel(ll.lat, zoom)
}
}
/**
* Convert a pixel to it's longitude value given a zoom level.
*
* @param {number} x
* @param {number} zoom
* @return {number} longitude
* @example
* var lon = lonlat.pixelToLongitude(40000, 9) //= -70.13671875
*/
function pixelToLongitude (x, zoom) {
return x / zScale(zoom) * 360 - 180
}
module.exports.pixelToLongitude = pixelToLongitude
/**
* Convert a pixel to it's latitude value given a zoom level.
*
* @param {number} y
* @param {number} zoom
* @return {number} latitude
* @example
* var lat = lonlat.pixelToLatitude(50000, 9) //= 39.1982053488948
*/
function pixelToLatitude (y, zoom) {
var latRad = Math.atan(Math.sinh(Math.PI * (1 - 2 * y / zScale(zoom))))
return toDegrees(latRad)
}
module.exports.pixelToLatitude = pixelToLatitude
/**
* From pixel.
*
* @param {lonlat.types.point} pixel
* @param {number} zoom
* @return {lonlat.types.output}
* @example
* var ll = lonlat.fromPixel({x: 40000, y: 50000}, 9) //= {lon: -70.13671875, lat: 39.1982053488948}
*/
module.exports.fromPixel = function fromPixel (pixel, zoom) {
return {
lon: pixelToLongitude(pixel.x, zoom),
lat: pixelToLatitude(pixel.y, zoom)
}
}
function floatize (lonlat) {

@@ -295,0 +434,0 @@ var lon = parseFloatWithAlternates([lonlat.lon, lonlat.lng, lonlat.longitude])

@@ -7,2 +7,4 @@ /* globals describe, expect, jest, it */

const lon = 70.01232
const Z = 9 // Zoom level to use
const pixel = {x: 91026.70779, y: 50497.02600}
const lonlat = {lon, lat}

@@ -144,2 +146,20 @@ const point = {x: lon, y: lat}

describe('pixel', () => {
it('can convert to web mercator pixel coordinates', () => {
const p = ll.toPixel({lat, lon}, Z)
expect(Math.round(p.x)).toBe(Math.round(pixel.x))
expect(Math.round(p.y)).toBe(Math.round(pixel.y))
})
it('can convert from web mercator pixel coordinates', () => {
const l = ll.fromPixel(pixel, Z)
expect(Math.round(l.lat)).toBe(Math.round(lat))
expect(Math.round(l.lon)).toBe(Math.round(lon))
})
it('should throw an error if converting a latitude > MAX_LAT', () => {
expect(() => ll.toPixel({lat: 86, lon}, Z)).toThrow()
})
})
describe('issues', () => {

@@ -146,0 +166,0 @@ it('#3 - Does not parse coordinates with 0 for lat or lon', () => {

22

package.json

@@ -6,8 +6,11 @@ {

"scripts": {
"cover": "npm test -- --coverage --coverage-paths index.js",
"cover": "yarn test --coverage --coverage-paths index.js",
"generate-docs": "documentation readme index.js --section=API --markdown-toc=true",
"lint": "mastarm lint",
"jest": "mastarm test",
"lint": "mastarm lint index.js index.test.js",
"lint-docs": "documentation lint index.js",
"semantic-release": "semantic-release pre && npm publish && semantic-release post",
"test": "npm run lint && npm run lint-docs && mastarm test"
"pretest": "yarn",
"semantic-release": "semantic-release",
"test": "yarn run lint && yarn run lint-docs && yarn run jest",
"travis-deploy-once": "travis-deploy-once"
},

@@ -32,7 +35,8 @@ "repository": {

"devDependencies": {
"documentation": "^4.0.0-beta.18",
"mastarm": "^3.5.1",
"semantic-release": "^6.3.2"
"documentation": "^8.1.2",
"mastarm": "^4.4.0",
"semantic-release": "^15.10.3",
"travis-deploy-once": "^5.0.9"
},
"version": "1.3.0"
}
"version": "1.4.0"
}

@@ -25,17 +25,67 @@ # lonlat

- [lonlat.types.input](#lonlattypesinput)
- [lonlat.types.output](#lonlattypesoutput)
- [Properties](#properties)
- [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)
- [lonlat.types.output](#lonlattypesoutput)
- [Parameters](#parameters-12)
- [Examples](#examples-12)
- [toPixel](#topixel)
- [Parameters](#parameters-13)
- [Examples](#examples-13)
- [fromPixel](#frompixel)
- [Parameters](#parameters-14)
- [Examples](#examples-14)
- [lonlat.types.point](#lonlattypespoint)
- [Properties](#properties-1)
- [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)

@@ -57,8 +107,17 @@ ### lonlat.types.input

Type: ([Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) \| [string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object))
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))
**Parameters**
### lonlat.types.output
- `unknown`
(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.InvalidCoordinateException

@@ -70,8 +129,4 @@

Type: [Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)
Type: [Error](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error)
**Parameters**
- `unknown`
### conveyal/lonlat

@@ -81,7 +136,7 @@

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

@@ -123,7 +178,7 @@ ```javascript

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

@@ -147,7 +202,7 @@ ```javascript

**Parameters**
##### Parameters
- `lonlat` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** An object with a `lon`, `lng` or `longitude` attribute and a `lat` or `latitude` attribute
- `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**
##### Examples

@@ -169,8 +224,8 @@ ```javascript

**Parameters**
##### Parameters
- `point` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** An object with a `x` attribute representing `longitude`
- `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**
##### Examples

@@ -193,7 +248,7 @@ ```javascript

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

@@ -215,7 +270,7 @@ ```javascript

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

@@ -236,9 +291,9 @@ ```javascript

**Parameters**
##### Parameters
- `lonlat1` **input**
- `lonlat2` **input**
- `epsilon` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?** The maximum acceptable deviation to be considered equal. (optional, default `0`)
- `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**
##### Examples

@@ -253,12 +308,12 @@ ```javascript

Returns **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)**
Returns **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)**
#### print
**Parameters**
##### Parameters
- `input` **input**
- `fixed` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?** The number of decimal places to round to. (optional, default `5`)
- `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**
##### Examples

@@ -273,3 +328,3 @@ ```javascript

Returns **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** A string with in the format `longitude,latitude` rounded to
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`

@@ -283,7 +338,7 @@

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

@@ -298,3 +353,3 @@ ```javascript

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

@@ -306,7 +361,7 @@ #### toLeaflet

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

@@ -321,3 +376,3 @@ ```javascript

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

@@ -328,7 +383,7 @@ #### toPoint

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

@@ -343,3 +398,3 @@ ```javascript

Returns **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** An object with `x` and `y` attributes representing latitude and longitude respectively
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

@@ -352,7 +407,7 @@ #### toString

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

@@ -368,3 +423,3 @@ ```javascript

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

@@ -375,7 +430,7 @@ #### toLatFirstString

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

@@ -390,19 +445,134 @@ ```javascript

Returns **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** A string in the format 'longitude,latitude'
Returns **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** A string in the format 'longitude,latitude'
### lonlat.types.output
#### 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)**
### lonlat.types.point
(type)
Standardized lon/lat object.
Object with x/y number values.
Type: [Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)
Type: [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)
**Parameters**
#### Properties
- `unknown`
- `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)**
**Properties**
### PIXELS_PER_TILE
- `lat` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)**
- `lon` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)**
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

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc