-
Notifications
You must be signed in to change notification settings - Fork 942
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
Additional @turf/concave
refactoring
#907
Changes from 5 commits
b753f16
93c66f2
f7885be
1abc084
1d4bc35
3abf424
5a67910
8542491
2354cd9
c5d204a
6aec615
eb30104
bd2720a
97647e0
f41d202
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,12 @@ | ||
// 1. run tin on points | ||
// 2. calculate length of all edges and area of all triangles | ||
// 3. remove triangles that fail the max length test | ||
// 4. buffer the results slightly | ||
// 5. merge the results | ||
var tin = require('@turf/tin'); | ||
var union = require('@turf/union'); | ||
var helpers = require('@turf/helpers'); | ||
var distance = require('@turf/distance'); | ||
var clone = require('@turf/clone'); | ||
var dissolve = require('geojson-dissolve'); | ||
var feature = helpers.feature; | ||
var featureCollection = helpers.featureCollection; | ||
|
||
/** | ||
* Takes a set of {@link Point|points} and returns a concave hull polygon. | ||
* Takes a set of {@link Point|points} and returns a concave hull Polygon or MultiPolygon. | ||
* Internally, this uses [turf-tin](https://github.com/Turfjs/turf-tin) to generate geometries. | ||
* | ||
* @name concave | ||
|
@@ -34,11 +31,16 @@ var clone = require('@turf/clone'); | |
* var addToMap = [points, hull] | ||
*/ | ||
module.exports = function (points, maxEdge, units) { | ||
// validation | ||
if (!points) throw new Error('points is required'); | ||
if (maxEdge === undefined || maxEdge === null) throw new Error('maxEdge is required'); | ||
if (typeof maxEdge !== 'number') throw new Error('invalid maxEdge'); | ||
|
||
var tinPolys = tin(points); | ||
var cleaned = removeDuplicates(points); | ||
|
||
var tinPolys = tin(cleaned); | ||
// calculate length of all edges and area of all triangles | ||
// and remove triangles that fail the max length test | ||
tinPolys.features = tinPolys.features.filter(function (triangle) { | ||
var pt1 = triangle.geometry.coordinates[0][0]; | ||
var pt2 = triangle.geometry.coordinates[0][1]; | ||
|
@@ -51,23 +53,32 @@ module.exports = function (points, maxEdge, units) { | |
|
||
if (tinPolys.features.length < 1) throw new Error('too few polygons found to compute concave hull'); | ||
|
||
return merge(tinPolys.features); | ||
// merge the adjacent triangles | ||
var dissolved = dissolve(tinPolys.features); | ||
// geojson-dissolve always returns a MultiPolygon | ||
if (dissolved.coordinates.length === 1) { | ||
dissolved.coordinates = dissolved.coordinates[0]; | ||
dissolved.type = 'Polygon'; | ||
} | ||
return feature(dissolved); | ||
}; | ||
|
||
/** | ||
* Merges/Unifies all the features in a single polygon | ||
* Removes duplicated points in a collection returning a new collection | ||
* | ||
* @private | ||
* @param {Array<Feature>} features to be merged | ||
* @returns {Feature<(Polygon|MultiPolygon)>} merged result | ||
* @param {FeatureCollection<Point>} points to be cleaned | ||
* @returns {FeatureCollection<Point>} cleaned set of points | ||
*/ | ||
function merge(features) { | ||
var merged = clone(features[0]); | ||
merged.properties = {}; | ||
|
||
for (var i = 0, len = features.length; i < len; i++) { | ||
var poly = features[i]; | ||
if (poly.geometry) merged = union(merged, poly); | ||
} | ||
return merged; | ||
function removeDuplicates(points) { | ||
var cleaned = []; | ||
var existing = {}; | ||
points.features.forEach(function (pt) { | ||
var key = pt.geometry.coordinates.join('-'); | ||
if (!existing.hasOwnProperty(key)) { | ||
cleaned.push(pt); | ||
existing[key] = true; | ||
} | ||
}); | ||
return featureCollection(cleaned, points.properties); | ||
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. FeatureCollection doesn't have 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. ah... ok! 👍 |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,25 +24,31 @@ | |
], | ||
"author": "Turf Authors", | ||
"contributors": [ | ||
"Stefano Borghi <@stebogit>" | ||
"Tom MacWright <@tmcw>", | ||
"Lyzi Diamond <@lyzidiamond>", | ||
"Denis Carriere <@DenisCarriere>", | ||
"Stefano Borghi <@stebogit>", | ||
"Rowan Winsemius <@rowanwins>", | ||
"Daniel Pulido <@dpmcmlxxvi>", | ||
"Stephen Whitmore <@noffle>", | ||
"Gregor MacLennan <@gmaclennan>" | ||
], | ||
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 would also include the Authors/Contributors of
|
||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/Turfjs/turf/issues" | ||
}, | ||
"homepage": "https://github.com/Turfjs/turf", | ||
"devDependencies": { | ||
"@turf/helpers": "^4.6.0", | ||
"@turf/meta": "^4.6.0", | ||
"benchmark": "^2.1.4", | ||
"load-json-file": "^2.0.0", | ||
"tape": "^4.6.3", | ||
"write-json-file": "^2.1.4" | ||
}, | ||
"dependencies": { | ||
"@turf/distance": "^4.6.0", | ||
"@turf/tin": "^4.6.0", | ||
"@turf/union": "^4.6.0", | ||
"@turf/clone": "^4.6.0" | ||
"@turf/distance": "4.6.0", | ||
"@turf/helpers": "^4.6.0", | ||
"@turf/tin": "4.6.0", | ||
"geojson-dissolve": "3.1.0" | ||
} | ||
} |
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.
👍 This syntax looked so familiar that I thought I wrote this part :)
https://github.com/Turfjs/turf/blob/master/packages/turf-clean-coords/index.js#L52-L58
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.
Yes! I told you it was sooo smart! 😄