diff --git a/packages/turf-line-segment/README.md b/packages/turf-line-segment/README.md index bdb2791aa2..42ffbfa50c 100644 --- a/packages/turf-line-segment/README.md +++ b/packages/turf-line-segment/README.md @@ -6,7 +6,7 @@ Creates a [FeatureCollection](http://geojson.org/geojson-spec.html#feature-colle **Parameters** -- `geojson` **([Geometry](http://geojson.org/geojson-spec.html#geometry) \| [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) \| [MultiPolygon](http://geojson.org/geojson-spec.html#multipolygon) \| [Polygon](http://geojson.org/geojson-spec.html#polygon))>)** GeoJSON Polygon or LineString +- `geojson` **([GeometryCollection](http://geojson.org/geojson-spec.html#geometrycollection) \| [Geometry](http://geojson.org/geojson-spec.html#geometry) \| [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) \| [MultiPolygon](http://geojson.org/geojson-spec.html#multipolygon) \| [Polygon](http://geojson.org/geojson-spec.html#polygon))>)** GeoJSON Polygon or LineString **Examples** diff --git a/packages/turf-line-segment/index.js b/packages/turf-line-segment/index.js index 8a58c69c6f..c4b323cb06 100644 --- a/packages/turf-line-segment/index.js +++ b/packages/turf-line-segment/index.js @@ -1,18 +1,13 @@ -var flatten = require('@turf/flatten'); -var meta = require('@turf/meta'); -var geomEach = meta.geomEach; -var featureEach = meta.featureEach; +var featureCollection = require('@turf/helpers').featureCollection; +var flattenEach = require('@turf/meta').flattenEach; var getCoords = require('@turf/invariant').getCoords; -var helpers = require('@turf/helpers'); -var feature = helpers.feature; -var lineString = helpers.lineString; -var featureCollection = helpers.featureCollection; +var lineString = require('@turf/helpers').lineString; /** * Creates a {@link FeatureCollection} of 2-vertex {@link LineString} segments from a {@link LineString|(Multi)LineString} or {@link Polygon|(Multi)Polygon}. * * @name lineSegment - * @param {Geometry|FeatureCollection|Feature} geojson GeoJSON Polygon or LineString + * @param {GeometryCollection|Geometry|FeatureCollection|Feature} geojson GeoJSON Polygon or LineString * @returns {FeatureCollection} 2-vertex line segments * @example * var polygon = { @@ -32,20 +27,9 @@ module.exports = function (geojson) { if (!geojson) throw new Error('geojson is required'); var results = []; - switch (geojson.type) { - case 'FeatureCollection': - featureEach(geojson, function (feature) { - lineSegment(feature, results); - }); - break; - case 'GeometryCollection': - geomEach(geojson, function (geometry) { - lineSegment(geometry, results); - }); - break; - default: - lineSegment(geojson, results); - } + flattenEach(geojson, function (feature) { + lineSegment(feature, results); + }); return featureCollection(results); }; @@ -53,35 +37,25 @@ module.exports = function (geojson) { * Line Segment * * @private - * @param {Geomtry|Feature} geojson GeoJSON Feature or Geometry + * @param {Feature} geojson Line or polygon feature * @param {Array} results push to results * @returns {void} */ function lineSegment(geojson, results) { - switch (geojson.type) { - case 'Point': - case 'LineString': + var coords = []; + var geometry = geojson.geometry; + switch (geometry.type) { case 'Polygon': - case 'MultiPoint': - case 'MultiLineString': - case 'MultiPolygon': - geojson = feature(geojson); + coords = getCoords(geometry); + break; + case 'LineString': + coords = [getCoords(geometry)]; } - geomEach(flatten(geojson), function (geometry) { - var coords = []; - switch (geometry.type) { - case 'Polygon': - coords = getCoords(geometry); - break; - case 'LineString': - coords = [getCoords(geometry)]; - } - coords.forEach(function (coord) { - var segments = createSegments(coord, geojson.properties); - segments.forEach(function (segment) { - segment.id = results.length; - results.push(segment); - }); + coords.forEach(function (coord) { + var segments = createSegments(coord, geojson.properties); + segments.forEach(function (segment) { + segment.id = results.length; + results.push(segment); }); }); } diff --git a/packages/turf-line-segment/package.json b/packages/turf-line-segment/package.json index 38c48923df..742cd5a1b9 100644 --- a/packages/turf-line-segment/package.json +++ b/packages/turf-line-segment/package.json @@ -34,7 +34,6 @@ "write-json-file": "^2.0.0" }, "dependencies": { - "@turf/flatten": "^4.2.0", "@turf/helpers": "^4.2.0", "@turf/invariant": "^4.2.0", "@turf/meta": "^4.2.0" diff --git a/packages/turf-line-segment/test/in/geometry-collection.geojson b/packages/turf-line-segment/test/in/geometry-collection.geojson new file mode 100644 index 0000000000..8199100455 --- /dev/null +++ b/packages/turf-line-segment/test/in/geometry-collection.geojson @@ -0,0 +1,96 @@ +{ + "type": "GeometryCollection", + "geometries": [ + { + "type": "MultiLineString", + "coordinates": [ + [ + [ + -77.031669, + 38.878605 + ], + [ + -77.029609, + 38.881946 + ], + [ + -77.020339, + 38.884084 + ], + [ + -77.025661, + 38.885821 + ], + [ + -77.021884, + 38.889563 + ], + [ + -77.019824, + 38.892368 + ] + ], + [ + [ + -77.041669, + 38.885821 + ], + [ + -77.039609, + 38.881946 + ], + [ + -77.030339, + 38.884084 + ], + [ + -77.035661, + 38.878605 + ] + ] + ] + }, + { + "type": "Polygon", + "coordinates": [ + [ + [ + -77.0361328125, + 38.8840513003232 + ], + [ + -77.02806472778319, + 38.88602223128263 + ], + [ + -77.02536106109619, + 38.888761400455074 + ], + [ + -77.02557563781738, + 38.89266954433839 + ], + [ + -77.03368663787842, + 38.88882820813984 + ], + [ + -77.03630447387695, + 38.88769246895389 + ], + [ + -77.0361328125, + 38.8840513003232 + ] + ] + ] + }, + { + "type": "Point", + "coordinates": [ + -77.02810764312744, + 38.883049111069546 + ] + } + ] +} \ No newline at end of file diff --git a/packages/turf-line-segment/test/out/geometry-collection.geojson b/packages/turf-line-segment/test/out/geometry-collection.geojson new file mode 100644 index 0000000000..24b65a991b --- /dev/null +++ b/packages/turf-line-segment/test/out/geometry-collection.geojson @@ -0,0 +1,383 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "stroke-width": 10 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -77.031669, + 38.878605 + ], + [ + -77.029609, + 38.881946 + ] + ] + }, + "bbox": [ + -77.031669, + 38.878605, + -77.029609, + 38.881946 + ], + "id": 0 + }, + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "stroke-width": 10 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -77.029609, + 38.881946 + ], + [ + -77.020339, + 38.884084 + ] + ] + }, + "bbox": [ + -77.029609, + 38.881946, + -77.020339, + 38.884084 + ], + "id": 1 + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "stroke-width": 10 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -77.020339, + 38.884084 + ], + [ + -77.025661, + 38.885821 + ] + ] + }, + "bbox": [ + -77.025661, + 38.884084, + -77.020339, + 38.885821 + ], + "id": 2 + }, + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "stroke-width": 10 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -77.025661, + 38.885821 + ], + [ + -77.021884, + 38.889563 + ] + ] + }, + "bbox": [ + -77.025661, + 38.885821, + -77.021884, + 38.889563 + ], + "id": 3 + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "stroke-width": 10 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -77.021884, + 38.889563 + ], + [ + -77.019824, + 38.892368 + ] + ] + }, + "bbox": [ + -77.021884, + 38.889563, + -77.019824, + 38.892368 + ], + "id": 4 + }, + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "stroke-width": 10 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -77.041669, + 38.885821 + ], + [ + -77.039609, + 38.881946 + ] + ] + }, + "bbox": [ + -77.041669, + 38.881946, + -77.039609, + 38.885821 + ], + "id": 5 + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "stroke-width": 10 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -77.039609, + 38.881946 + ], + [ + -77.030339, + 38.884084 + ] + ] + }, + "bbox": [ + -77.039609, + 38.881946, + -77.030339, + 38.884084 + ], + "id": 6 + }, + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "stroke-width": 10 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -77.030339, + 38.884084 + ], + [ + -77.035661, + 38.878605 + ] + ] + }, + "bbox": [ + -77.035661, + 38.878605, + -77.030339, + 38.884084 + ], + "id": 7 + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "stroke-width": 10 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -77.0361328125, + 38.8840513003232 + ], + [ + -77.02806472778319, + 38.88602223128263 + ] + ] + }, + "bbox": [ + -77.0361328125, + 38.8840513003232, + -77.02806472778319, + 38.88602223128263 + ], + "id": 8 + }, + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "stroke-width": 10 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -77.02806472778319, + 38.88602223128263 + ], + [ + -77.02536106109619, + 38.888761400455074 + ] + ] + }, + "bbox": [ + -77.02806472778319, + 38.88602223128263, + -77.02536106109619, + 38.888761400455074 + ], + "id": 9 + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "stroke-width": 10 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -77.02536106109619, + 38.888761400455074 + ], + [ + -77.02557563781738, + 38.89266954433839 + ] + ] + }, + "bbox": [ + -77.02557563781738, + 38.888761400455074, + -77.02536106109619, + 38.89266954433839 + ], + "id": 10 + }, + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "stroke-width": 10 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -77.02557563781738, + 38.89266954433839 + ], + [ + -77.03368663787842, + 38.88882820813984 + ] + ] + }, + "bbox": [ + -77.03368663787842, + 38.88882820813984, + -77.02557563781738, + 38.89266954433839 + ], + "id": 11 + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "stroke-width": 10 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -77.03368663787842, + 38.88882820813984 + ], + [ + -77.03630447387695, + 38.88769246895389 + ] + ] + }, + "bbox": [ + -77.03630447387695, + 38.88769246895389, + -77.03368663787842, + 38.88882820813984 + ], + "id": 12 + }, + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "stroke-width": 10 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -77.03630447387695, + 38.88769246895389 + ], + [ + -77.0361328125, + 38.8840513003232 + ] + ] + }, + "bbox": [ + -77.03630447387695, + 38.8840513003232, + -77.0361328125, + 38.88769246895389 + ], + "id": 13 + } + ] +}