index.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 clone_1 = __importDefault(require("@turf/clone"));
  7. var meta_1 = require("@turf/meta");
  8. var skmeans_1 = __importDefault(require("skmeans"));
  9. /**
  10. * Takes a set of {@link Point|points} and partition them into clusters using the k-mean .
  11. * It uses the [k-means algorithm](https://en.wikipedia.org/wiki/K-means_clustering)
  12. *
  13. * @name clustersKmeans
  14. * @param {FeatureCollection<Point>} points to be clustered
  15. * @param {Object} [options={}] Optional parameters
  16. * @param {number} [options.numberOfClusters=Math.sqrt(numberOfPoints/2)] numberOfClusters that will be generated
  17. * @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)
  18. * @returns {FeatureCollection<Point>} Clustered Points with an additional two properties associated to each Feature:
  19. * - {number} cluster - the associated clusterId
  20. * - {[number, number]} centroid - Centroid of the cluster [Longitude, Latitude]
  21. * @example
  22. * // create random points with random z-values in their properties
  23. * var points = turf.randomPoint(100, {bbox: [0, 30, 20, 50]});
  24. * var options = {numberOfClusters: 7};
  25. * var clustered = turf.clustersKmeans(points, options);
  26. *
  27. * //addToMap
  28. * var addToMap = [clustered];
  29. */
  30. function clustersKmeans(points, options) {
  31. if (options === void 0) { options = {}; }
  32. // Default Params
  33. var count = points.features.length;
  34. options.numberOfClusters =
  35. options.numberOfClusters || Math.round(Math.sqrt(count / 2));
  36. // numberOfClusters can't be greater than the number of points
  37. // fallbacks to count
  38. if (options.numberOfClusters > count)
  39. options.numberOfClusters = count;
  40. // Clone points to prevent any mutations (enabled by default)
  41. if (options.mutate !== true)
  42. points = clone_1.default(points);
  43. // collect points coordinates
  44. var data = meta_1.coordAll(points);
  45. // create seed to avoid skmeans to drift
  46. var initialCentroids = data.slice(0, options.numberOfClusters);
  47. // create skmeans clusters
  48. var skmeansResult = skmeans_1.default(data, options.numberOfClusters, initialCentroids);
  49. // store centroids {clusterId: [number, number]}
  50. var centroids = {};
  51. skmeansResult.centroids.forEach(function (coord, idx) {
  52. centroids[idx] = coord;
  53. });
  54. // add associated cluster number
  55. meta_1.featureEach(points, function (point, index) {
  56. var clusterId = skmeansResult.idxs[index];
  57. point.properties.cluster = clusterId;
  58. point.properties.centroid = centroids[clusterId];
  59. });
  60. return points;
  61. }
  62. exports.default = clustersKmeans;