From caf65068e17fb71322b6e5392aa62244fcf2d887 Mon Sep 17 00:00:00 2001 From: dpmcmlxxvi Date: Tue, 9 May 2017 01:28:55 -0400 Subject: [PATCH] Support GeometryCollection in @turf/line-chunk. --- packages/turf-line-chunk/README.md | 2 +- packages/turf-line-chunk/index.js | 40 +-- packages/turf-line-chunk/package.json | 1 - .../test/in/GeometryCollection.geojson | 43 +++ .../out/GeometryCollection.longer.geojson | 57 ++++ .../out/GeometryCollection.reverse.geojson | 317 ++++++++++++++++++ .../out/GeometryCollection.shorter.geojson | 317 ++++++++++++++++++ 7 files changed, 750 insertions(+), 27 deletions(-) create mode 100644 packages/turf-line-chunk/test/in/GeometryCollection.geojson create mode 100644 packages/turf-line-chunk/test/out/GeometryCollection.longer.geojson create mode 100644 packages/turf-line-chunk/test/out/GeometryCollection.reverse.geojson create mode 100644 packages/turf-line-chunk/test/out/GeometryCollection.shorter.geojson diff --git a/packages/turf-line-chunk/README.md b/packages/turf-line-chunk/README.md index edd220790..df607738e 100644 --- a/packages/turf-line-chunk/README.md +++ b/packages/turf-line-chunk/README.md @@ -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`) diff --git a/packages/turf-line-chunk/index.js b/packages/turf-line-chunk/index.js index 5bdb83205..b28f9c3b0 100644 --- a/packages/turf-line-chunk/index.js +++ b/packages/turf-line-chunk/index.js @@ -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} featureIn the lines to split + * @param {FeatureCollection|Feature|GeometryCollection} 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(); } - - // 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); diff --git a/packages/turf-line-chunk/package.json b/packages/turf-line-chunk/package.json index dc92d5c96..39715f268 100644 --- a/packages/turf-line-chunk/package.json +++ b/packages/turf-line-chunk/package.json @@ -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", diff --git a/packages/turf-line-chunk/test/in/GeometryCollection.geojson b/packages/turf-line-chunk/test/in/GeometryCollection.geojson new file mode 100644 index 000000000..66a526b93 --- /dev/null +++ b/packages/turf-line-chunk/test/in/GeometryCollection.geojson @@ -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 + ] + ] + } + ] +} diff --git a/packages/turf-line-chunk/test/out/GeometryCollection.longer.geojson b/packages/turf-line-chunk/test/out/GeometryCollection.longer.geojson new file mode 100644 index 000000000..4c8b4522e --- /dev/null +++ b/packages/turf-line-chunk/test/out/GeometryCollection.longer.geojson @@ -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 + ] + ] + } + } + ] +} diff --git a/packages/turf-line-chunk/test/out/GeometryCollection.reverse.geojson b/packages/turf-line-chunk/test/out/GeometryCollection.reverse.geojson new file mode 100644 index 000000000..5ae421c7c --- /dev/null +++ b/packages/turf-line-chunk/test/out/GeometryCollection.reverse.geojson @@ -0,0 +1,317 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -85.779877, + 40.155786 + ], + [ + -85.865966, + 40.125748 + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -85.865966, + 40.125748 + ], + [ + -85.95198, + 40.095646 + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -85.95198, + 40.095646 + ], + [ + -85.972137, + 40.088579 + ], + [ + -85.980499, + 40.143588 + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -85.980499, + 40.143588 + ], + [ + -85.98587, + 40.178873 + ], + [ + -86.031859, + 40.189888 + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -86.031859, + 40.189888 + ], + [ + -86.12226, + 40.211475 + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -86.12226, + 40.211475 + ], + [ + -86.212718, + 40.232992 + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -86.212718, + 40.232992 + ], + [ + -86.285248, + 40.250184 + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -85.936432, + 39.95607 + ], + [ + -86.027334, + 39.936657 + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -86.027334, + 39.936657 + ], + [ + -86.118185, + 39.917174 + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -86.118185, + 39.917174 + ], + [ + -86.208984, + 39.897619 + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -86.208984, + 39.897619 + ], + [ + -86.235809, + 39.891826 + ], + [ + -86.246079, + 39.942176 + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -86.246079, + 39.942176 + ], + [ + -86.260684, + 40.013648 + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -86.260684, + 40.013648 + ], + [ + -86.27532, + 40.085119 + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -86.27532, + 40.085119 + ], + [ + -86.289986, + 40.156587 + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -86.289986, + 40.156587 + ], + [ + -86.293488, + 40.173627 + ] + ] + } + } + ] +} diff --git a/packages/turf-line-chunk/test/out/GeometryCollection.shorter.geojson b/packages/turf-line-chunk/test/out/GeometryCollection.shorter.geojson new file mode 100644 index 000000000..7d6496ba2 --- /dev/null +++ b/packages/turf-line-chunk/test/out/GeometryCollection.shorter.geojson @@ -0,0 +1,317 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -86.285248, + 40.250184 + ], + [ + -86.194743, + 40.228723 + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -86.194743, + 40.228723 + ], + [ + -86.104296, + 40.207192 + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -86.104296, + 40.207192 + ], + [ + -86.013907, + 40.185591 + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -86.013907, + 40.185591 + ], + [ + -85.98587, + 40.178873 + ], + [ + -85.978328, + 40.129313 + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -85.978328, + 40.129313 + ], + [ + -85.972137, + 40.088579 + ], + [ + -85.934899, + 40.101631 + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -85.934899, + 40.101631 + ], + [ + -85.84887, + 40.13172 + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -85.84887, + 40.13172 + ], + [ + -85.779877, + 40.155786 + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -86.293488, + 40.173627 + ], + [ + -86.278814, + 40.102159 + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -86.278814, + 40.102159 + ], + [ + -86.264171, + 40.030689 + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -86.264171, + 40.030689 + ], + [ + -86.249559, + 39.959217 + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -86.249559, + 39.959217 + ], + [ + -86.235809, + 39.891826 + ], + [ + -86.230625, + 39.892946 + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -86.230625, + 39.892946 + ], + [ + -86.139838, + 39.912518 + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -86.139838, + 39.912518 + ], + [ + -86.049, + 39.932018 + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -86.049, + 39.932018 + ], + [ + -85.95811, + 39.951448 + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "stroke-width": 6 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -85.95811, + 39.951448 + ], + [ + -85.936432, + 39.95607 + ] + ] + } + } + ] +}