-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathunderscore.geospatial.coffee
More file actions
37 lines (29 loc) · 1.09 KB
/
underscore.geospatial.coffee
File metadata and controls
37 lines (29 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Decide where to export functions to.
root = exports ? @
# This module depends on Underscore.js to not become too large.
_ = root._ or require 'underscore'
# Degrees need to be converted to Radians
rad = (n) -> n * Math.PI / 180
mrad = _.memoize (n) -> rad n
# Haversine formula
# http://en.wikipedia.org/wiki/Haversine_formula
distance = (lat1, lng1, lat2, lng2) ->
dLat = rad(lat2-lat1)
dLng = rad(lng2-lng1)
lat1 = mrad(lat1)
lat2 = rad(lat2)
a = (Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.sin(dLng/2) * Math.sin(dLng/2) * Math.cos(lat1) * Math.cos(lat2))
# 12742 - shortened km transform (6371) * 2 (part of the formula)
12742 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a))
# Assumes an iterable object with elements that have `.lat` and
# `.lng` attributes. See specs for examples.
nearest = (lat, lng, iterable) ->
_.sortBy iterable, (p) -> distance lat, lng, p.lat, p.lng
# Same as `.nearest()`, but reverse order.
farthest = (lat, lng, iterable) ->
_.sortBy iterable, (p) -> - distance lat, lng, p.lat, p.lng
_.mixin
distance: distance
nearest: nearest
farthest: farthest