index.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 a {@link Feature} or {@link FeatureCollection} and returns the mean center. Can be weighted.
  7. *
  8. * @name centerMean
  9. * @param {GeoJSON} geojson GeoJSON to be centered
  10. * @param {Object} [options={}] Optional parameters
  11. * @param {Object} [options.properties={}] Translate GeoJSON Properties to Point
  12. * @param {Object} [options.bbox={}] Translate GeoJSON BBox to Point
  13. * @param {Object} [options.id={}] Translate GeoJSON Id to Point
  14. * @param {string} [options.weight] the property name used to weight the center
  15. * @returns {Feature<Point>} a Point feature at the mean center point of all input features
  16. * @example
  17. * var features = turf.featureCollection([
  18. * turf.point([-97.522259, 35.4691], {value: 10}),
  19. * turf.point([-97.502754, 35.463455], {value: 3}),
  20. * turf.point([-97.508269, 35.463245], {value: 5})
  21. * ]);
  22. *
  23. * var options = {weight: "value"}
  24. * var mean = turf.centerMean(features, options);
  25. *
  26. * //addToMap
  27. * var addToMap = [features, mean]
  28. * mean.properties['marker-size'] = 'large';
  29. * mean.properties['marker-color'] = '#000';
  30. */
  31. function centerMean(geojson, // To-Do include Typescript AllGeoJSON
  32. options) {
  33. if (options === void 0) { options = {}; }
  34. var sumXs = 0;
  35. var sumYs = 0;
  36. var sumNs = 0;
  37. meta_1.geomEach(geojson, function (geom, featureIndex, properties) {
  38. var weight = options.weight ? properties === null || properties === void 0 ? void 0 : properties[options.weight] : undefined;
  39. weight = weight === undefined || weight === null ? 1 : weight;
  40. if (!helpers_1.isNumber(weight))
  41. throw new Error("weight value must be a number for feature index " + featureIndex);
  42. weight = Number(weight);
  43. if (weight > 0) {
  44. meta_1.coordEach(geom, function (coord) {
  45. sumXs += coord[0] * weight;
  46. sumYs += coord[1] * weight;
  47. sumNs += weight;
  48. });
  49. }
  50. });
  51. return helpers_1.point([sumXs / sumNs, sumYs / sumNs], options.properties, options);
  52. }
  53. exports.default = centerMean;