Skip to content

Instantly share code, notes, and snippets.

@stephanbogner
Created July 6, 2017 10:12
Show Gist options
  • Save stephanbogner/8777bb7fb4377dc39deda6d41be53e85 to your computer and use it in GitHub Desktop.
Save stephanbogner/8777bb7fb4377dc39deda6d41be53e85 to your computer and use it in GitHub Desktop.
turf-subdivide-proposal
<script src='https://npmcdn.com/@turf/turf/turf.min.js'></script>
<script>
var lineString = {
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[
13.0078125,
19.31114335506464
],
[-73.47656249999999,
5.266007882805498
]
]
}
}
var polygon = {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[-90.3515625,
45.583289756006316
],
[-49.21875,
45.583289756006316
],
[-49.21875,
57.136239319177434
],
[-90.3515625,
57.136239319177434
],
[-90.3515625,
45.583289756006316
]
],
[
[-61.17187499999999,
49.61070993807422
],
[-72.0703125,
39.639537564366684
],
[-62.57812500000001,
34.30714385628804
],
[-48.8671875,
34.88593094075317
],
[-41.484375,
45.82879925192134
],
[-38.3203125,
48.922499263758255
],
[-61.17187499999999,
49.61070993807422
]
]
]
}
}
var polygonAfter = subdivide(polygon, 100);
var lineStringAfter = subdivide(lineString, 100);
var fc = turf.featureCollection([polygon, polygonAfter, lineString, lineStringAfter]);
// console.log( 'Polgon - Before', JSON.stringify( polygon ) );
// console.log( 'Polgon - After', JSON.stringify( polygonAfter ) );
// console.log();
// console.log( 'LineString - Before', JSON.stringify( lineString ) );
// console.log( 'LineString - After', JSON.stringify( subdivide(lineStringAfter, 100) ) );
console.log( JSON.stringify( fc ) );
function subdivide(feature, maximumLength, units) {
feature = JSON.parse(JSON.stringify(feature));
var arrayOfCoordinates = [];
if (feature.geometry.type === 'Polygon') {
arrayOfCoordinates = feature.geometry.coordinates;
} else if (feature.geometry.type === 'LineString') {
arrayOfCoordinates = [feature.geometry.coordinates];
}
var newCoordinates = [];
for (var partIndex = 0; partIndex < arrayOfCoordinates.length; partIndex++) {
var part = arrayOfCoordinates[partIndex];
var origin = turf.point(part[0]);
var destination = turf.point(part[part.length - 1]);
var subCoordinates = [];
for (var coordinatesIndex = 0; coordinatesIndex < part.length - 1; coordinatesIndex++) {
var fromCoordinates = part[coordinatesIndex];
var toCoordinates = part[coordinatesIndex + 1];
var from = turf.point(fromCoordinates);
var to = turf.point(toCoordinates);
var bearing = turf.bearing(from, to);
var distance = turf.distance(from, to, units);
var numberOfSubdivision = Math.floor(distance / maximumLength);
var segmentLength = distance / numberOfSubdivision;
subCoordinates.push(fromCoordinates);
for (var s = 1; s < numberOfSubdivision; s++) {
var currentDistance = s * segmentLength;
var point = turf.destination(from, currentDistance, bearing, units);
var newStopCoordinates = point.geometry.coordinates;
subCoordinates.push(newStopCoordinates);
}
}
subCoordinates.push(part[part.length - 1]);
newCoordinates[partIndex] = subCoordinates;
}
if (feature.geometry.type === 'Polygon') {
feature.geometry.coordinates = newCoordinates;
} else if (feature.geometry.type === 'LineString') {
feature.geometry.coordinates = newCoordinates[0];
}
return feature;
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment