-
Notifications
You must be signed in to change notification settings - Fork 944
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
@@ -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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Didn't notice |
||
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); | ||
|
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 | ||
] | ||
] | ||
} | ||
} | ||
] | ||
} |
There was a problem hiding this comment.
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 allGeometry Collection
&Geometry Objects
.There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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