Skip to content

Instantly share code, notes, and snippets.

@dzfranklin
Created June 15, 2024 23:28
Show Gist options
  • Save dzfranklin/aa964e173bb219037d8367ef7744af97 to your computer and use it in GitHub Desktop.
Save dzfranklin/aa964e173bb219037d8367ef7744af97 to your computer and use it in GitHub Desktop.
turf-sandbox snippet
// Paste into <https://turf-sandbox.netlify.app/>
function sampleLine(inputLine, distanceMeters) {
let inputLength = turf.length(inputLine, "m")
let samples = []
for (let i = 0; i < Math.floor(inputLength); i+=distanceMeters) {
let point = turf.along(inputLine, i, 'm')
samples.push(point["geometry"]["coordinates"])
}
samples.push(inputLine["geometry"]["coordinates"].at(-1))
return turf.lineString(samples)
}
const l = turf.lineString(
[[-83, 30], [-84, 36]],
{'stroke-width': 3, stroke: '#00F'}
);
const samples = sampleLine(l, 10);
for (const sample of samples["geometry"]["coordinates"]) {
console.log(turf.pointToLineDistance(sample, l, {units: "meters"}))
}
const samplePoints = samples["geometry"]["coordinates"]
.map(c => turf.point(c, ))
return turf.featureCollection([l, turf.featureCollection(samplePoints)]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment