index.js 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. var convex_1 = __importDefault(require("@turf/convex"));
  7. var centroid_1 = __importDefault(require("@turf/centroid"));
  8. var helpers_1 = require("@turf/helpers");
  9. var invariant_1 = require("@turf/invariant");
  10. var meta_1 = require("@turf/meta");
  11. /**
  12. * Takes any {@link Feature} or a {@link FeatureCollection} and returns its [center of mass](https://en.wikipedia.org/wiki/Center_of_mass) using this formula: [Centroid of Polygon](https://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon).
  13. *
  14. * @name centerOfMass
  15. * @param {GeoJSON} geojson GeoJSON to be centered
  16. * @param {Object} [options={}] Optional Parameters
  17. * @param {Object} [options.properties={}] Translate Properties to Feature
  18. * @returns {Feature<Point>} the center of mass
  19. * @example
  20. * var polygon = turf.polygon([[[-81, 41], [-88, 36], [-84, 31], [-80, 33], [-77, 39], [-81, 41]]]);
  21. *
  22. * var center = turf.centerOfMass(polygon);
  23. *
  24. * //addToMap
  25. * var addToMap = [polygon, center]
  26. */
  27. function centerOfMass(geojson, options) {
  28. if (options === void 0) { options = {}; }
  29. switch (invariant_1.getType(geojson)) {
  30. case "Point":
  31. return helpers_1.point(invariant_1.getCoord(geojson), options.properties);
  32. case "Polygon":
  33. var coords = [];
  34. meta_1.coordEach(geojson, function (coord) {
  35. coords.push(coord);
  36. });
  37. // First, we neutralize the feature (set it around coordinates [0,0]) to prevent rounding errors
  38. // We take any point to translate all the points around 0
  39. var centre = centroid_1.default(geojson, { properties: options.properties });
  40. var translation = centre.geometry.coordinates;
  41. var sx = 0;
  42. var sy = 0;
  43. var sArea = 0;
  44. var i, pi, pj, xi, xj, yi, yj, a;
  45. var neutralizedPoints = coords.map(function (point) {
  46. return [point[0] - translation[0], point[1] - translation[1]];
  47. });
  48. for (i = 0; i < coords.length - 1; i++) {
  49. // pi is the current point
  50. pi = neutralizedPoints[i];
  51. xi = pi[0];
  52. yi = pi[1];
  53. // pj is the next point (pi+1)
  54. pj = neutralizedPoints[i + 1];
  55. xj = pj[0];
  56. yj = pj[1];
  57. // a is the common factor to compute the signed area and the final coordinates
  58. a = xi * yj - xj * yi;
  59. // sArea is the sum used to compute the signed area
  60. sArea += a;
  61. // sx and sy are the sums used to compute the final coordinates
  62. sx += (xi + xj) * a;
  63. sy += (yi + yj) * a;
  64. }
  65. // Shape has no area: fallback on turf.centroid
  66. if (sArea === 0) {
  67. return centre;
  68. }
  69. else {
  70. // Compute the signed area, and factorize 1/6A
  71. var area = sArea * 0.5;
  72. var areaFactor = 1 / (6 * area);
  73. // Compute the final coordinates, adding back the values that have been neutralized
  74. return helpers_1.point([translation[0] + areaFactor * sx, translation[1] + areaFactor * sy], options.properties);
  75. }
  76. default:
  77. // Not a polygon: Compute the convex hull and work with that
  78. var hull = convex_1.default(geojson);
  79. if (hull)
  80. return centerOfMass(hull, { properties: options.properties });
  81. // Hull is empty: fallback on the centroid
  82. else
  83. return centroid_1.default(geojson, { properties: options.properties });
  84. }
  85. }
  86. exports.default = centerOfMass;