This script caculates and creates a mile-marker for each mile within a race, in this case a 26 mile marathon.
Output example: https://gist.github.com/rgdonohue/dd78bfdb5f5da76596b2
This script caculates and creates a mile-marker for each mile within a race, in this case a 26 mile marathon.
Output example: https://gist.github.com/rgdonohue/dd78bfdb5f5da76596b2
var turf = require('turf'); | |
var fs = require('fs'); | |
var gp = require("geojson-precision"); | |
fs.readFile('race_route.geojson', 'utf8', function(err,data){ | |
if(err) throw err; | |
processData(JSON.parse(data)) | |
}); | |
function processData(data){ | |
var line = data.features[0]; | |
var result = { | |
"type": "FeatureCollection", | |
"features": [line] | |
}; | |
var i = 0; | |
while(i <= 26){ | |
var along = turf.along(line,i,'miles'); | |
if(i == 0) { | |
along.properties = { 'distance': 'start' }; | |
} else if(i == 26) { | |
along.properties = { 'distance': 'finish' }; | |
} else { | |
along.properties = { 'distance':'mile ' + i }; | |
} | |
result.features.push(along); | |
i++; | |
} | |
// trim ridiculous precison thnx to https://github.com/jczaplew/geojson-precision | |
gp.parse(result,4); | |
writeToFile(result); | |
} | |
function writeToFile(output){ | |
fs.writeFile('route_with_markers.json', JSON.stringify(output), function(err){ | |
if(err){ | |
return console.log(err); | |
} | |
console.log('success!') | |
}); | |
} |