-
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
line-segment: Simplify implementation and add GeometryCollection test fixture #727
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
* @returns {FeatureCollection<LineString>} 2-vertex line segments | ||
* @example | ||
* var polygon = { | ||
|
@@ -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) { | ||
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. 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) { | ||
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. 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); | ||
}); | ||
}); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
packages/turf-line-segment/test/in/geometry-collection.geojson
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
] | ||
} | ||
] | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
These JSDocs are becoming so long!... 😮
I wonder if we could shorthand the Multi Geometries
To me
GeometryCollection
isGeometry
(it was just never supported correctly in the past).