@turf/clusters-kmeans 7.0.0-alpha.1 → 7.0.0-alpha.111
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -9
- package/dist/cjs/index.cjs +35 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/{js → cjs}/index.d.ts +5 -3
- package/dist/esm/index.d.mts +33 -0
- package/dist/esm/index.mjs +35 -0
- package/dist/esm/index.mjs.map +1 -0
- package/package.json +39 -35
- package/dist/es/index.js +0 -56
- package/dist/es/package.json +0 -1
- package/dist/js/index.js +0 -59
package/README.md
CHANGED
|
@@ -44,26 +44,21 @@ Returns **[FeatureCollection][3]<[Point][4]>** Clustered Points with an addition
|
|
|
44
44
|
|
|
45
45
|
[7]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
|
|
46
46
|
|
|
47
|
-
<!-- This file is automatically generated. Please don't edit it directly
|
|
48
|
-
if you find an error, edit the source file (likely index.js), and re-run
|
|
49
|
-
./scripts/generate-readmes in the turf project. -->
|
|
47
|
+
<!-- This file is automatically generated. Please don't edit it directly. If you find an error, edit the source file of the module in question (likely index.js or index.ts), and re-run "yarn docs" from the root of the turf project. -->
|
|
50
48
|
|
|
51
49
|
---
|
|
52
50
|
|
|
53
|
-
This module is part of the [Turfjs project](
|
|
54
|
-
module collection dedicated to geographic algorithms. It is maintained in the
|
|
55
|
-
[Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create
|
|
56
|
-
PRs and issues.
|
|
51
|
+
This module is part of the [Turfjs project](https://turfjs.org/), an open source module collection dedicated to geographic algorithms. It is maintained in the [Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create PRs and issues.
|
|
57
52
|
|
|
58
53
|
### Installation
|
|
59
54
|
|
|
60
|
-
Install this module individually:
|
|
55
|
+
Install this single module individually:
|
|
61
56
|
|
|
62
57
|
```sh
|
|
63
58
|
$ npm install @turf/clusters-kmeans
|
|
64
59
|
```
|
|
65
60
|
|
|
66
|
-
Or install the
|
|
61
|
+
Or install the all-encompassing @turf/turf module that includes all modules as functions:
|
|
67
62
|
|
|
68
63
|
```sh
|
|
69
64
|
$ npm install @turf/turf
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// index.ts
|
|
5
|
+
var _clone = require('@turf/clone');
|
|
6
|
+
var _meta = require('@turf/meta');
|
|
7
|
+
var _skmeans = require('skmeans'); var _skmeans2 = _interopRequireDefault(_skmeans);
|
|
8
|
+
function clustersKmeans(points, options = {}) {
|
|
9
|
+
var count = points.features.length;
|
|
10
|
+
options.numberOfClusters = options.numberOfClusters || Math.round(Math.sqrt(count / 2));
|
|
11
|
+
if (options.numberOfClusters > count)
|
|
12
|
+
options.numberOfClusters = count;
|
|
13
|
+
if (options.mutate !== true)
|
|
14
|
+
points = _clone.clone.call(void 0, points);
|
|
15
|
+
var data = _meta.coordAll.call(void 0, points);
|
|
16
|
+
var initialCentroids = data.slice(0, options.numberOfClusters);
|
|
17
|
+
var skmeansResult = _skmeans2.default.call(void 0, data, options.numberOfClusters, initialCentroids);
|
|
18
|
+
var centroids = {};
|
|
19
|
+
skmeansResult.centroids.forEach(function(coord, idx) {
|
|
20
|
+
centroids[idx] = coord;
|
|
21
|
+
});
|
|
22
|
+
_meta.featureEach.call(void 0, points, function(point, index) {
|
|
23
|
+
var clusterId = skmeansResult.idxs[index];
|
|
24
|
+
point.properties.cluster = clusterId;
|
|
25
|
+
point.properties.centroid = centroids[clusterId];
|
|
26
|
+
});
|
|
27
|
+
return points;
|
|
28
|
+
}
|
|
29
|
+
__name(clustersKmeans, "clustersKmeans");
|
|
30
|
+
var turf_clusters_kmeans_default = clustersKmeans;
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
exports.clustersKmeans = clustersKmeans; exports.default = turf_clusters_kmeans_default;
|
|
35
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../index.ts"],"names":[],"mappings":";;;;AACA,SAAS,aAAa;AACtB,SAAS,UAAU,mBAAmB;AACtC,OAAO,aAAa;AA4BpB,SAAS,eACP,QACA,UAGI,CAAC,GACkC;AAEvC,MAAI,QAAQ,OAAO,SAAS;AAC5B,UAAQ,mBACN,QAAQ,oBAAoB,KAAK,MAAM,KAAK,KAAK,QAAQ,CAAC,CAAC;AAI7D,MAAI,QAAQ,mBAAmB;AAAO,YAAQ,mBAAmB;AAGjE,MAAI,QAAQ,WAAW;AAAM,aAAS,MAAM,MAAM;AAGlD,MAAI,OAAO,SAAS,MAAM;AAG1B,MAAI,mBAAmB,KAAK,MAAM,GAAG,QAAQ,gBAAgB;AAG7D,MAAI,gBAAgB,QAAQ,MAAM,QAAQ,kBAAkB,gBAAgB;AAG5E,MAAI,YAAsC,CAAC;AAC3C,EAAC,cAAc,UAAyB,QAAQ,SAC9C,OACA,KACA;AACA,cAAU,GAAG,IAAI;AAAA,EACnB,CAAC;AAGD,cAAY,QAAQ,SAAU,OAAO,OAAO;AAC1C,QAAI,YAAY,cAAc,KAAK,KAAK;AACxC,UAAM,WAAY,UAAU;AAC5B,UAAM,WAAY,WAAW,UAAU,SAAS;AAAA,EAClD,CAAC;AAED,SAAO;AACT;AA7CS;AAgDT,IAAO,+BAAQ","sourcesContent":["import { FeatureCollection, Point, GeoJsonProperties } from \"geojson\";\nimport { clone } from \"@turf/clone\";\nimport { coordAll, featureEach } from \"@turf/meta\";\nimport skmeans from \"skmeans\";\n\ntype KmeansProps = GeoJsonProperties & {\n cluster?: number;\n centroid?: [number, number];\n};\n\n/**\n * Takes a set of {@link Point|points} and partition them into clusters using the k-mean .\n * It uses the [k-means algorithm](https://en.wikipedia.org/wiki/K-means_clustering)\n *\n * @name clustersKmeans\n * @param {FeatureCollection<Point>} points to be clustered\n * @param {Object} [options={}] Optional parameters\n * @param {number} [options.numberOfClusters=Math.sqrt(numberOfPoints/2)] numberOfClusters that will be generated\n * @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)\n * @returns {FeatureCollection<Point>} Clustered Points with an additional two properties associated to each Feature:\n * - {number} cluster - the associated clusterId\n * - {[number, number]} centroid - Centroid of the cluster [Longitude, Latitude]\n * @example\n * // create random points with random z-values in their properties\n * var points = turf.randomPoint(100, {bbox: [0, 30, 20, 50]});\n * var options = {numberOfClusters: 7};\n * var clustered = turf.clustersKmeans(points, options);\n *\n * //addToMap\n * var addToMap = [clustered];\n */\nfunction clustersKmeans(\n points: FeatureCollection<Point>,\n options: {\n numberOfClusters?: number;\n mutate?: boolean;\n } = {}\n): FeatureCollection<Point, KmeansProps> {\n // Default Params\n var count = points.features.length;\n options.numberOfClusters =\n options.numberOfClusters || Math.round(Math.sqrt(count / 2));\n\n // numberOfClusters can't be greater than the number of points\n // fallbacks to count\n if (options.numberOfClusters > count) options.numberOfClusters = count;\n\n // Clone points to prevent any mutations (enabled by default)\n if (options.mutate !== true) points = clone(points);\n\n // collect points coordinates\n var data = coordAll(points);\n\n // create seed to avoid skmeans to drift\n var initialCentroids = data.slice(0, options.numberOfClusters);\n\n // create skmeans clusters\n var skmeansResult = skmeans(data, options.numberOfClusters, initialCentroids);\n\n // store centroids {clusterId: [number, number]}\n var centroids: Record<string, number[]> = {};\n (skmeansResult.centroids as number[][]).forEach(function (\n coord: number[],\n idx: number\n ) {\n centroids[idx] = coord;\n });\n\n // add associated cluster number\n featureEach(points, function (point, index) {\n var clusterId = skmeansResult.idxs[index];\n point.properties!.cluster = clusterId;\n point.properties!.centroid = centroids[clusterId];\n });\n\n return points as FeatureCollection<Point, KmeansProps>;\n}\n\nexport { clustersKmeans, KmeansProps };\nexport default clustersKmeans;\n"]}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { FeatureCollection, Point
|
|
2
|
-
|
|
1
|
+
import { GeoJsonProperties, FeatureCollection, Point } from 'geojson';
|
|
2
|
+
|
|
3
|
+
type KmeansProps = GeoJsonProperties & {
|
|
3
4
|
cluster?: number;
|
|
4
5
|
centroid?: [number, number];
|
|
5
6
|
};
|
|
@@ -28,4 +29,5 @@ declare function clustersKmeans(points: FeatureCollection<Point>, options?: {
|
|
|
28
29
|
numberOfClusters?: number;
|
|
29
30
|
mutate?: boolean;
|
|
30
31
|
}): FeatureCollection<Point, KmeansProps>;
|
|
31
|
-
|
|
32
|
+
|
|
33
|
+
export { type KmeansProps, clustersKmeans, clustersKmeans as default };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { GeoJsonProperties, FeatureCollection, Point } from 'geojson';
|
|
2
|
+
|
|
3
|
+
type KmeansProps = GeoJsonProperties & {
|
|
4
|
+
cluster?: number;
|
|
5
|
+
centroid?: [number, number];
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Takes a set of {@link Point|points} and partition them into clusters using the k-mean .
|
|
9
|
+
* It uses the [k-means algorithm](https://en.wikipedia.org/wiki/K-means_clustering)
|
|
10
|
+
*
|
|
11
|
+
* @name clustersKmeans
|
|
12
|
+
* @param {FeatureCollection<Point>} points to be clustered
|
|
13
|
+
* @param {Object} [options={}] Optional parameters
|
|
14
|
+
* @param {number} [options.numberOfClusters=Math.sqrt(numberOfPoints/2)] numberOfClusters that will be generated
|
|
15
|
+
* @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)
|
|
16
|
+
* @returns {FeatureCollection<Point>} Clustered Points with an additional two properties associated to each Feature:
|
|
17
|
+
* - {number} cluster - the associated clusterId
|
|
18
|
+
* - {[number, number]} centroid - Centroid of the cluster [Longitude, Latitude]
|
|
19
|
+
* @example
|
|
20
|
+
* // create random points with random z-values in their properties
|
|
21
|
+
* var points = turf.randomPoint(100, {bbox: [0, 30, 20, 50]});
|
|
22
|
+
* var options = {numberOfClusters: 7};
|
|
23
|
+
* var clustered = turf.clustersKmeans(points, options);
|
|
24
|
+
*
|
|
25
|
+
* //addToMap
|
|
26
|
+
* var addToMap = [clustered];
|
|
27
|
+
*/
|
|
28
|
+
declare function clustersKmeans(points: FeatureCollection<Point>, options?: {
|
|
29
|
+
numberOfClusters?: number;
|
|
30
|
+
mutate?: boolean;
|
|
31
|
+
}): FeatureCollection<Point, KmeansProps>;
|
|
32
|
+
|
|
33
|
+
export { type KmeansProps, clustersKmeans, clustersKmeans as default };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// index.ts
|
|
5
|
+
import { clone } from "@turf/clone";
|
|
6
|
+
import { coordAll, featureEach } from "@turf/meta";
|
|
7
|
+
import skmeans from "skmeans";
|
|
8
|
+
function clustersKmeans(points, options = {}) {
|
|
9
|
+
var count = points.features.length;
|
|
10
|
+
options.numberOfClusters = options.numberOfClusters || Math.round(Math.sqrt(count / 2));
|
|
11
|
+
if (options.numberOfClusters > count)
|
|
12
|
+
options.numberOfClusters = count;
|
|
13
|
+
if (options.mutate !== true)
|
|
14
|
+
points = clone(points);
|
|
15
|
+
var data = coordAll(points);
|
|
16
|
+
var initialCentroids = data.slice(0, options.numberOfClusters);
|
|
17
|
+
var skmeansResult = skmeans(data, options.numberOfClusters, initialCentroids);
|
|
18
|
+
var centroids = {};
|
|
19
|
+
skmeansResult.centroids.forEach(function(coord, idx) {
|
|
20
|
+
centroids[idx] = coord;
|
|
21
|
+
});
|
|
22
|
+
featureEach(points, function(point, index) {
|
|
23
|
+
var clusterId = skmeansResult.idxs[index];
|
|
24
|
+
point.properties.cluster = clusterId;
|
|
25
|
+
point.properties.centroid = centroids[clusterId];
|
|
26
|
+
});
|
|
27
|
+
return points;
|
|
28
|
+
}
|
|
29
|
+
__name(clustersKmeans, "clustersKmeans");
|
|
30
|
+
var turf_clusters_kmeans_default = clustersKmeans;
|
|
31
|
+
export {
|
|
32
|
+
clustersKmeans,
|
|
33
|
+
turf_clusters_kmeans_default as default
|
|
34
|
+
};
|
|
35
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../index.ts"],"sourcesContent":["import { FeatureCollection, Point, GeoJsonProperties } from \"geojson\";\nimport { clone } from \"@turf/clone\";\nimport { coordAll, featureEach } from \"@turf/meta\";\nimport skmeans from \"skmeans\";\n\ntype KmeansProps = GeoJsonProperties & {\n cluster?: number;\n centroid?: [number, number];\n};\n\n/**\n * Takes a set of {@link Point|points} and partition them into clusters using the k-mean .\n * It uses the [k-means algorithm](https://en.wikipedia.org/wiki/K-means_clustering)\n *\n * @name clustersKmeans\n * @param {FeatureCollection<Point>} points to be clustered\n * @param {Object} [options={}] Optional parameters\n * @param {number} [options.numberOfClusters=Math.sqrt(numberOfPoints/2)] numberOfClusters that will be generated\n * @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)\n * @returns {FeatureCollection<Point>} Clustered Points with an additional two properties associated to each Feature:\n * - {number} cluster - the associated clusterId\n * - {[number, number]} centroid - Centroid of the cluster [Longitude, Latitude]\n * @example\n * // create random points with random z-values in their properties\n * var points = turf.randomPoint(100, {bbox: [0, 30, 20, 50]});\n * var options = {numberOfClusters: 7};\n * var clustered = turf.clustersKmeans(points, options);\n *\n * //addToMap\n * var addToMap = [clustered];\n */\nfunction clustersKmeans(\n points: FeatureCollection<Point>,\n options: {\n numberOfClusters?: number;\n mutate?: boolean;\n } = {}\n): FeatureCollection<Point, KmeansProps> {\n // Default Params\n var count = points.features.length;\n options.numberOfClusters =\n options.numberOfClusters || Math.round(Math.sqrt(count / 2));\n\n // numberOfClusters can't be greater than the number of points\n // fallbacks to count\n if (options.numberOfClusters > count) options.numberOfClusters = count;\n\n // Clone points to prevent any mutations (enabled by default)\n if (options.mutate !== true) points = clone(points);\n\n // collect points coordinates\n var data = coordAll(points);\n\n // create seed to avoid skmeans to drift\n var initialCentroids = data.slice(0, options.numberOfClusters);\n\n // create skmeans clusters\n var skmeansResult = skmeans(data, options.numberOfClusters, initialCentroids);\n\n // store centroids {clusterId: [number, number]}\n var centroids: Record<string, number[]> = {};\n (skmeansResult.centroids as number[][]).forEach(function (\n coord: number[],\n idx: number\n ) {\n centroids[idx] = coord;\n });\n\n // add associated cluster number\n featureEach(points, function (point, index) {\n var clusterId = skmeansResult.idxs[index];\n point.properties!.cluster = clusterId;\n point.properties!.centroid = centroids[clusterId];\n });\n\n return points as FeatureCollection<Point, KmeansProps>;\n}\n\nexport { clustersKmeans, KmeansProps };\nexport default clustersKmeans;\n"],"mappings":";;;;AACA,SAAS,aAAa;AACtB,SAAS,UAAU,mBAAmB;AACtC,OAAO,aAAa;AA4BpB,SAAS,eACP,QACA,UAGI,CAAC,GACkC;AAEvC,MAAI,QAAQ,OAAO,SAAS;AAC5B,UAAQ,mBACN,QAAQ,oBAAoB,KAAK,MAAM,KAAK,KAAK,QAAQ,CAAC,CAAC;AAI7D,MAAI,QAAQ,mBAAmB;AAAO,YAAQ,mBAAmB;AAGjE,MAAI,QAAQ,WAAW;AAAM,aAAS,MAAM,MAAM;AAGlD,MAAI,OAAO,SAAS,MAAM;AAG1B,MAAI,mBAAmB,KAAK,MAAM,GAAG,QAAQ,gBAAgB;AAG7D,MAAI,gBAAgB,QAAQ,MAAM,QAAQ,kBAAkB,gBAAgB;AAG5E,MAAI,YAAsC,CAAC;AAC3C,EAAC,cAAc,UAAyB,QAAQ,SAC9C,OACA,KACA;AACA,cAAU,GAAG,IAAI;AAAA,EACnB,CAAC;AAGD,cAAY,QAAQ,SAAU,OAAO,OAAO;AAC1C,QAAI,YAAY,cAAc,KAAK,KAAK;AACxC,UAAM,WAAY,UAAU;AAC5B,UAAM,WAAY,WAAW,UAAU,SAAS;AAAA,EAClD,CAAC;AAED,SAAO;AACT;AA7CS;AAgDT,IAAO,+BAAQ;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@turf/clusters-kmeans",
|
|
3
|
-
"version": "7.0.0-alpha.
|
|
3
|
+
"version": "7.0.0-alpha.111+08576cb50",
|
|
4
4
|
"description": "turf clusters-kmeans module",
|
|
5
5
|
"author": "Turf Authors",
|
|
6
6
|
"contributors": [
|
|
@@ -29,56 +29,60 @@
|
|
|
29
29
|
"clustering",
|
|
30
30
|
"k-means"
|
|
31
31
|
],
|
|
32
|
-
"
|
|
33
|
-
"
|
|
32
|
+
"type": "commonjs",
|
|
33
|
+
"main": "dist/cjs/index.cjs",
|
|
34
|
+
"module": "dist/esm/index.mjs",
|
|
35
|
+
"types": "dist/cjs/index.d.ts",
|
|
34
36
|
"exports": {
|
|
35
37
|
"./package.json": "./package.json",
|
|
36
38
|
".": {
|
|
37
|
-
"
|
|
38
|
-
|
|
39
|
-
|
|
39
|
+
"import": {
|
|
40
|
+
"types": "./dist/esm/index.d.mts",
|
|
41
|
+
"default": "./dist/esm/index.mjs"
|
|
42
|
+
},
|
|
43
|
+
"require": {
|
|
44
|
+
"types": "./dist/cjs/index.d.ts",
|
|
45
|
+
"default": "./dist/cjs/index.cjs"
|
|
46
|
+
}
|
|
40
47
|
}
|
|
41
48
|
},
|
|
42
|
-
"types": "dist/js/index.d.ts",
|
|
43
49
|
"sideEffects": false,
|
|
44
50
|
"files": [
|
|
45
51
|
"dist"
|
|
46
52
|
],
|
|
47
53
|
"scripts": {
|
|
48
|
-
"bench": "tsx bench.
|
|
49
|
-
"build": "
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
"
|
|
53
|
-
"test": "npm-run-all test:*",
|
|
54
|
-
"test:tape": "tsx test.js",
|
|
54
|
+
"bench": "tsx bench.ts",
|
|
55
|
+
"build": "tsup --config ../../tsup.config.ts",
|
|
56
|
+
"docs": "tsx ../../scripts/generate-readmes.ts",
|
|
57
|
+
"test": "npm-run-all --npm-path npm test:*",
|
|
58
|
+
"test:tape": "tsx test.ts",
|
|
55
59
|
"test:types": "tsc --esModuleInterop --noEmit --strict types.ts"
|
|
56
60
|
},
|
|
57
61
|
"devDependencies": {
|
|
58
|
-
"@turf/centroid": "^7.0.0-alpha.
|
|
59
|
-
"@turf/clusters": "^7.0.0-alpha.
|
|
60
|
-
"@turf/random": "^7.0.0-alpha.
|
|
61
|
-
"@types/
|
|
62
|
-
"@types/
|
|
63
|
-
"
|
|
64
|
-
"
|
|
62
|
+
"@turf/centroid": "^7.0.0-alpha.111+08576cb50",
|
|
63
|
+
"@turf/clusters": "^7.0.0-alpha.111+08576cb50",
|
|
64
|
+
"@turf/random": "^7.0.0-alpha.111+08576cb50",
|
|
65
|
+
"@types/benchmark": "^2.1.5",
|
|
66
|
+
"@types/skmeans": "^0.11.7",
|
|
67
|
+
"@types/tape": "^4.2.32",
|
|
68
|
+
"benchmark": "^2.1.4",
|
|
69
|
+
"chromatism": "^3.0.0",
|
|
65
70
|
"concaveman": "^1.2.1",
|
|
66
|
-
"load-json-file": "
|
|
67
|
-
"
|
|
68
|
-
"
|
|
69
|
-
"
|
|
70
|
-
"
|
|
71
|
-
"
|
|
72
|
-
"
|
|
73
|
-
"write-json-file": "*"
|
|
71
|
+
"load-json-file": "^7.0.1",
|
|
72
|
+
"npm-run-all": "^4.1.5",
|
|
73
|
+
"tape": "^5.7.2",
|
|
74
|
+
"tsup": "^8.0.1",
|
|
75
|
+
"tsx": "^4.6.2",
|
|
76
|
+
"typescript": "^5.2.2",
|
|
77
|
+
"write-json-file": "^5.0.0"
|
|
74
78
|
},
|
|
75
79
|
"dependencies": {
|
|
76
|
-
"@turf/clone": "^7.0.0-alpha.
|
|
77
|
-
"@turf/helpers": "^7.0.0-alpha.
|
|
78
|
-
"@turf/invariant": "^7.0.0-alpha.
|
|
79
|
-
"@turf/meta": "^7.0.0-alpha.
|
|
80
|
+
"@turf/clone": "^7.0.0-alpha.111+08576cb50",
|
|
81
|
+
"@turf/helpers": "^7.0.0-alpha.111+08576cb50",
|
|
82
|
+
"@turf/invariant": "^7.0.0-alpha.111+08576cb50",
|
|
83
|
+
"@turf/meta": "^7.0.0-alpha.111+08576cb50",
|
|
80
84
|
"skmeans": "0.9.7",
|
|
81
|
-
"tslib": "^2.
|
|
85
|
+
"tslib": "^2.6.2"
|
|
82
86
|
},
|
|
83
|
-
"gitHead": "
|
|
87
|
+
"gitHead": "08576cb50376e0199aea02dbd887e3af83672246"
|
|
84
88
|
}
|
package/dist/es/index.js
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import clone from "@turf/clone";
|
|
2
|
-
import { coordAll, featureEach } from "@turf/meta";
|
|
3
|
-
import skmeans from "skmeans";
|
|
4
|
-
/**
|
|
5
|
-
* Takes a set of {@link Point|points} and partition them into clusters using the k-mean .
|
|
6
|
-
* It uses the [k-means algorithm](https://en.wikipedia.org/wiki/K-means_clustering)
|
|
7
|
-
*
|
|
8
|
-
* @name clustersKmeans
|
|
9
|
-
* @param {FeatureCollection<Point>} points to be clustered
|
|
10
|
-
* @param {Object} [options={}] Optional parameters
|
|
11
|
-
* @param {number} [options.numberOfClusters=Math.sqrt(numberOfPoints/2)] numberOfClusters that will be generated
|
|
12
|
-
* @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)
|
|
13
|
-
* @returns {FeatureCollection<Point>} Clustered Points with an additional two properties associated to each Feature:
|
|
14
|
-
* - {number} cluster - the associated clusterId
|
|
15
|
-
* - {[number, number]} centroid - Centroid of the cluster [Longitude, Latitude]
|
|
16
|
-
* @example
|
|
17
|
-
* // create random points with random z-values in their properties
|
|
18
|
-
* var points = turf.randomPoint(100, {bbox: [0, 30, 20, 50]});
|
|
19
|
-
* var options = {numberOfClusters: 7};
|
|
20
|
-
* var clustered = turf.clustersKmeans(points, options);
|
|
21
|
-
*
|
|
22
|
-
* //addToMap
|
|
23
|
-
* var addToMap = [clustered];
|
|
24
|
-
*/
|
|
25
|
-
function clustersKmeans(points, options = {}) {
|
|
26
|
-
// Default Params
|
|
27
|
-
var count = points.features.length;
|
|
28
|
-
options.numberOfClusters =
|
|
29
|
-
options.numberOfClusters || Math.round(Math.sqrt(count / 2));
|
|
30
|
-
// numberOfClusters can't be greater than the number of points
|
|
31
|
-
// fallbacks to count
|
|
32
|
-
if (options.numberOfClusters > count)
|
|
33
|
-
options.numberOfClusters = count;
|
|
34
|
-
// Clone points to prevent any mutations (enabled by default)
|
|
35
|
-
if (options.mutate !== true)
|
|
36
|
-
points = clone(points);
|
|
37
|
-
// collect points coordinates
|
|
38
|
-
var data = coordAll(points);
|
|
39
|
-
// create seed to avoid skmeans to drift
|
|
40
|
-
var initialCentroids = data.slice(0, options.numberOfClusters);
|
|
41
|
-
// create skmeans clusters
|
|
42
|
-
var skmeansResult = skmeans(data, options.numberOfClusters, initialCentroids);
|
|
43
|
-
// store centroids {clusterId: [number, number]}
|
|
44
|
-
var centroids = {};
|
|
45
|
-
skmeansResult.centroids.forEach(function (coord, idx) {
|
|
46
|
-
centroids[idx] = coord;
|
|
47
|
-
});
|
|
48
|
-
// add associated cluster number
|
|
49
|
-
featureEach(points, function (point, index) {
|
|
50
|
-
var clusterId = skmeansResult.idxs[index];
|
|
51
|
-
point.properties.cluster = clusterId;
|
|
52
|
-
point.properties.centroid = centroids[clusterId];
|
|
53
|
-
});
|
|
54
|
-
return points;
|
|
55
|
-
}
|
|
56
|
-
export default clustersKmeans;
|
package/dist/es/package.json
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"type":"module"}
|
package/dist/js/index.js
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const tslib_1 = require("tslib");
|
|
4
|
-
const clone_1 = tslib_1.__importDefault(require("@turf/clone"));
|
|
5
|
-
const meta_1 = require("@turf/meta");
|
|
6
|
-
const skmeans_1 = tslib_1.__importDefault(require("skmeans"));
|
|
7
|
-
/**
|
|
8
|
-
* Takes a set of {@link Point|points} and partition them into clusters using the k-mean .
|
|
9
|
-
* It uses the [k-means algorithm](https://en.wikipedia.org/wiki/K-means_clustering)
|
|
10
|
-
*
|
|
11
|
-
* @name clustersKmeans
|
|
12
|
-
* @param {FeatureCollection<Point>} points to be clustered
|
|
13
|
-
* @param {Object} [options={}] Optional parameters
|
|
14
|
-
* @param {number} [options.numberOfClusters=Math.sqrt(numberOfPoints/2)] numberOfClusters that will be generated
|
|
15
|
-
* @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)
|
|
16
|
-
* @returns {FeatureCollection<Point>} Clustered Points with an additional two properties associated to each Feature:
|
|
17
|
-
* - {number} cluster - the associated clusterId
|
|
18
|
-
* - {[number, number]} centroid - Centroid of the cluster [Longitude, Latitude]
|
|
19
|
-
* @example
|
|
20
|
-
* // create random points with random z-values in their properties
|
|
21
|
-
* var points = turf.randomPoint(100, {bbox: [0, 30, 20, 50]});
|
|
22
|
-
* var options = {numberOfClusters: 7};
|
|
23
|
-
* var clustered = turf.clustersKmeans(points, options);
|
|
24
|
-
*
|
|
25
|
-
* //addToMap
|
|
26
|
-
* var addToMap = [clustered];
|
|
27
|
-
*/
|
|
28
|
-
function clustersKmeans(points, options = {}) {
|
|
29
|
-
// Default Params
|
|
30
|
-
var count = points.features.length;
|
|
31
|
-
options.numberOfClusters =
|
|
32
|
-
options.numberOfClusters || Math.round(Math.sqrt(count / 2));
|
|
33
|
-
// numberOfClusters can't be greater than the number of points
|
|
34
|
-
// fallbacks to count
|
|
35
|
-
if (options.numberOfClusters > count)
|
|
36
|
-
options.numberOfClusters = count;
|
|
37
|
-
// Clone points to prevent any mutations (enabled by default)
|
|
38
|
-
if (options.mutate !== true)
|
|
39
|
-
points = clone_1.default(points);
|
|
40
|
-
// collect points coordinates
|
|
41
|
-
var data = meta_1.coordAll(points);
|
|
42
|
-
// create seed to avoid skmeans to drift
|
|
43
|
-
var initialCentroids = data.slice(0, options.numberOfClusters);
|
|
44
|
-
// create skmeans clusters
|
|
45
|
-
var skmeansResult = skmeans_1.default(data, options.numberOfClusters, initialCentroids);
|
|
46
|
-
// store centroids {clusterId: [number, number]}
|
|
47
|
-
var centroids = {};
|
|
48
|
-
skmeansResult.centroids.forEach(function (coord, idx) {
|
|
49
|
-
centroids[idx] = coord;
|
|
50
|
-
});
|
|
51
|
-
// add associated cluster number
|
|
52
|
-
meta_1.featureEach(points, function (point, index) {
|
|
53
|
-
var clusterId = skmeansResult.idxs[index];
|
|
54
|
-
point.properties.cluster = clusterId;
|
|
55
|
-
point.properties.centroid = centroids[clusterId];
|
|
56
|
-
});
|
|
57
|
-
return points;
|
|
58
|
-
}
|
|
59
|
-
exports.default = clustersKmeans;
|