Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
@conveyal/lonlat
Advanced tools
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.
(type)
An unknown type of input. Must be one of the following:
longitude,latitude
x
and y
properties.
x
represents longitude and y
represents latitude.lon
, lng
or longitude
For latitude any of the following are valid: lat
or latitude
Type: (Array | string | Object)
(type)
Standardized lon/lat object.
Type: Object
(type)
Object with x/y number values.
Type: Object
(exception type)
An error that is thrown upon providing invalid coordinates.
Type: Error
Parse an unknown type of input.
unknown
lonlat.types.inputvar 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
Returns lonlat.types.output
aliases: fromGeoJSON
Tries to parse from an array of coordinates.
coordinates
Array An array in the format: [longitude, latitude]var lonlat = require('@conveyal/lonlat')
var position = lonlat.fromCoordinates([12, 34]) // { lon: 12, lat: 34 }
position = lonlat.fromGeoJSON([12, 34]) // { lon: 12, lat: 34 }
Returns lonlat.types.output
aliases: fromLeaflet
Tries to parse from an object.
lonlat
Object An object with a lon
, lng
or longitude
attribute and a lat
or latitude
attributevar 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 }
Returns lonlat.types.output
Tries to parse from an object.
point
Object An object with a x
attribute representing longitude
and a y
attribute representing latitude
var lonlat = require('@conveyal/lonlat')
var position = lonlat.fromPoint({ x: 12, y: 34 }) // { lon: 12, lat: 34 }
Returns lonlat.types.output
aliases: fromLonFirstString
Tries to parse from a string where the longitude appears before the latitude.
str
string A string in the format: longitude,latitude
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 }
Returns lonlat.types.output
Tries to parse from a string where the latitude appears before the longitude.
str
string A string in the format: latitude,longitude
var lonlat = require('@conveyal/lonlat')
var position = lonlat.fromLatFirstString('12,34') // { lon: 34, lat: 12 }
Returns lonlat.types.output
Determine if two inputs are equal to each other
lonlat1
lonlat.types.inputlonlat2
lonlat.types.inputepsilon
number The maximum acceptable deviation to be considered equal. (optional, default 0
)var lonlat = require('@conveyal/lonlat')
var isEqual = lonlat.isEqual('12,34', [12, 34]) // true
Returns boolean
input
lonlat.types.inputfixed
number The number of decimal places to round to. (optional, default 5
)var lonlat = require('@conveyal/lonlat')
var pretty = lonlat.print('12.345678,34') // '12.34568, 34.00000'
Returns string A string with in the format longitude,latitude
rounded to
the number of decimal places as specified by fixed
aliases: toGeoJSON
Translates to a coordinate array.
input
lonlat.types.inputvar lonlat = require('@conveyal/lonlat')
var coords = lonlat.toCoordinates('12,34') // [12, 34]
Returns Array An array in the format [longitude, latitude]
Translates to Leaflet LatLng object.
This function requires Leaflet to be installed as a global variable L
in the window environment.
input
lonlat.types.inputvar lonlat = require('@conveyal/lonlat')
var position = lonlat.toLeaflet({ lat: 12, long: 34 }) // Leaflet LatLng object
Returns Object A Leaflet LatLng object
Translates to point Object.
input
lonlat.types.inputvar lonlat = require('@conveyal/lonlat')
var point = lonlat.toPoint('12,34') // { x: 12, y: 34 }
Returns Object An object with x
and y
attributes representing latitude and longitude respectively
aliases: toLonFirstString
Translates to coordinate string where the longitude appears before latitude.
input
lonlat.types.inputvar lonlat = require('@conveyal/lonlat')
var str = lonlat.toString({ lat: 12, longitude: 34 }) // '34,12'
var str = lonlat.toLonFirstString({ lat: 12, longitude: 34 }) // '34,12'
Returns string A string in the format 'longitude,latitude'
Translates to coordinate string where the latitude appears before longitude.
input
lonlat.types.inputvar lonlat = require('@conveyal/lonlat')
var str = lonlat.toLatFirstString({ lat: 12, longitude: 34 }) // '12,34'
Returns string A string in the format 'longitude,latitude'
Convert a coordinate to a pixel.
input
lonlat.types.inputzoom
numbervar pixel = lonlat.toPixel({lon: -70, lat: 40}, 9) //= {x: 40049.77777777778, y:49621.12736343896}
MAX_LAT
zoom
is undefined.Returns Object An object with x
and y
attributes representing pixel coordinates
From pixel.
pixel
lonlat.types.pointzoom
Numbervar ll = lonlat.fromPixel({x: 40000, y: 50000}, 9) //= {lon: -70.13671875, lat: 39.1982053488948}
Returns lonlat.types.output
Pixel conversions and constants taken from https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Implementations
Pixels per tile.
Convert a longitude to it's pixel value given a zoom
level.
var xPixel = lonlat.longitudeToPixel(-70, 9) //= 40049.77777777778
Returns number pixel
Convert a latitude to it's pixel value given a zoom
level.
var yPixel = lonlat.latitudeToPixel(40, 9) //= 49621.12736343896
Returns number pixel
Maximum Latitude for valid Mercator projection conversion.
Convert a pixel to it's longitude value given a zoom level.
var lon = lonlat.pixelToLongitude(40000, 9) //= -70.13671875
Returns number longitude
Convert a pixel to it's latitude value given a zoom level.
var lat = lonlat.pixelToLatitude(50000, 9) //= 39.1982053488948
Returns number latitude
FAQs
Lon/lat normalization
We found that @conveyal/lonlat demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.