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

line-segment: Simplify implementation and add GeometryCollection test fixture #727

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-segment/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**

Expand Down
66 changes: 20 additions & 46 deletions packages/turf-line-segment/index.js
Original file line number Diff line number Diff line change
@@ -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<LineString|MultiLineString|MultiPolygon|Polygon>} geojson GeoJSON Polygon or LineString
* @param {GeometryCollection|Geometry|FeatureCollection|Feature<LineString|MultiLineString|MultiPolygon|Polygon>} geojson GeoJSON Polygon or LineString
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.

These JSDocs are becoming so long!... 😮
I wonder if we could shorthand the Multi Geometries

To me GeometryCollection is Geometry (it was just never supported correctly in the past).

/**
 * @param {Geometry|FeatureCollection|Feature<(Multi)LineString|(Multi)Polygon>}

* @returns {FeatureCollection<LineString>} 2-vertex line segments
* @example
* var polygon = {
Expand All @@ -32,56 +27,35 @@ 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) {
Copy link
Member

Choose a reason for hiding this comment

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

So Clean!! ❤️

lineSegment(feature, results);
});
return featureCollection(results);
};

/**
* Line Segment
*
* @private
* @param {Geomtry|Feature<any>} geojson GeoJSON Feature or Geometry
* @param {Feature<LineString|Polygon>} 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) {
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.

Love it! 🏅 Removing LOCs is the best

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);
});
});
}
Expand Down
1 change: 0 additions & 1 deletion packages/turf-line-segment/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
96 changes: 96 additions & 0 deletions packages/turf-line-segment/test/in/geometry-collection.geojson
Original file line number Diff line number Diff line change
@@ -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
]
}
]
}
Loading