Skip to content

Commit

Permalink
feat(naming): Switching from lng to lon cause...
Browse files Browse the repository at this point in the history
In line with the name of this library we are changing the standard short representation of longitude

to be lon instead of lng. The author (Hi!) mistakenly thought through his experience that lng was

the more conventional representation and it has given himself and others headaches.
  • Loading branch information
trevorgerhardt committed Oct 5, 2016
1 parent b248fbd commit 78254ed
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 55 deletions.
39 changes: 19 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# lonlng
Lat/lon normalization cause...**sigh**.

No one has agreed on a standard way of representing lat/lng. 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.
No one has agreed on a standard way of representing lat/lon. 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.

## Just use the `{lng: ${longitude}, lat: ${latitude}}` representation
## Just use the `{lon: ${longitude}, lat: ${latitude}}` representation

Utilizing this won't always be possible/easiest, so please at least adopt the following conventions. Any variables or functions that contain the following names should be represented by the accompanying structure:

* `latlng`: `{lng: ${longitude}, lat: ${latitude}}`
* `latlon`: `{lon: ${longitude}, lat: ${latitude}}`
* `coordinates`: `[${longitude}, ${latitude}]`
* `point`: `{x: ${longitude}, y: ${latitude}}`

Expand All @@ -22,30 +22,29 @@ import assert from 'assert'
import ll from 'lonlng'

const lat = 38.13234
const lng = 70.01232
const latlng = {lat, lng}
const point = {x: lng, y: lat}
const coordinates = [lng, lat]
const str = `${lng},${lat}`
const lon = 70.01232
const latlon = {lat, lon}
const point = {x: lon, y: lat}
const coordinates = [lon, lat]
const str = `${lon},${lat}`

const pairs = [
// normalization
[latlng, ll(latlng)],
[latlng, ll(point)],
[latlng, ll(coordinates)],
[latlng, ll(str)],
[latlon, ll(latlon)],
[latlon, ll(point)],
[latlon, ll(coordinates)],
[latlon, ll(str)],

// convert to type, normalizes to `latlng` first in each function
[ll.toCoordinates(latlng), coordinates],
[ll.toPoint(latlng), point],
[ll.toString(latlng), str],

[ll.toCoordinates(latlon), coordinates],
[ll.toPoint(latlon), point],
[ll.toString(latlon), str],

// if the type is known, use the specific convert function directly
[latlng, ll.fromLatlng(latlng)],
[latlng, ll.fromCoordinates(coordinates)],
[latlng, ll.fromPoint(point)],
[latlng, ll.fromString(str)]
[latlon, ll.fromLatlng(latlon)],
[latlon, ll.fromCoordinates(coordinates)],
[latlon, ll.fromPoint(point)],
[latlon, ll.fromString(str)]
]

pairs.forEach(pair => assert.deepEqual(pair[0], pair[1]))
Expand Down
38 changes: 19 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,67 @@

module.exports = normalize
module.exports.fromCoordinates = module.exports.fromGeoJSON = fromCoordinates
module.exports.fromLatlng = module.exports.fromLeaflet = fromLatlng
module.exports.fromLatlng = module.exports.fromLatlon = module.exports.fromLeaflet = fromLatlon
module.exports.fromPoint = fromPoint
module.exports.fromString = fromString

module.exports.print = function print (input, fixed) {
var ll = normalize(input)
return ll.lng.toFixed(fixed || 5) + ', ' + ll.lat.toFixed(fixed || 5)
return ll.lon.toFixed(fixed || 5) + ', ' + ll.lat.toFixed(fixed || 5)
}

module.exports.isEqual = function (latlng1, latlng2, epsilon) {
latlng1 = normalize(latlng1)
latlng2 = normalize(latlng2)
module.exports.isEqual = function (latlon1, latlon2, epsilon) {
latlon1 = normalize(latlon1)
latlon2 = normalize(latlon2)
epsilon = epsilon || 0
return (Math.abs(latlng1.lat - latlng2.lat) <= epsilon) && (Math.abs(latlng1.lng - latlng2.lng) <= epsilon)
return (Math.abs(latlon1.lat - latlon2.lat) <= epsilon) && (Math.abs(latlon1.lon - latlon2.lon) <= epsilon)
}

module.exports.toCoordinates = module.exports.toGeoJSON = function toCoordinates (input) {
var ll = normalize(input)
return [ll.lng, ll.lat]
return [ll.lon, ll.lat]
}

module.exports.toLatlng = function toLatlng (input) {
module.exports.tolatlon = function tolatlon (input) {
return normalize(input)
}

module.exports.toPoint = function toPoint (input) {
var ll = normalize(input)
return {x: ll.lng, y: ll.lat}
return {x: ll.lon, y: ll.lat}
}

module.exports.toString = function toString (input) {
var ll = normalize(input)
return ll.lng + ',' + ll.lat
return ll.lon + ',' + ll.lat
}

module.exports.toLeaflet = function toLeaflet (input) {
if (!window.L) throw new Error('Leaflet not found.')
var ll = normalize(input)
return new window.L.Latlng(ll.lat, ll.lng)
return window.L.latLng(ll.lat, ll.lon)
}

function fromCoordinates (coordinates) {
return floatize({lng: coordinates[0], lat: coordinates[1]})
return floatize({lon: coordinates[0], lat: coordinates[1]})
}

function fromLatlng (latlng) {
return floatize(latlng)
function fromLatlon (latlon) {
return floatize(latlon)
}

function fromPoint (point) {
return floatize({lng: point.x, lat: point.y})
return floatize({lon: point.x, lat: point.y})
}

function fromString (str) {
const arr = str.split(',')
return floatize({lng: arr[0], lat: arr[1]})
return floatize({lon: arr[0], lat: arr[1]})
}

function floatize (latlng) {
return {lng: parseFloat(latlng.lng || latlng.lon || latlng.longitude), lat: parseFloat(latlng.lat || latlng.latitude)}
function floatize (latlon) {
const lon = parseFloat(latlon.lon || latlon.lng || latlon.longitude)
return {lng: lon, lon: lon, lat: parseFloat(latlon.lat || latlon.latitude)}
}

function normalize (unknown) {
Expand All @@ -70,4 +71,3 @@ function normalize (unknown) {
else if (unknown.x && unknown.y) return fromPoint(unknown)
return floatize(unknown)
}

32 changes: 16 additions & 16 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@ const assert = require('assert')
const ll = require('./')

const lat = 38.13234
const lng = 70.01232
const latlng = {lat, lng}
const point = {x: lng, y: lat}
const coordinates = [lng, lat]
const str = `${lng},${lat}`
const lon = 70.01232
const latlon = {lat, lon, lng: lon}
const point = {x: lon, y: lat}
const coordinates = [lon, lat]
const str = `${lon},${lat}`

const pairs = [
// normalization
[latlng, ll(latlng)],
[latlng, ll(point)],
[latlng, ll(coordinates)],
[latlng, ll(str)],
[latlon, ll(latlon)],
[latlon, ll(point)],
[latlon, ll(coordinates)],
[latlon, ll(str)],

// convert to type, normalizes to `latlng` first in each function
[ll.toCoordinates(latlng), coordinates],
[ll.toPoint(latlng), point],
[ll.toString(latlng), str],
[ll.toCoordinates(latlon), coordinates],
[ll.toPoint(latlon), point],
[ll.toString(latlon), str],

// if the type is known, use the specific convert function directly
[latlng, ll.fromLatlng(latlng)],
[latlng, ll.fromCoordinates(coordinates)],
[latlng, ll.fromPoint(point)],
[latlng, ll.fromString(str)]
[latlon, ll.fromLatlng(latlon)],
[latlon, ll.fromCoordinates(coordinates)],
[latlon, ll.fromPoint(point)],
[latlon, ll.fromString(str)]
]

pairs.forEach((pair) => assert.deepEqual(pair[0], pair[1]))

0 comments on commit 78254ed

Please sign in to comment.