Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support GeometryCollection in @turf/line-chunk. #726

Merged
merged 1 commit into from
May 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

@DenisCarriere DenisCarriere May 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the JSDocs I've been using Geometry to encompass all Geometry Collection & Geometry Objects.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. A GeometryCollection is a Geometry per the spec. In my head I think of them as different since it's strange to think of a collection of something to also be that something, but it is.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've never really understood the difference between both of them until you've pointed it out to me. Geometry Collections is hard one to support, however with the latest flattenEach method it's pretty simple now.
+1

* @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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this would cause the input geojson to be mutated, I'll add a test case for that and see if this method causes input mutation.

}

// 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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't notice (debug===true) was still here, this section can be removed since it can easily be handled in the test.js.

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