Last active
June 22, 2017 15:01
-
-
Save stephanbogner/8a2216817ad143b6c455311355733041 to your computer and use it in GitHub Desktop.
Creates an outline to a lineString similar to the outline stroke feature in illustrator
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
// Example Result (e.g. paste into http://geojson.io) | |
// {"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[7.162147402259639,51.95323664248935],[9.397525357884254,52.01990267103883],[10.276573430855475,52.73102957225263],[11.384078028489244,52.73187278993396],[12.440828207929341,52.53150641982873],[12.432220352373731,52.51430530461843],[11.379594760127718,52.71409821694048],[10.28983037386654,52.714941107317614],[9.41106598137281,52.0039700143863],[7.16402409736249,51.93529310810906],[7.162147402259639,51.95323664248935]]]}} | |
var turf = require('turf'); | |
var ls = { | |
"type": "Feature", | |
"properties": {}, | |
"geometry": { | |
"type": "LineString", | |
"coordinates": [ | |
[ | |
7.163085937499999, | |
51.944264879028765 | |
], | |
[ | |
9.404296875, | |
52.01193653675363 | |
], | |
[ | |
10.283203125, | |
52.72298552457069 | |
], | |
[ | |
11.3818359375, | |
52.72298552457069 | |
], | |
[ | |
12.436523437499998, | |
52.522905940278065 | |
] | |
] | |
} | |
} | |
console.log(); | |
console.log('Input'); | |
console.log(JSON.stringify(ls)); | |
console.log(); | |
console.log('Output'); | |
console.log(JSON.stringify(computeOutline(ls, 2))); | |
function computeOutline(lineString, thickness) { | |
// thickness in km | |
// Start | |
var uppers = []; | |
var lowers = []; | |
var newCoordinates = []; | |
var r = thickness / 2; | |
var coordinates = lineString.geometry.coordinates; | |
var initialBearing = turf.bearing(turf.point(coordinates[0]), turf.point(coordinates[1])); | |
var firstUpper = turf.destination(turf.point(coordinates[0]), r, initialBearing - 90); | |
var firstLower = turf.destination(turf.point(coordinates[0]), r, initialBearing + 90); | |
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(curr), turf.point(next)); | |
var bearingMid = (bearingFromPrev + bearingToNext) / 2; | |
var upper = turf.destination(turf.point(curr), r, bearingMid); | |
var lower = turf.destination(turf.point(curr), r, bearingMid + 180); | |
newCoordinates.push(upper.geometry.coordinates); | |
lowers.push(lower.geometry.coordinates); | |
} | |
lowers.reverse() | |
// 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); | |
var midLower = turf.destination(turf.point(coordinates[coordinates.length - 1]), r, lastBearing + 90); | |
newCoordinates.push(lastUpper.geometry.coordinates); | |
newCoordinates.push(midLower.geometry.coordinates); | |
newCoordinates = newCoordinates.concat( lowers ); | |
newCoordinates.push(firstLower.geometry.coordinates); | |
newCoordinates.push(newCoordinates[0]); | |
return turf.polygon([newCoordinates]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment