turf-convex
Advanced tools
Comparing version 1.0.0 to 1.0.1
129
index.js
@@ -1,46 +0,87 @@ | ||
// http://en.wikibooks.org/wiki/Algorithm_Implementation/Geometry/Convex_hull/Monotone_chain#JavaScript | ||
var each = require('turf-meta').coordEach, | ||
convexHull = require('convex-hull'), | ||
polygon = require('turf-polygon'); | ||
module.exports = function(fc){ | ||
var points = fc.features.map(function(point){ | ||
return point.geometry.coordinates; | ||
}); | ||
points.sort(function(a, b) { | ||
return a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]; | ||
}); | ||
var lower = []; | ||
for (var i = 0; i < points.length; i++) { | ||
while (lower.length >= 2 && cross(lower[lower.length - 2], lower[lower.length - 1], points[i]) <= 0) { | ||
lower.pop(); | ||
} | ||
lower.push(points[i]); | ||
/** | ||
* Takes any {@link GeoJSON} object and returns a | ||
* [convex hull](http://en.wikipedia.org/wiki/Convex_hull) polygon. | ||
* | ||
* Internally this uses | ||
* the [convex-hull](https://github.com/mikolalysenko/convex-hull) module that | ||
* implements a [monotone chain hull](http://en.wikibooks.org/wiki/Algorithm_Implementation/Geometry/Convex_hull/Monotone_chain). | ||
* | ||
* @module turf/convex | ||
* @category transformation | ||
* @param {GeoJSON} input any GeoJSON object | ||
* @returns {Feature} a {@link Polygon} feature | ||
* @example | ||
* var points = { | ||
* "type": "FeatureCollection", | ||
* "features": [ | ||
* { | ||
* "type": "Feature", | ||
* "properties": {}, | ||
* "geometry": { | ||
* "type": "Point", | ||
* "coordinates": [10.195312, 43.755225] | ||
* } | ||
* }, { | ||
* "type": "Feature", | ||
* "properties": {}, | ||
* "geometry": { | ||
* "type": "Point", | ||
* "coordinates": [10.404052, 43.8424511] | ||
* } | ||
* }, { | ||
* "type": "Feature", | ||
* "properties": {}, | ||
* "geometry": { | ||
* "type": "Point", | ||
* "coordinates": [10.579833, 43.659924] | ||
* } | ||
* }, { | ||
* "type": "Feature", | ||
* "properties": {}, | ||
* "geometry": { | ||
* "type": "Point", | ||
* "coordinates": [10.360107, 43.516688] | ||
* } | ||
* }, { | ||
* "type": "Feature", | ||
* "properties": {}, | ||
* "geometry": { | ||
* "type": "Point", | ||
* "coordinates": [10.14038, 43.588348] | ||
* } | ||
* }, { | ||
* "type": "Feature", | ||
* "properties": {}, | ||
* "geometry": { | ||
* "type": "Point", | ||
* "coordinates": [10.195312, 43.755225] | ||
* } | ||
* } | ||
* ] | ||
* }; | ||
* | ||
* var hull = turf.convex(points); | ||
* | ||
* var resultFeatures = points.features.concat(hull); | ||
* var result = { | ||
* "type": "FeatureCollection", | ||
* "features": resultFeatures | ||
* }; | ||
* | ||
* //=result | ||
*/ | ||
module.exports = function(fc) { | ||
var points = []; | ||
each(fc, function(coord) { points.push(coord); }); | ||
var hull = convexHull(points); | ||
var ring = []; | ||
for (var i = 0; i < hull.length; i++) { | ||
ring.push(points[hull[i][0]]); | ||
} | ||
var upper = []; | ||
for (var i = points.length - 1; i >= 0; i--) { | ||
while (upper.length >= 2 && cross(upper[upper.length - 2], upper[upper.length - 1], points[i]) <= 0) { | ||
upper.pop(); | ||
} | ||
upper.push(points[i]); | ||
} | ||
upper.pop(); | ||
lower.pop(); | ||
var coords = lower.concat(upper); | ||
coords.push(coords[0]); | ||
return { | ||
type:'Feature', | ||
properties: {}, | ||
geometry: { | ||
type:'Polygon', | ||
coordinates: [ | ||
coords | ||
] | ||
} | ||
}; | ||
} | ||
function cross(o, a, b) { | ||
return (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]); | ||
} | ||
ring.push(points[hull[hull.length - 1][1]]); | ||
return polygon([ring]); | ||
}; |
{ | ||
"name": "turf-convex", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "", | ||
@@ -10,3 +10,4 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "tape test/*.js" | ||
"test": "tape test/*.js", | ||
"doc": "dox -r < index.js | doxme --readme > README.md" | ||
}, | ||
@@ -29,6 +30,12 @@ "repository": { | ||
"benchmark": "^1.0.0", | ||
"glob": "~4.3.1", | ||
"tape": "~3.0.3" | ||
"glob": "~4.3.5", | ||
"tape": "~3.5.0", | ||
"dox": "^0.6.1", | ||
"doxme": "^1.4.3" | ||
}, | ||
"dependencies": {} | ||
"dependencies": { | ||
"convex-hull": "^1.0.3", | ||
"turf-meta": "^1.0.2", | ||
"turf-polygon": "^1.0.2" | ||
} | ||
} |
@@ -1,36 +0,59 @@ | ||
turf-convex | ||
=============== | ||
# turf-convex | ||
[![build status](https://secure.travis-ci.org/Turfjs/turf-convex.png)](http://travis-ci.org/Turfjs/turf-convex) | ||
Takes a set of points and returns a convex hull polygon. | ||
###Install | ||
```sh | ||
npm install turf-convex | ||
``` | ||
###Parameters | ||
### `turf.convex(input)` | ||
|name|description| | ||
|---|---| | ||
|points|A FeatureCollection of Point Features| | ||
Takes any GeoJSON object and returns a | ||
[convex hull](http://en.wikipedia.org/wiki/Convex_hull) polygon. | ||
###Usage | ||
Internally this implements | ||
a [Monotone chain algorithm](http://en.wikibooks.org/wiki/Algorithm_Implementation/Geometry/Convex_hull/Monotone_chain#JavaScript). | ||
### Parameters | ||
| parameter | type | description | | ||
| --------- | ------- | ------------------ | | ||
| `input` | GeoJSON | any GeoJSON object | | ||
### Example | ||
```js | ||
convex(pts) | ||
var points = turf.featurecollection([ | ||
turf.point([10.195312, 43.755225]), | ||
turf.point([10.404052, 43.8424511]), | ||
turf.point([10.579833, 43.659924]), | ||
turf.point([10.360107, 43.516688]), | ||
turf.point([10.14038, 43.588348]), | ||
turf.point([10.195312, 43.755225])]); | ||
var hull = turf.convex(points); | ||
var result = turf.featurecollection( | ||
points.features.concat(hull)); | ||
//=result | ||
``` | ||
###Example | ||
```js | ||
var convex = require('turf-convex') | ||
var fs = require('fs') | ||
**Returns** `Feature`, a Polygon feature | ||
var pts = JSON.parse(fs.readFileSync('/path/to/pts.geojson')) | ||
var hull = convex(pts) | ||
## Installation | ||
console.log(hull) | ||
``` | ||
Requires [nodejs](http://nodejs.org/). | ||
```sh | ||
$ npm install turf-convex | ||
``` | ||
## Tests | ||
```sh | ||
$ npm test | ||
``` | ||
@@ -6,12 +6,10 @@ var convex = require('../'), | ||
var REGEN = true; | ||
test('intersect', function(t){ | ||
test('convex hull', function(t){ | ||
glob.sync(__dirname + '/fixtures/in/*.geojson').forEach(function(input) { | ||
var fcs = JSON.parse(fs.readFileSync(input)); | ||
var output = convex(fcs); | ||
if (REGEN) fs.writeFileSync(input.replace('/in/', '/out/'), JSON.stringify(output)); | ||
if (process.env.UPDATE) fs.writeFileSync(input.replace('/in/', '/out/'), JSON.stringify(output)); | ||
t.deepEqual(output, JSON.parse(fs.readFileSync(input.replace('/in/', '/out/'))), input); | ||
}); | ||
t.end(); | ||
}) | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15751
113
60
3
5
3
+ Addedconvex-hull@^1.0.3
+ Addedturf-meta@^1.0.2
+ Addedturf-polygon@^1.0.2
+ Addedaffine-hull@1.0.0(transitive)
+ Addedbit-twiddle@1.0.2(transitive)
+ Addedconvex-hull@1.0.3(transitive)
+ Addedincremental-convex-hull@1.0.1(transitive)
+ Addedmonotone-convex-hull-2d@1.0.1(transitive)
+ Addedrobust-orientation@1.2.1(transitive)
+ Addedrobust-scale@1.0.2(transitive)
+ Addedrobust-subtract@1.0.0(transitive)
+ Addedrobust-sum@1.0.0(transitive)
+ Addedsimplicial-complex@1.0.0(transitive)
+ Addedturf-meta@1.0.2(transitive)
+ Addedturf-polygon@1.0.3(transitive)
+ Addedtwo-product@1.0.2(transitive)
+ Addedtwo-sum@1.0.0(transitive)
+ Addedunion-find@1.0.2(transitive)