index.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import centerMean from "@turf/center-mean";
  2. import distance from "@turf/distance";
  3. import centroid from "@turf/centroid";
  4. import { isNumber, point, isObject, featureCollection, } from "@turf/helpers";
  5. import { featureEach } from "@turf/meta";
  6. /**
  7. * Takes a {@link FeatureCollection} of points and calculates the median center,
  8. * algorithimically. The median center is understood as the point that is
  9. * requires the least total travel from all other points.
  10. *
  11. * Turfjs has four different functions for calculating the center of a set of
  12. * data. Each is useful depending on circumstance.
  13. *
  14. * `@turf/center` finds the simple center of a dataset, by finding the
  15. * midpoint between the extents of the data. That is, it divides in half the
  16. * farthest east and farthest west point as well as the farthest north and
  17. * farthest south.
  18. *
  19. * `@turf/center-of-mass` imagines that the dataset is a sheet of paper.
  20. * The center of mass is where the sheet would balance on a fingertip.
  21. *
  22. * `@turf/center-mean` takes the averages of all the coordinates and
  23. * produces a value that respects that. Unlike `@turf/center`, it is
  24. * sensitive to clusters and outliers. It lands in the statistical middle of a
  25. * dataset, not the geographical. It can also be weighted, meaning certain
  26. * points are more important than others.
  27. *
  28. * `@turf/center-median` takes the mean center and tries to find, iteratively,
  29. * a new point that requires the least amount of travel from all the points in
  30. * the dataset. It is not as sensitive to outliers as `@turf/center-mean`, but it is
  31. * attracted to clustered data. It, too, can be weighted.
  32. *
  33. * **Bibliography**
  34. *
  35. * Harold W. Kuhn and Robert E. Kuenne, “An Efficient Algorithm for the
  36. * Numerical Solution of the Generalized Weber Problem in Spatial
  37. * Economics,” _Journal of Regional Science_ 4, no. 2 (1962): 21–33,
  38. * doi:{@link https://doi.org/10.1111/j.1467-9787.1962.tb00902.x}.
  39. *
  40. * James E. Burt, Gerald M. Barber, and David L. Rigby, _Elementary
  41. * Statistics for Geographers_, 3rd ed., New York: The Guilford
  42. * Press, 2009, 150–151.
  43. *
  44. * @name centerMedian
  45. * @param {FeatureCollection<any>} features Any GeoJSON Feature Collection
  46. * @param {Object} [options={}] Optional parameters
  47. * @param {string} [options.weight] the property name used to weight the center
  48. * @param {number} [options.tolerance=0.001] the difference in distance between candidate medians at which point the algorighim stops iterating.
  49. * @param {number} [options.counter=10] how many attempts to find the median, should the tolerance be insufficient.
  50. * @returns {Feature<Point>} The median center of the collection
  51. * @example
  52. * var points = turf.points([[0, 0], [1, 0], [0, 1], [5, 8]]);
  53. * var medianCenter = turf.centerMedian(points);
  54. *
  55. * //addToMap
  56. * var addToMap = [points, medianCenter]
  57. */
  58. function centerMedian(features, options) {
  59. if (options === void 0) { options = {}; }
  60. // Optional params
  61. options = options || {};
  62. if (!isObject(options))
  63. throw new Error("options is invalid");
  64. var counter = options.counter || 10;
  65. if (!isNumber(counter))
  66. throw new Error("counter must be a number");
  67. var weightTerm = options.weight;
  68. // Calculate mean center:
  69. var meanCenter = centerMean(features, { weight: options.weight });
  70. // Calculate center of every feature:
  71. var centroids = featureCollection([]);
  72. featureEach(features, function (feature) {
  73. var _a;
  74. centroids.features.push(centroid(feature, {
  75. properties: { weight: (_a = feature.properties) === null || _a === void 0 ? void 0 : _a[weightTerm] },
  76. }));
  77. });
  78. var properties = {
  79. tolerance: options.tolerance,
  80. medianCandidates: [],
  81. };
  82. return findMedian(meanCenter.geometry.coordinates, [0, 0], centroids, properties, counter);
  83. }
  84. /**
  85. * Recursive function to find new candidate medians.
  86. *
  87. * @private
  88. * @param {Position} candidateMedian current candidate median
  89. * @param {Position} previousCandidate the previous candidate median
  90. * @param {FeatureCollection<Point>} centroids the collection of centroids whose median we are determining
  91. * @param {number} counter how many attempts to try before quitting.
  92. * @returns {Feature<Point>} the median center of the dataset.
  93. */
  94. function findMedian(candidateMedian, previousCandidate, centroids, properties, counter) {
  95. var tolerance = properties.tolerance || 0.001;
  96. var candidateXsum = 0;
  97. var candidateYsum = 0;
  98. var kSum = 0;
  99. var centroidCount = 0;
  100. featureEach(centroids, function (theCentroid) {
  101. var _a;
  102. var weightValue = (_a = theCentroid.properties) === null || _a === void 0 ? void 0 : _a.weight;
  103. var weight = weightValue === undefined || weightValue === null ? 1 : weightValue;
  104. weight = Number(weight);
  105. if (!isNumber(weight))
  106. throw new Error("weight value must be a number");
  107. if (weight > 0) {
  108. centroidCount += 1;
  109. var distanceFromCandidate = weight * distance(theCentroid, candidateMedian);
  110. if (distanceFromCandidate === 0)
  111. distanceFromCandidate = 1;
  112. var k = weight / distanceFromCandidate;
  113. candidateXsum += theCentroid.geometry.coordinates[0] * k;
  114. candidateYsum += theCentroid.geometry.coordinates[1] * k;
  115. kSum += k;
  116. }
  117. });
  118. if (centroidCount < 1)
  119. throw new Error("no features to measure");
  120. var candidateX = candidateXsum / kSum;
  121. var candidateY = candidateYsum / kSum;
  122. if (centroidCount === 1 ||
  123. counter === 0 ||
  124. (Math.abs(candidateX - previousCandidate[0]) < tolerance &&
  125. Math.abs(candidateY - previousCandidate[1]) < tolerance)) {
  126. return point([candidateX, candidateY], {
  127. medianCandidates: properties.medianCandidates,
  128. });
  129. }
  130. else {
  131. properties.medianCandidates.push([candidateX, candidateY]);
  132. return findMedian([candidateX, candidateY], candidateMedian, centroids, properties, counter - 1);
  133. }
  134. }
  135. export default centerMedian;