-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
46 lines (45 loc) · 1.16 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
var each = require('turf-meta').coordEach;
var point = require('turf-point');
/**
* Takes one or more features 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
* @category measurement
* @param {(Feature|FeatureCollection)} features input features
* @return {Feature<Point>} the centroid of the input features
* @example
* var poly = {
* "type": "Feature",
* "properties": {},
* "geometry": {
* "type": "Polygon",
* "coordinates": [[
* [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 = {
* "type": "FeatureCollection",
* "features": [poly, centroidPt]
* };
*
* //=result
*/
module.exports = function(features) {
var xSum = 0, ySum = 0, len = 0;
each(features, function(coord) {
xSum += coord[0];
ySum += coord[1];
len++;
}, true);
return point([xSum / len, ySum / len]);
};