-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathinterpolate.js
40 lines (29 loc) · 1.47 KB
/
interpolate.js
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
38
39
40
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 );
// house numbers are only a single number apart
// see: https://github.com/pelias/interpolation/issues/6
if( maxHouseNumber <= ( minHouseNumber + 1 )){ return null; }
// return fractional housenumber
return minHouseNumber + (( maxHouseNumber - minHouseNumber ) * ratio);
}
// else the vertex is greater than the highest housenumber
// @extrapolation
}
return null;
}
module.exports = interpolate;