Skip to content

Commit

Permalink
Support GeometryCollection in @turf/line-chunk. (#726)
Browse files Browse the repository at this point in the history
  • Loading branch information
dpmcmlxxvi authored and DenisCarriere committed May 10, 2017
1 parent 910d5f5 commit 46b8502
Show file tree
Hide file tree
Showing 7 changed files with 750 additions and 27 deletions.
2 changes: 1 addition & 1 deletion packages/turf-line-chunk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ If the line is shorter than the segment length then the original line is returne

**Parameters**

- `featureIn` **([FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)<([LineString](http://geojson.org/geojson-spec.html#linestring) \| [MultiLineString](http://geojson.org/geojson-spec.html#multilinestring))>)** the lines to split
- `featureIn` **([FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects) \| [GeometryCollection](http://geojson.org/geojson-spec.html#geometrycollection)<([LineString](http://geojson.org/geojson-spec.html#linestring) \| [MultiLineString](http://geojson.org/geojson-spec.html#multilinestring))>)** the lines to split
- `segmentLength` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** how long to make each segment
- `units` **\[[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)]** units can be degrees, radians, miles, or kilometers (optional, default `'kilometers'`)
- `reverse` **\[[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)]** reverses coordinates to start the first chunked segment at the end (optional, default `false`)
Expand Down
40 changes: 15 additions & 25 deletions packages/turf-line-chunk/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
var lineSliceAlong = require('@turf/line-slice-along');
var lineDistance = require('@turf/line-distance');
var featureCollection = require('@turf/helpers').featureCollection;
var featureEach = require('@turf/meta').featureEach;
var flatten = require('@turf/flatten');
var flattenEach = require('@turf/meta').flattenEach;

/**
* Divides a {@link LineString} into chunks of a specified length.
* If the line is shorter than the segment length then the original line is returned.
*
* @name lineChunk
* @param {FeatureCollection|Feature<LineString|MultiLineString>} featureIn the lines to split
* @param {FeatureCollection|Feature|GeometryCollection<LineString|MultiLineString>} featureIn the lines to split
* @param {number} segmentLength how long to make each segment
* @param {string}[units='kilometers'] units can be degrees, radians, miles, or kilometers
* @param {boolean}[reverse=false] reverses coordinates to start the first chunked segment at the end
Expand All @@ -32,30 +31,21 @@ module.exports = function (featureIn, segmentLength, units, reverse) {
var outFeatures = [];
var debug = arguments['4']; // Hidden @param {boolean} Enable debug mode

// Handles FeatureCollection
featureEach(featureIn, function (multiFeature) {

// Handles MultiLineString
if (multiFeature.geometry.type === 'MultiLineString') {
multiFeature = flatten(multiFeature);
// Flatten each feature to simple LineString
flattenEach(featureIn, function (feature) {
if (reverse) {
feature.geometry.coordinates = feature.geometry.coordinates.reverse();
}

// All features are simple LineString
featureEach(multiFeature, function (feature) {
if (reverse) {
feature.geometry.coordinates = feature.geometry.coordinates.reverse();
var lineSegments = sliceLineSegments(feature, segmentLength, units);
lineSegments.forEach(function (segment, index) {
if (debug === true) {
var r = (index % 2 === 0) ? 'F' : '0';
var g = (index % 2 === 0) ? '0' : '0';
var b = (index % 2 === 0) ? '0' : 'F';
segment.properties['stroke'] = '#' + r + g + b;
segment.properties['stroke-width'] = 6;
}
var lineSegments = sliceLineSegments(feature, segmentLength, units);
lineSegments.forEach(function (segment, index) {
if (debug === true) {
var r = (index % 2 === 0) ? 'F' : '0';
var g = (index % 2 === 0) ? '0' : '0';
var b = (index % 2 === 0) ? '0' : 'F';
segment.properties['stroke'] = '#' + r + g + b;
segment.properties['stroke-width'] = 6;
}
outFeatures.push(segment);
});
outFeatures.push(segment);
});
});
return featureCollection(outFeatures);
Expand Down
1 change: 0 additions & 1 deletion packages/turf-line-chunk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"write-json-file": "^2.0.0"
},
"dependencies": {
"@turf/flatten": "^4.2.0",
"@turf/helpers": "^4.2.0",
"@turf/line-distance": "^4.2.0",
"@turf/line-slice-along": "^4.2.0",
Expand Down
43 changes: 43 additions & 0 deletions packages/turf-line-chunk/test/in/GeometryCollection.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"type": "GeometryCollection",
"geometries": [
{
"type": "LineString",
"coordinates": [
[
-86.28524780273438,
40.250184183819854
],
[
-85.98587036132812,
40.17887331434696
],
[
-85.97213745117188,
40.08857859823707
],
[
-85.77987670898438,
40.15578608609647
]
]
},
{
"type": "LineString",
"coordinates": [
[
-86.29348754882811,
40.17362690655496
],
[
-86.23580932617188,
39.891826241725596
],
[
-85.93643188476562,
39.95606977009003
]
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"stroke": "#F00",
"stroke-width": 6
},
"geometry": {
"type": "LineString",
"coordinates": [
[
-86.285248,
40.250184
],
[
-85.98587,
40.178873
],
[
-85.972137,
40.088579
],
[
-85.779877,
40.155786
]
]
}
},
{
"type": "Feature",
"properties": {
"stroke": "#F00",
"stroke-width": 6
},
"geometry": {
"type": "LineString",
"coordinates": [
[
-86.293488,
40.173627
],
[
-86.235809,
39.891826
],
[
-85.936432,
39.95607
]
]
}
}
]
}
Loading

0 comments on commit 46b8502

Please sign in to comment.