index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 centroid_1 = __importDefault(require("@turf/centroid"));
  7. var invariant_1 = require("@turf/invariant");
  8. var meta_1 = require("@turf/meta");
  9. /**
  10. * calcualte the Minkowski p-norm distance between two features.
  11. * @param feature1 point feature
  12. * @param feature2 point feature
  13. * @param p p-norm 1=<p<=infinity 1: Manhattan distance 2: Euclidean distance
  14. */
  15. function pNormDistance(feature1, feature2, p) {
  16. if (p === void 0) { p = 2; }
  17. var coordinate1 = invariant_1.getCoord(feature1);
  18. var coordinate2 = invariant_1.getCoord(feature2);
  19. var xDiff = coordinate1[0] - coordinate2[0];
  20. var yDiff = coordinate1[1] - coordinate2[1];
  21. if (p === 1) {
  22. return Math.abs(xDiff) + Math.abs(yDiff);
  23. }
  24. return Math.pow(Math.pow(xDiff, p) + Math.pow(yDiff, p), 1 / p);
  25. }
  26. exports.pNormDistance = pNormDistance;
  27. /**
  28. *
  29. *
  30. * @name distanceWeight
  31. * @param {FeatureCollection<any>} fc FeatureCollection.
  32. * @param {Object} [options] option object.
  33. * @param {number} [options.threshold=10000] If the distance between neighbor and
  34. * target features is greater than threshold, the weight of that neighbor is 0.
  35. * @param {number} [options.p=2] Minkowski p-norm distance parameter.
  36. * 1: Manhattan distance. 2: Euclidean distance. 1=<p<=infinity.
  37. * @param {boolean} [options.binary=false] If true, weight=1 if d <= threshold otherwise weight=0.
  38. * If false, weight=Math.pow(d, alpha).
  39. * @param {number} [options.alpha=-1] distance decay parameter.
  40. * A big value means the weight decay quickly as distance increases.
  41. * @param {boolean} [options.standardization=false] row standardization.
  42. * @returns {Array<Array<number>>} distance weight matrix.
  43. * @example
  44. *
  45. * var bbox = [-65, 40, -63, 42];
  46. * var dataset = turf.randomPoint(100, { bbox: bbox });
  47. * var result = turf.distanceWeight(dataset);
  48. */
  49. function distanceWeight(fc, options) {
  50. options = options || {};
  51. var threshold = options.threshold || 10000;
  52. var p = options.p || 2;
  53. var binary = options.binary || false;
  54. var alpha = options.alpha || -1;
  55. var rowTransform = options.standardization || false;
  56. var features = [];
  57. meta_1.featureEach(fc, function (feature) {
  58. features.push(centroid_1.default(feature));
  59. });
  60. // computing the distance between the features
  61. var weights = [];
  62. for (var i = 0; i < features.length; i++) {
  63. weights[i] = [];
  64. }
  65. for (var i = 0; i < features.length; i++) {
  66. for (var j = i; j < features.length; j++) {
  67. if (i === j) {
  68. weights[i][j] = 0;
  69. }
  70. var dis = pNormDistance(features[i], features[j], p);
  71. weights[i][j] = dis;
  72. weights[j][i] = dis;
  73. }
  74. }
  75. // binary or distance decay
  76. for (var i = 0; i < features.length; i++) {
  77. for (var j = 0; j < features.length; j++) {
  78. var dis = weights[i][j];
  79. if (dis === 0) {
  80. continue;
  81. }
  82. if (binary) {
  83. if (dis <= threshold) {
  84. weights[i][j] = 1.0;
  85. }
  86. else {
  87. weights[i][j] = 0.0;
  88. }
  89. }
  90. else {
  91. if (dis <= threshold) {
  92. weights[i][j] = Math.pow(dis, alpha);
  93. }
  94. else {
  95. weights[i][j] = 0.0;
  96. }
  97. }
  98. }
  99. }
  100. if (rowTransform) {
  101. for (var i = 0; i < features.length; i++) {
  102. var rowSum = weights[i].reduce(function (sum, currentVal) {
  103. return sum + currentVal;
  104. }, 0);
  105. for (var j = 0; j < features.length; j++) {
  106. weights[i][j] = weights[i][j] / rowSum;
  107. }
  108. }
  109. }
  110. return weights;
  111. }
  112. exports.default = distanceWeight;