index.cjs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. "use strict";Object.defineProperty(exports, "__esModule", {value: true});// index.ts
  2. var _centermean = require('@turf/center-mean');
  3. var _distance = require('@turf/distance');
  4. var _centroid = require('@turf/centroid');
  5. var _helpers = require('@turf/helpers');
  6. var _meta = require('@turf/meta');
  7. function centerMedian(features, options = {}) {
  8. options = options || {};
  9. if (!_helpers.isObject.call(void 0, options)) throw new Error("options is invalid");
  10. var counter = options.counter || 10;
  11. if (!_helpers.isNumber.call(void 0, counter)) throw new Error("counter must be a number");
  12. var weightTerm = options.weight;
  13. var meanCenter = _centermean.centerMean.call(void 0, features, { weight: options.weight });
  14. var centroids = _helpers.featureCollection.call(void 0, []);
  15. _meta.featureEach.call(void 0, features, function(feature) {
  16. var _a;
  17. centroids.features.push(
  18. _centroid.centroid.call(void 0, feature, {
  19. properties: { weight: (_a = feature.properties) == null ? void 0 : _a[weightTerm] }
  20. })
  21. );
  22. });
  23. const properties = {
  24. tolerance: options.tolerance,
  25. medianCandidates: []
  26. };
  27. return findMedian(
  28. meanCenter.geometry.coordinates,
  29. [0, 0],
  30. centroids,
  31. properties,
  32. counter
  33. );
  34. }
  35. function findMedian(candidateMedian, previousCandidate, centroids, properties, counter) {
  36. var tolerance = properties.tolerance || 1e-3;
  37. var candidateXsum = 0;
  38. var candidateYsum = 0;
  39. var kSum = 0;
  40. var centroidCount = 0;
  41. _meta.featureEach.call(void 0, centroids, function(theCentroid) {
  42. var _a;
  43. var weightValue = (_a = theCentroid.properties) == null ? void 0 : _a.weight;
  44. var weight = weightValue === void 0 || weightValue === null ? 1 : weightValue;
  45. weight = Number(weight);
  46. if (!_helpers.isNumber.call(void 0, weight)) throw new Error("weight value must be a number");
  47. if (weight > 0) {
  48. centroidCount += 1;
  49. var distanceFromCandidate = weight * _distance.distance.call(void 0, theCentroid, candidateMedian);
  50. if (distanceFromCandidate === 0) distanceFromCandidate = 1;
  51. var k = weight / distanceFromCandidate;
  52. candidateXsum += theCentroid.geometry.coordinates[0] * k;
  53. candidateYsum += theCentroid.geometry.coordinates[1] * k;
  54. kSum += k;
  55. }
  56. });
  57. if (centroidCount < 1) throw new Error("no features to measure");
  58. var candidateX = candidateXsum / kSum;
  59. var candidateY = candidateYsum / kSum;
  60. if (centroidCount === 1 || counter === 0 || Math.abs(candidateX - previousCandidate[0]) < tolerance && Math.abs(candidateY - previousCandidate[1]) < tolerance) {
  61. return _helpers.point.call(void 0, [candidateX, candidateY], {
  62. medianCandidates: properties.medianCandidates
  63. });
  64. } else {
  65. properties.medianCandidates.push([candidateX, candidateY]);
  66. return findMedian(
  67. [candidateX, candidateY],
  68. candidateMedian,
  69. centroids,
  70. properties,
  71. counter - 1
  72. );
  73. }
  74. }
  75. var turf_center_median_default = centerMedian;
  76. exports.centerMedian = centerMedian; exports.default = turf_center_median_default;
  77. //# sourceMappingURL=index.cjs.map