Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

turf-centroid

Package Overview
Dependencies
Maintainers
8
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

turf-centroid - npm Package Compare versions

Comparing version 1.0.1 to 1.0.2

45

index.js

@@ -1,16 +0,35 @@

var explode = require('turf-explode');
var each = require('turf-meta').coordEach;
var point = require('turf-point');
/**
* Takes a {@link Feature} or {@link FeatureCollection} of any type and calculates the centroid using the arithmetic mean of all vertices.
* This lessens the effect of small islands and artifacts when calculating
* the centroid of a set of polygons.
*
* @module turf/centroid
* @param {FeatureCollection} fc a {@link Feature} or FeatureCollection of any type
* @return {Point} a Point feature at the centroid of the input feature(s)
* @example
* var poly = turf.polygon([[
* [105.818939,21.004714],
* [105.818939,21.061754],
* [105.890007,21.061754],
* [105.890007,21.004714],
* [105.818939,21.004714]
* ]]);
*
* var centroidPt = turf.centroid(poly);
*
* var result = turf.featurecollection([poly, centroidPt]);
*
* //=result
*/
module.exports = function(features){
var vertices = explode(features).features,
xSum = 0,
ySum = 0,
len = vertices.length;
for (var i = 0; i < len; i++) {
xSum += vertices[i].geometry.coordinates[0];
ySum += vertices[i].geometry.coordinates[1];
}
return point(xSum / len, ySum / len);
}
var xSum = 0, ySum = 0, len = 0;
each(features, function(coord) {
xSum += coord[0];
ySum += coord[1];
len++;
});
return point([xSum / len, ySum / len]);
};
{
"name": "turf-centroid",
"version": "1.0.1",
"version": "1.0.2",
"description": "turf centroid module",
"main": "index.js",
"scripts": {
"test": "node test.js"
"test": "node test.js",
"doc": "dox -r < index.js | doxme --readme > README.md"
},
"repository": {
"type": "git",
"url": "https://github.com/morganherlocker/turf-centroid.git"
"url": "https://github.com/Turfjs/turf-centroid.git"
},

@@ -22,14 +23,15 @@ "keywords": [

"bugs": {
"url": "https://github.com/morganherlocker/turf-centroid/issues"
"url": "https://github.com/Turfjs/turf-centroid/issues"
},
"homepage": "https://github.com/morganherlocker/turf-centroid",
"homepage": "https://github.com/Turfjs/turf-centroid",
"devDependencies": {
"benchmark": "^1.0.0",
"tape": "^3.0.3"
"tape": "^3.0.3",
"dox": "^0.6.1",
"doxme": "^1.4.2"
},
"dependencies": {
"simple-statistics": "^0.9.0",
"turf-explode": "^1.0.0",
"turf-point": "^1.2.0"
"turf-meta": "^1.0.1",
"turf-point": "^2.0.0"
}
}

@@ -1,36 +0,53 @@

turf-centroid
=============
[![Build Status](https://travis-ci.org/Turfjs/turf-centroid.svg)](https://travis-ci.org/Turfjs/turf-centroid)
# turf-centroid
Calculates the centroid of a polygon Feature or FeatureCollection using the geometric mean of all vertices. This lessens the effect of small islands and artifacts when calculating the centroid of a set of polygons.
[![build status](https://secure.travis-ci.org/Turfjs/turf-centroid.png)](http://travis-ci.org/Turfjs/turf-centroid)
###Install
turf centroid module
```sh
npm install turf-centroid
```
###Parameters
### `turf.centroid(fc)`
|name|description|
|---|---|
|fc|A Feature or FeatureCollection of any type|
Takes a Feature or FeatureCollection of any type and calculates the centroid using the arithmetic mean of all vertices.
This lessens the effect of small islands and artifacts when calculating
the centroid of a set of polygons.
###Usage
### Parameters
| parameter | type | description |
| --------- | ----------------- | ------------------------------------------ |
| `fc` | FeatureCollection | a Feature or FeatureCollection of any type |
### Example
```js
centroid(fc)
var poly = turf.polygon([[
[105.818939,21.004714],
[105.818939,21.061754],
[105.890007,21.061754],
[105.890007,21.004714],
[105.818939,21.004714]
]]);
var centroidPt = turf.centroid(poly);
var result = turf.featurecollection([poly, centroidPt]);
//=result
```
###Example
## Installation
```javascript
var centroid = require('turf-centroid')
var polygon = require('turf-polygon')
Requires [nodejs](http://nodejs.org/).
var poly = polygon([[[0,0], [0,10], [10,10] , [10,0]]])
```sh
$ npm install turf-centroid
```
var centroidPt = centroid(poly)
## Tests
console.log(centroidPt) // a point at 5, 5
```
```sh
$ npm test
```

@@ -9,5 +9,5 @@ var test = require('tape');

t.ok(centered, 'should return the proper center for a FeatureCollection');
t.deepEqual(centered.geometry.coordinates, [84.4, 0.4]);
t.deepEqual(centered.geometry.coordinates, [96.3, -0.25]);
t.end();
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc