index.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var meta_1 = require("@turf/meta");
  4. var helpers_1 = require("@turf/helpers");
  5. /**
  6. * Takes one or more features and calculates the centroid using the mean of all vertices.
  7. * This lessens the effect of small islands and artifacts when calculating the centroid of a set of polygons.
  8. *
  9. * @name centroid
  10. * @param {GeoJSON} geojson GeoJSON to be centered
  11. * @param {Object} [options={}] Optional Parameters
  12. * @param {Object} [options.properties={}] an Object that is used as the {@link Feature}'s properties
  13. * @returns {Feature<Point>} the centroid of the input features
  14. * @example
  15. * var polygon = turf.polygon([[[-81, 41], [-88, 36], [-84, 31], [-80, 33], [-77, 39], [-81, 41]]]);
  16. *
  17. * var centroid = turf.centroid(polygon);
  18. *
  19. * //addToMap
  20. * var addToMap = [polygon, centroid]
  21. */
  22. function centroid(geojson, options) {
  23. if (options === void 0) { options = {}; }
  24. var xSum = 0;
  25. var ySum = 0;
  26. var len = 0;
  27. meta_1.coordEach(geojson, function (coord) {
  28. xSum += coord[0];
  29. ySum += coord[1];
  30. len++;
  31. }, true);
  32. return helpers_1.point([xSum / len, ySum / len], options.properties);
  33. }
  34. exports.default = centroid;