index.js 1.1 KB

1234567891011121314151617181920212223242526272829
  1. // index.ts
  2. import { clone } from "@turf/clone";
  3. import { coordAll, featureEach } from "@turf/meta";
  4. import skmeans from "skmeans";
  5. function clustersKmeans(points, options = {}) {
  6. var count = points.features.length;
  7. options.numberOfClusters = options.numberOfClusters || Math.round(Math.sqrt(count / 2));
  8. if (options.numberOfClusters > count) options.numberOfClusters = count;
  9. if (options.mutate !== true) points = clone(points);
  10. var data = coordAll(points);
  11. var initialCentroids = data.slice(0, options.numberOfClusters);
  12. var skmeansResult = skmeans(data, options.numberOfClusters, initialCentroids);
  13. var centroids = {};
  14. skmeansResult.centroids.forEach(function(coord, idx) {
  15. centroids[idx] = coord;
  16. });
  17. featureEach(points, function(point, index) {
  18. var clusterId = skmeansResult.idxs[index];
  19. point.properties.cluster = clusterId;
  20. point.properties.centroid = centroids[clusterId];
  21. });
  22. return points;
  23. }
  24. var turf_clusters_kmeans_default = clustersKmeans;
  25. export {
  26. clustersKmeans,
  27. turf_clusters_kmeans_default as default
  28. };
  29. //# sourceMappingURL=index.js.map