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

No concaveman #369

Merged
merged 2 commits into from
May 11, 2016
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
42 changes: 33 additions & 9 deletions packages/turf-concave/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
// 3. remove triangles that fail the max length test
// 4. buffer the results slightly
// 5. merge the results
var concaveman = require('concaveman');
var coordAll = require('turf-meta').coordAll;
var tin = require('turf-tin');
var union = require('turf-union');
var distance = require('turf-distance');

/**
* Takes a set of {@link Point|points} and returns a concave hull polygon.
*
* Internally, this uses [turf-tin](https://github.com/Turfjs/turf-tin) to generate geometries.
*
* @name concave
* @module concave
* @category transformation
* @param {FeatureCollection<Point>} points input points
* @param {Number} maxEdge the size of an edge necessary for part of the
Expand Down Expand Up @@ -81,14 +82,37 @@ var coordAll = require('turf-meta').coordAll;
* //=result
*/

module.exports = function (layer, maxEdge, units) {

module.exports = function (points, maxEdge, units) {
if (typeof maxEdge !== 'number') throw new Error('maxEdge parameter is required');
if (typeof units !== 'string') throw new Error('units parameter is required');

var hull = concaveman(coordAll(layer));
var tinPolys = tin(points);
var filteredPolys = tinPolys.features.filter(filterTriangles);
tinPolys.features = filteredPolys;

function filterTriangles(triangle) {
var pt1 = triangle.geometry.coordinates[0][0];
var pt2 = triangle.geometry.coordinates[0][1];
var pt3 = triangle.geometry.coordinates[0][2];
var dist1 = distance(pt1, pt2, units);
var dist2 = distance(pt2, pt3, units);
var dist3 = distance(pt1, pt3, units);
return (dist1 <= maxEdge && dist2 <= maxEdge && dist3 <= maxEdge);
}

return {
type: 'Polygon',
coordinates: [hull]
};
return merge(tinPolys);
};

function merge(polygons) {
var merged = JSON.parse(JSON.stringify(polygons.features[0])),
features = polygons.features;

for (var i = 0, len = features.length; i < len; i++) {
var poly = features[i];
if (poly.geometry) {
merged = union(merged, poly);
}
}
return merged;
}
4 changes: 3 additions & 1 deletion packages/turf-concave/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
},
"dependencies": {
"turf-meta": "^3.0.0",
"concaveman": "1.1.0"
"turf-tin": "^3.0.0",
"turf-union": "^3.0.0",
"turf-distance": "^3.0.0"
}
}
4 changes: 2 additions & 2 deletions packages/turf-concave/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ test('concave', function(t){
var pts1HullMiles = concave(pts1, 5.5, 'miles');
var pts1HullKilometers = concave(pts1, 5.5 * 1.60934, 'kilometers');
t.ok(pts1HullMiles, 'computes hull');
t.equal(pts1HullMiles.type, 'Polygon');
t.equal(pts1HullMiles.type, 'Feature');
t.deepEqual(pts1HullMiles, pts1HullKilometers, 'miles and km should return the same result');

var pts2HullMiles = concave(pts2, 2, 'miles');
var pts2HullKilometers = concave(pts2, 2 * 1.60934, 'kilometers');
t.ok(pts2HullMiles, 'computes hull');
t.equal(pts2HullMiles.type, 'Polygon');
t.equal(pts2HullMiles.type, 'Feature');
t.deepEqual(pts2HullMiles, pts2HullKilometers, 'miles and km should return the same result');

// output results
Expand Down
118 changes: 68 additions & 50 deletions packages/turf-concave/test/fixtures/out/pts1_out.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -184,63 +184,81 @@
}
},
{
"type": "Polygon",
"coordinates": [
[
[
-122.47833251953125,
37.73651223296987
],
[
-122.53395080566405,
37.83690319650768
],
[
-122.62527465820311,
37.89327929625019
],
[
-122.60467529296875,
37.902490518640995
],
[
-122.58682250976562,
37.895988598965644
],
[
-122.52639770507812,
37.83473402375478
],
[
-122.51541137695311,
37.83473402375478
],
[
-122.49069213867188,
37.837445479729666
],
[
-122.40898132324217,
37.77505678240509
],
[
-122.41790771484375,
37.74682893940135
],
"type": "Feature",
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
-122.4103546142578,
37.72184917678752
[
[
-122.62527465820311,
37.89327929625019
],
[
-122.60467529296875,
37.902490518640995
],
[
-122.58682250976562,
37.895988598965644
],
[
-122.62527465820311,
37.89327929625019
]
]
],
[
-122.44331359863283,
37.726194088705576
[
[
-122.52639770507812,
37.83473402375478
],
[
-122.53395080566405,
37.83690319650768
],
[
-122.51541137695311,
37.83473402375478
],
[
-122.52639770507812,
37.83473402375478
]
]
],
[
-122.47833251953125,
37.73651223296987
[
[
-122.44331359863283,
37.726194088705576
],
[
-122.47833251953125,
37.73651223296987
],
[
-122.43095397949219,
37.74411415606583
],
[
-122.40898132324217,
37.77505678240509
],
[
-122.4103546142578,
37.72184917678752
],
[
-122.44331359863283,
37.726194088705576
]
]
]
]
]
},
"properties": {}
}
]
}
Loading