-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e21b49b
commit bb13d10
Showing
19 changed files
with
8,691 additions
and
1,827 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
|
||
/** | ||
geodesic functions, javascript versions of aviation formulary: | ||
http://williams.best.vwh.net/avform.htm | ||
note: all point values are in radians, not degrees | ||
**/ | ||
|
||
/** | ||
distance between point A and point B (in radians) | ||
**/ | ||
function distance( a, b ){ | ||
return Math.acos( Math.sin( a.lat ) * Math.sin( b.lat ) + | ||
Math.cos( a.lat ) * Math.cos( b.lat ) * Math.cos( a.lon - b.lon )); | ||
} | ||
|
||
/** | ||
distance between point A and point B (in radians) | ||
note: for very short distances this version is less susceptible to rounding error | ||
**/ | ||
function distance2( a, b ){ | ||
return 2 * Math.asin( Math.sqrt( Math.pow( Math.sin(( a.lat - b.lat ) / 2 ), 2 ) + | ||
Math.cos( a.lat ) * Math.cos( b.lat ) * Math.pow( Math.sin(( a.lon - b.lon ) / 2), 2))); | ||
} | ||
|
||
/** | ||
course from point A and point B (in radians) | ||
**/ | ||
function course( a, b, d ){ | ||
return Math.acos( | ||
( Math.sin( b.lat ) - Math.sin( a.lat ) * Math.cos( d )) / | ||
( Math.sin( d ) * Math.cos( a.lat ) ) | ||
); | ||
} | ||
|
||
/** | ||
cross track error (distance off course) | ||
(positive XTD means right of course, negative means left) | ||
**/ | ||
function crossTrack( d, crs1, crs2, A, B, D ){ | ||
|
||
var calc = ( crs1 - crs2 ); | ||
|
||
// north pole / south pole | ||
if( A.lat === +90 ){ calc = ( D.lon - B.lon ); } | ||
if( A.lat === -90 ){ calc = ( B.lon - D.lon ); } | ||
|
||
return Math.asin( Math.sin( d ) * Math.sin( calc ) ); | ||
} | ||
|
||
/** | ||
along track distance (the distance from A along the course towards B to the point abeam D) | ||
**/ | ||
function alongTrack( d, xtd ){ | ||
return Math.acos( Math.cos( d ) / Math.cos( xtd ) ); | ||
} | ||
|
||
/** | ||
along track distance (the distance from A along the course towards B to the point abeam D) | ||
note: for very short distances this version is less susceptible to rounding error | ||
**/ | ||
function alongTrack2( d, xtd ){ | ||
return Math.asin( Math.sqrt( Math.pow( Math.sin( d ), 2 ) - Math.pow( Math.sin( xtd ), 2 ) ) / Math.cos( xtd ) ); | ||
} | ||
|
||
/** | ||
interpolate f (percent) of the distance d along path A-B | ||
**/ | ||
function interpolate( d, f, a, b ){ | ||
var A = Math.sin( (1-f) * d ) / Math.sin( d ); | ||
var B = Math.sin( f * d ) / Math.sin( d ); | ||
var X = A * Math.cos( a.lat ) * Math.cos( a.lon ) + B * Math.cos( b.lat ) * Math.cos( b.lon ); | ||
var Y = A * Math.cos( a.lat ) * Math.sin( a.lon ) + B * Math.cos( b.lat ) * Math.sin( b.lon ); | ||
var Z = A * Math.sin( a.lat ) + B * Math.sin( b.lat ); | ||
return { | ||
lat: Math.atan2( Z, Math.sqrt( Math.pow( X, 2 ) + Math.pow( Y, 2 ) ) ), | ||
lon: Math.atan2( Y, X ) | ||
} | ||
} | ||
|
||
/** | ||
calculate the distance a using b and C | ||
c | ||
A -------B | ||
\ | | ||
\ | | ||
\b |a | ||
\ | | ||
\ | | ||
\ | | ||
\C| | ||
\| | ||
Napier's rules: tan(a) = tan(b)*cos(C) | ||
**/ | ||
function project( b, C ){ | ||
return Math.atan( Math.tan( b ) * Math.cos( C ) ); | ||
} | ||
|
||
module.exports.distance = distance; | ||
module.exports.distance2 = distance2; | ||
module.exports.course = course; | ||
module.exports.crossTrack = crossTrack; | ||
module.exports.alongTrack = alongTrack; | ||
module.exports.alongTrack2 = alongTrack2; | ||
module.exports.interpolate = interpolate; | ||
module.exports.project = project; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
function interpolate( addrPoints, vertexDistance ){ | ||
|
||
// cycle through calculated addrPoints and interpolate a fractional housenumber | ||
// value which would sit at this vertexDistance. | ||
for( var x=0; x<addrPoints.length-1; x++ ){ | ||
|
||
var thisAddr = addrPoints[x], | ||
nextAddr = addrPoints[x+1]; | ||
|
||
// the vertex vertexDistance is less that the lowest housenumber | ||
// @extrapolation | ||
if( vertexDistance < thisAddr.dist ){ return null; } | ||
|
||
// vertex vertexDistance is between two house number vertexDistance | ||
if( nextAddr.dist > vertexDistance ){ | ||
var ratio = ((vertexDistance - thisAddr.dist) / (nextAddr.dist - thisAddr.dist)); | ||
|
||
// invert ratio if the street was drawn from high house number to low | ||
if( thisAddr.housenumber > nextAddr.housenumber ){ ratio = 1 - ratio; } | ||
|
||
if( ratio >= 1 || ratio <= 0 ){ break; } // will result in a duplicate value | ||
var minHouseNumber = Math.min( thisAddr.housenumber, nextAddr.housenumber ); | ||
var maxHouseNumber = Math.max( thisAddr.housenumber, nextAddr.housenumber ); | ||
return minHouseNumber + (( maxHouseNumber - minHouseNumber ) * ratio); | ||
} | ||
|
||
// else the vertex is greater than the highest housenumber | ||
// @extrapolation | ||
} | ||
return null; | ||
} | ||
|
||
module.exports = interpolate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.