@turf/concave
Advanced tools
Comparing version 4.6.0 to 4.7.0
/// <reference types="geojson" /> | ||
import {Units} from '@turf/helpers' | ||
type Points = GeoJSON.FeatureCollection<GeoJSON.Point>; | ||
type Polygon = GeoJSON.Feature<GeoJSON.Polygon>; | ||
type Polygon = GeoJSON.Feature<GeoJSON.Polygon | GeoJSON.MultiPolygon>; | ||
@@ -9,4 +11,4 @@ /** | ||
*/ | ||
declare function concave(points: Points, maxEdge: number, units: string): Polygon; | ||
declare function concave(points: Points, maxEdge: number, units?: Units): Polygon; | ||
declare namespace concave { } | ||
export = concave; |
81
index.js
@@ -1,18 +0,18 @@ | ||
// 1. run tin on points | ||
// 2. calculate lenth 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 dissolve = require('geojson-dissolve'); | ||
var featureEach = require('@turf/meta').featureEach; | ||
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 | ||
* @param {FeatureCollection<Point>} points input points | ||
* @param {number} maxEdge the size of an edge necessary for part of the hull to become concave (in miles) | ||
* @param {number} maxEdge the length (in 'units') of an edge necessary for part of the hull to become concave | ||
* @param {string} [units=kilometers] can be degrees, radians, miles, or kilometers | ||
* @returns {Feature<Polygon>} a concave hull | ||
* @returns {Feature<(Polygon|MultiPolygon)>} a concave hull | ||
* @throws {Error} if maxEdge parameter is missing or unable to compute hull | ||
@@ -34,13 +34,14 @@ * @example | ||
*/ | ||
function concave(points, maxEdge, units) { | ||
if (typeof maxEdge !== 'number') throw new Error('maxEdge parameter is required'); | ||
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 filteredPolys = tinPolys.features.filter(filterTriangles); | ||
tinPolys.features = filteredPolys; | ||
if (tinPolys.features.length < 1) { | ||
throw new Error('too few polygons found to compute concave hull'); | ||
} | ||
var cleaned = removeDuplicates(points); | ||
function filterTriangles(triangle) { | ||
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]; | ||
@@ -53,20 +54,36 @@ var pt2 = triangle.geometry.coordinates[0][1]; | ||
return (dist1 <= maxEdge && dist2 <= maxEdge && dist3 <= maxEdge); | ||
}); | ||
if (tinPolys.features.length < 1) throw new Error('too few polygons found to compute concave hull'); | ||
// 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); | ||
}; | ||
return merge(tinPolys); | ||
} | ||
/** | ||
* Removes duplicated points in a collection returning a new collection | ||
* | ||
* @private | ||
* @param {FeatureCollection<Point>} points to be cleaned | ||
* @returns {FeatureCollection<Point>} cleaned set of points | ||
*/ | ||
function removeDuplicates(points) { | ||
var cleaned = []; | ||
var existing = {}; | ||
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); | ||
featureEach(points, function (pt) { | ||
if (!pt.geometry) return; | ||
var key = pt.geometry.coordinates.join('-'); | ||
if (!existing.hasOwnProperty(key)) { | ||
cleaned.push(pt); | ||
existing[key] = true; | ||
} | ||
} | ||
return merged; | ||
}); | ||
return featureCollection(cleaned); | ||
} | ||
module.exports = concave; |
{ | ||
"name": "@turf/concave", | ||
"version": "4.6.0", | ||
"version": "4.7.0", | ||
"description": "turf concave module", | ||
@@ -22,5 +22,17 @@ "main": "index.js", | ||
"gis", | ||
"concave", | ||
"geometry" | ||
], | ||
"author": "Turf Authors", | ||
"contributors": [ | ||
"Tom MacWright <@tmcw>", | ||
"Lyzi Diamond <@lyzidiamond>", | ||
"Denis Carriere <@DenisCarriere>", | ||
"Stefano Borghi <@stebogit>", | ||
"Rowan Winsemius <@rowanwins>", | ||
"Daniel Pulido <@dpmcmlxxvi>", | ||
"Stephen Whitmore <@noffle>", | ||
"Gregor MacLennan <@gmaclennan>", | ||
"Mike Bostock <@mbostock>" | ||
], | ||
"license": "MIT", | ||
@@ -32,12 +44,14 @@ "bugs": { | ||
"devDependencies": { | ||
"@turf/helpers": "^4.6.0", | ||
"benchmark": "^2.1.4", | ||
"glob": "~4.3.5", | ||
"tape": "^4.6.3" | ||
"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/distance": "4.7.0", | ||
"@turf/helpers": "4.7.0", | ||
"@turf/meta": "4.7.0", | ||
"@turf/tin": "4.7.0", | ||
"geojson-dissolve": "3.1.0" | ||
} | ||
} |
@@ -5,3 +5,3 @@ # @turf/concave | ||
Takes a set of [points](http://geojson.org/geojson-spec.html#point) and returns a concave hull polygon. | ||
Takes a set of [points](http://geojson.org/geojson-spec.html#point) and returns a concave hull Polygon or MultiPolygon. | ||
Internally, this uses [turf-tin](https://github.com/Turfjs/turf-tin) to generate geometries. | ||
@@ -12,3 +12,3 @@ | ||
- `points` **[FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects)<[Point](http://geojson.org/geojson-spec.html#point)>** input points | ||
- `maxEdge` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** the size of an edge necessary for part of the hull to become concave (in miles) | ||
- `maxEdge` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** the length (in 'units') of an edge necessary for part of the hull to become concave | ||
- `units` **\[[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)]** can be degrees, radians, miles, or kilometers (optional, default `kilometers`) | ||
@@ -36,3 +36,3 @@ | ||
Returns **[Feature](http://geojson.org/geojson-spec.html#feature-objects)<[Polygon](http://geojson.org/geojson-spec.html#polygon)>** a concave hull | ||
Returns **[Feature](http://geojson.org/geojson-spec.html#feature-objects)<([Polygon](http://geojson.org/geojson-spec.html#polygon) \| [MultiPolygon](http://geojson.org/geojson-spec.html#multipolygon))>** a concave hull | ||
@@ -39,0 +39,0 @@ <!-- This file is automatically generated. Please don't edit it directly: |
8226
90
5
+ Added@turf/helpers@4.7.0
+ Added@turf/meta@4.7.0
+ Addedgeojson-dissolve@3.1.0
+ Added@turf/distance@4.7.0(transitive)
+ Added@turf/helpers@4.7.0(transitive)
+ Added@turf/invariant@4.7.0(transitive)
+ Added@turf/meta@3.14.04.7.0(transitive)
+ Addedcommander@2.20.3(transitive)
+ Addedgeojson-dissolve@3.1.0(transitive)
+ Addedgeojson-flatten@0.2.4(transitive)
+ Addedgeojson-linestring-dissolve@0.0.1(transitive)
+ Addedget-stdin@6.0.0(transitive)
+ Addedminimist@1.2.0(transitive)
+ Addedtopojson-client@3.1.0(transitive)
+ Addedtopojson-server@3.0.1(transitive)
- Removed@turf/union@^4.6.0
- Removed@turf/distance@4.7.3(transitive)
- Removed@turf/helpers@4.7.3(transitive)
- Removed@turf/invariant@4.7.3(transitive)
- Removed@turf/tin@4.7.3(transitive)
- Removed@turf/union@4.7.3(transitive)
- Removedjsts@1.3.0(transitive)
Updated@turf/distance@4.7.0
Updated@turf/tin@4.7.0