index.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // index.ts
  2. import { centerMean } from "@turf/center-mean";
  3. import { distance } from "@turf/distance";
  4. import { centroid } from "@turf/centroid";
  5. import { isNumber, point, isObject, featureCollection } from "@turf/helpers";
  6. import { featureEach } from "@turf/meta";
  7. function centerMedian(features, options = {}) {
  8. options = options || {};
  9. if (!isObject(options)) throw new Error("options is invalid");
  10. var counter = options.counter || 10;
  11. if (!isNumber(counter)) throw new Error("counter must be a number");
  12. var weightTerm = options.weight;
  13. var meanCenter = centerMean(features, { weight: options.weight });
  14. var centroids = featureCollection([]);
  15. featureEach(features, function(feature) {
  16. var _a;
  17. centroids.features.push(
  18. centroid(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. featureEach(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 (!isNumber(weight)) throw new Error("weight value must be a number");
  47. if (weight > 0) {
  48. centroidCount += 1;
  49. var distanceFromCandidate = weight * distance(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 point([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. export {
  77. centerMedian,
  78. turf_center_median_default as default
  79. };
  80. //# sourceMappingURL=index.js.map