Last active
June 22, 2017 15:55
-
-
Save stephanbogner/60e4652c22dc9366a733a6495c071181 to your computer and use it in GitHub Desktop.
Offsets creates a lineString offset from the original one
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
function getOffsetOutline(lineString, thickness, side) { | |
// thickness in km | |
var direction = 1; | |
if (side === 'left') { | |
direction = -1; | |
} else if (side === 'right') { | |
direction = 1; | |
} else { | |
console.log('Please specify direction as \'left\' or \'right\''); | |
} | |
// Start | |
var uppers = []; | |
var newCoordinates = []; | |
var r = thickness / 2; | |
var coordinates = lineString.geometry.coordinates; | |
var initialBearing = turf.bearing(turf.point(coordinates[1]), turf.point(coordinates[0])); | |
var firstUpper = turf.destination(turf.point(coordinates[0]), r, initialBearing - 90 * direction); | |
newCoordinates.push(firstUpper.geometry.coordinates); | |
// Mid | |
for (var i = 1; i < coordinates.length - 1; i++) { | |
var prev = coordinates[i - 1]; | |
var curr = coordinates[i]; | |
var next = coordinates[i + 1]; | |
var bearingFromPrev = turf.bearing(turf.point(curr), turf.point(prev)); | |
var bearingToNext = turf.bearing(turf.point(next), turf.point(curr)); | |
var bearingMid = getAverageBearing(bearingFromPrev, bearingToNext); | |
console.log(bearingFromPrev, bearingToNext, bearingMid); | |
var upper = turf.destination(turf.point(curr), r, bearingMid - 90 * direction); | |
newCoordinates.push(upper.geometry.coordinates); | |
} | |
// End | |
var lastBearing = turf.bearing(turf.point(coordinates[coordinates.length - 2]), turf.point(coordinates[coordinates.length - 1])); | |
var lastUpper = turf.destination(turf.point(coordinates[coordinates.length - 1]), r, lastBearing + 90 * direction); | |
newCoordinates.push(lastUpper.geometry.coordinates); | |
return turf.lineString(newCoordinates); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment