@turf/clusters 7.1.0 → 7.2.0
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 +8 -2
- package/dist/cjs/index.cjs +9 -18
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +9 -8
- package/dist/esm/index.d.ts +9 -8
- package/dist/esm/index.js +9 -18
- package/dist/esm/index.js.map +1 -1
- package/package.json +10 -10
package/README.md
CHANGED
|
@@ -60,7 +60,7 @@ clusterEach
|
|
|
60
60
|
|
|
61
61
|
* `geojson` **[FeatureCollection][1]** GeoJSON Features
|
|
62
62
|
* `property` **([string][4] | [number][3])** GeoJSON property key/value used to create clusters
|
|
63
|
-
* `callback` **[
|
|
63
|
+
* `callback` **[clusterEachCallback][5]** a method that takes (cluster, clusterValue, currentIndex)
|
|
64
64
|
|
|
65
65
|
### Examples
|
|
66
66
|
|
|
@@ -126,6 +126,8 @@ Type: [Function][2]
|
|
|
126
126
|
* `currentIndex` **[number][3]?** The index of the current element being processed in the
|
|
127
127
|
array. Starts at index 0, if an initialValue is provided, and at index 1 otherwise.
|
|
128
128
|
|
|
129
|
+
Returns **void** 
|
|
130
|
+
|
|
129
131
|
## clusterReduce
|
|
130
132
|
|
|
131
133
|
Reduce clusters in GeoJSON Features, similar to Array.reduce()
|
|
@@ -134,7 +136,7 @@ Reduce clusters in GeoJSON Features, similar to Array.reduce()
|
|
|
134
136
|
|
|
135
137
|
* `geojson` **[FeatureCollection][1]** GeoJSON Features
|
|
136
138
|
* `property` **([string][4] | [number][3])** GeoJSON property key/value used to create clusters
|
|
137
|
-
* `callback` **[
|
|
139
|
+
* `callback` **[clusterReduceCallback][6]** a method that takes (previousValue, cluster, clusterValue, currentIndex)
|
|
138
140
|
* `initialValue` **any?** Value to use as the first argument to the first call of the callback.
|
|
139
141
|
|
|
140
142
|
### Examples
|
|
@@ -182,6 +184,10 @@ Returns **any** The value that results from the reduction.
|
|
|
182
184
|
|
|
183
185
|
[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
|
|
184
186
|
|
|
187
|
+
[5]: #clustereachcallback
|
|
188
|
+
|
|
189
|
+
[6]: #clusterreducecallback
|
|
190
|
+
|
|
185
191
|
<!-- 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. -->
|
|
186
192
|
|
|
187
193
|
---
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -2,22 +2,19 @@
|
|
|
2
2
|
var _meta = require('@turf/meta');
|
|
3
3
|
var _helpers = require('@turf/helpers');
|
|
4
4
|
function getCluster(geojson, filter) {
|
|
5
|
-
if (!geojson)
|
|
6
|
-
throw new Error("geojson is required");
|
|
5
|
+
if (!geojson) throw new Error("geojson is required");
|
|
7
6
|
if (geojson.type !== "FeatureCollection")
|
|
8
7
|
throw new Error("geojson must be a FeatureCollection");
|
|
9
8
|
if (filter === void 0 || filter === null)
|
|
10
9
|
throw new Error("filter is required");
|
|
11
10
|
var features = [];
|
|
12
11
|
_meta.featureEach.call(void 0, geojson, function(feature) {
|
|
13
|
-
if (applyFilter(feature.properties, filter))
|
|
14
|
-
features.push(feature);
|
|
12
|
+
if (applyFilter(feature.properties, filter)) features.push(feature);
|
|
15
13
|
});
|
|
16
14
|
return _helpers.featureCollection.call(void 0, features);
|
|
17
15
|
}
|
|
18
16
|
function clusterEach(geojson, property, callback) {
|
|
19
|
-
if (!geojson)
|
|
20
|
-
throw new Error("geojson is required");
|
|
17
|
+
if (!geojson) throw new Error("geojson is required");
|
|
21
18
|
if (geojson.type !== "FeatureCollection")
|
|
22
19
|
throw new Error("geojson must be a FeatureCollection");
|
|
23
20
|
if (property === void 0 || property === null)
|
|
@@ -61,22 +58,19 @@ function createBins(geojson, property) {
|
|
|
61
58
|
var value = properties[property];
|
|
62
59
|
if (Object.prototype.hasOwnProperty.call(bins, value))
|
|
63
60
|
bins[value].push(i);
|
|
64
|
-
else
|
|
65
|
-
bins[value] = [i];
|
|
61
|
+
else bins[value] = [i];
|
|
66
62
|
}
|
|
67
63
|
});
|
|
68
64
|
return bins;
|
|
69
65
|
}
|
|
70
66
|
function applyFilter(properties, filter) {
|
|
71
|
-
if (properties === void 0)
|
|
72
|
-
return false;
|
|
67
|
+
if (properties === void 0) return false;
|
|
73
68
|
var filterType = typeof filter;
|
|
74
69
|
if (filterType === "number" || filterType === "string")
|
|
75
70
|
return Object.prototype.hasOwnProperty.call(properties, filter);
|
|
76
71
|
else if (Array.isArray(filter)) {
|
|
77
72
|
for (var i = 0; i < filter.length; i++) {
|
|
78
|
-
if (!applyFilter(properties, filter[i]))
|
|
79
|
-
return false;
|
|
73
|
+
if (!applyFilter(properties, filter[i])) return false;
|
|
80
74
|
}
|
|
81
75
|
return true;
|
|
82
76
|
} else {
|
|
@@ -87,16 +81,13 @@ function propertiesContainsFilter(properties, filter) {
|
|
|
87
81
|
var keys = Object.keys(filter);
|
|
88
82
|
for (var i = 0; i < keys.length; i++) {
|
|
89
83
|
var key = keys[i];
|
|
90
|
-
if (properties[key] !== filter[key])
|
|
91
|
-
return false;
|
|
84
|
+
if (properties[key] !== filter[key]) return false;
|
|
92
85
|
}
|
|
93
86
|
return true;
|
|
94
87
|
}
|
|
95
88
|
function filterProperties(properties, keys) {
|
|
96
|
-
if (!keys)
|
|
97
|
-
|
|
98
|
-
if (!keys.length)
|
|
99
|
-
return {};
|
|
89
|
+
if (!keys) return {};
|
|
90
|
+
if (!keys.length) return {};
|
|
100
91
|
var newProperties = {};
|
|
101
92
|
for (var i = 0; i < keys.length; i++) {
|
|
102
93
|
var key = keys[i];
|
package/dist/cjs/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../index.ts"],"names":[],"mappings":";AAMA,SAAS,mBAAmB;AAC5B,SAAS,yBAAyB;AA+BlC,SAAS,WAGP,SAAkC,QAAsC;AAExE,MAAI,CAAC;AAAS,UAAM,IAAI,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Ae,KAAK,YAAY,GAAG;AACtD,oBAAc,GAAG,IAAI,WAAW,GAAG;AAAA,EACvC;AACA,SAAO;AACT","sourcesContent":["import {\n Feature,\n FeatureCollection,\n GeoJsonProperties,\n GeometryObject,\n} from \"geojson\";\nimport { featureEach } from \"@turf/meta\";\nimport { featureCollection } from \"@turf/helpers\";\n\n/**\n * Get Cluster\n *\n * @name getCluster\n * @param {FeatureCollection} geojson GeoJSON Features\n * @param {*} filter Filter used on GeoJSON properties to get Cluster\n * @returns {FeatureCollection} Single Cluster filtered by GeoJSON Properties\n * @example\n * var geojson = turf.featureCollection([\n * turf.point([0, 0], {'marker-symbol': 'circle'}),\n * turf.point([2, 4], {'marker-symbol': 'star'}),\n * turf.point([3, 6], {'marker-symbol': 'star'}),\n * turf.point([5, 1], {'marker-symbol': 'square'}),\n * turf.point([4, 2], {'marker-symbol': 'circle'})\n * ]);\n *\n * // Create a cluster using K-Means (adds `cluster` to GeoJSON properties)\n * var clustered = turf.clustersKmeans(geojson);\n *\n * // Retrieve first cluster (0)\n * var cluster = turf.getCluster(clustered, {cluster: 0});\n * //= cluster\n *\n * // Retrieve cluster based on custom properties\n * turf.getCluster(clustered, {'marker-symbol': 'circle'}).length;\n * //= 2\n * turf.getCluster(clustered, {'marker-symbol': 'square'}).length;\n * //= 1\n */\nfunction getCluster<\n G extends GeometryObject,\n P extends GeoJsonProperties = GeoJsonProperties,\n>(geojson: FeatureCollection<G, P>, filter: any): FeatureCollection<G, P> {\n // Validation\n if (!geojson) throw new Error(\"geojson is required\");\n if (geojson.type !== \"FeatureCollection\")\n throw new Error(\"geojson must be a FeatureCollection\");\n if (filter === undefined || filter === null)\n throw new Error(\"filter is required\");\n\n // Filter Features\n var features: Feature<G, P>[] = [];\n featureEach<G, P>(geojson, function (feature) {\n if (applyFilter(feature.properties, filter)) features.push(feature);\n });\n return featureCollection(features);\n}\n\n/**\n * Callback for clusterEach\n *\n * @callback clusterEachCallback\n * @param {FeatureCollection} [cluster] The current cluster being processed.\n * @param {*} [clusterValue] Value used to create cluster being processed.\n * @param {number} [currentIndex] The index of the current element being processed in the array.Starts at index 0\n * @returns {void}\n */\n\n/**\n * clusterEach\n *\n * @name clusterEach\n * @param {FeatureCollection} geojson GeoJSON Features\n * @param {string|number} property GeoJSON property key/value used to create clusters\n * @param {Function} callback a method that takes (cluster, clusterValue, currentIndex)\n * @returns {void}\n * @example\n * var geojson = turf.featureCollection([\n * turf.point([0, 0]),\n * turf.point([2, 4]),\n * turf.point([3, 6]),\n * turf.point([5, 1]),\n * turf.point([4, 2])\n * ]);\n *\n * // Create a cluster using K-Means (adds `cluster` to GeoJSON properties)\n * var clustered = turf.clustersKmeans(geojson);\n *\n * // Iterate over each cluster\n * turf.clusterEach(clustered, 'cluster', function (cluster, clusterValue, currentIndex) {\n * //= cluster\n * //= clusterValue\n * //= currentIndex\n * })\n *\n * // Calculate the total number of clusters\n * var total = 0\n * turf.clusterEach(clustered, 'cluster', function () {\n * total++;\n * });\n *\n * // Create an Array of all the values retrieved from the 'cluster' property\n * var values = []\n * turf.clusterEach(clustered, 'cluster', function (cluster, clusterValue) {\n * values.push(clusterValue);\n * });\n */\nfunction clusterEach<\n G extends GeometryObject,\n P extends GeoJsonProperties = GeoJsonProperties,\n>(\n geojson: FeatureCollection<G, P>,\n property: number | string,\n callback: (\n cluster: FeatureCollection<G, P>,\n clusterValue?: any,\n currentIndex?: number\n ) => void\n): void {\n // Validation\n if (!geojson) throw new Error(\"geojson is required\");\n if (geojson.type !== \"FeatureCollection\")\n throw new Error(\"geojson must be a FeatureCollection\");\n if (property === undefined || property === null)\n throw new Error(\"property is required\");\n\n // Create clusters based on property values\n var bins = createBins(geojson, property);\n var values = Object.keys(bins);\n for (var index = 0; index < values.length; index++) {\n var value = values[index];\n var bin = bins[value];\n var features = [];\n for (var i = 0; i < bin.length; i++) {\n features.push(geojson.features[bin[i]]);\n }\n callback(featureCollection(features), value, index);\n }\n}\n\n/**\n * Callback for clusterReduce\n *\n * The first time the callback function is called, the values provided as arguments depend\n * on whether the reduce method has an initialValue argument.\n *\n * If an initialValue is provided to the reduce method:\n * - The previousValue argument is initialValue.\n * - The currentValue argument is the value of the first element present in the array.\n *\n * If an initialValue is not provided:\n * - The previousValue argument is the value of the first element present in the array.\n * - The currentValue argument is the value of the second element present in the array.\n *\n * @callback clusterReduceCallback\n * @param {*} [previousValue] The accumulated value previously returned in the last invocation\n * of the callback, or initialValue, if supplied.\n * @param {FeatureCollection} [cluster] The current cluster being processed.\n * @param {*} [clusterValue] Value used to create cluster being processed.\n * @param {number} [currentIndex] The index of the current element being processed in the\n * array. Starts at index 0, if an initialValue is provided, and at index 1 otherwise.\n */\n\n/**\n * Reduce clusters in GeoJSON Features, similar to Array.reduce()\n *\n * @name clusterReduce\n * @param {FeatureCollection} geojson GeoJSON Features\n * @param {string|number} property GeoJSON property key/value used to create clusters\n * @param {Function} callback a method that takes (previousValue, cluster, clusterValue, currentIndex)\n * @param {*} [initialValue] Value to use as the first argument to the first call of the callback.\n * @returns {*} The value that results from the reduction.\n * @example\n * var geojson = turf.featureCollection([\n * turf.point([0, 0]),\n * turf.point([2, 4]),\n * turf.point([3, 6]),\n * turf.point([5, 1]),\n * turf.point([4, 2])\n * ]);\n *\n * // Create a cluster using K-Means (adds `cluster` to GeoJSON properties)\n * var clustered = turf.clustersKmeans(geojson);\n *\n * // Iterate over each cluster and perform a calculation\n * var initialValue = 0\n * turf.clusterReduce(clustered, 'cluster', function (previousValue, cluster, clusterValue, currentIndex) {\n * //=previousValue\n * //=cluster\n * //=clusterValue\n * //=currentIndex\n * return previousValue++;\n * }, initialValue);\n *\n * // Calculate the total number of clusters\n * var total = turf.clusterReduce(clustered, 'cluster', function (previousValue) {\n * return previousValue++;\n * }, 0);\n *\n * // Create an Array of all the values retrieved from the 'cluster' property\n * var values = turf.clusterReduce(clustered, 'cluster', function (previousValue, cluster, clusterValue) {\n * return previousValue.concat(clusterValue);\n * }, []);\n */\nfunction clusterReduce<\n G extends GeometryObject,\n P extends GeoJsonProperties = GeoJsonProperties,\n>(\n geojson: FeatureCollection<G, P>,\n property: number | string,\n callback: (\n previousValue: any | undefined,\n cluster: FeatureCollection<G, P>,\n clusterValue?: any,\n currentIndex?: number\n ) => void,\n initialValue?: any\n): void {\n var previousValue = initialValue;\n clusterEach(\n geojson,\n property,\n function (cluster, clusterValue, currentIndex) {\n if (currentIndex === 0 && initialValue === undefined)\n previousValue = cluster;\n else\n previousValue = callback(\n previousValue,\n cluster,\n clusterValue,\n currentIndex\n );\n }\n );\n return previousValue;\n}\n\n/**\n * Create Bins\n *\n * @private\n * @param {FeatureCollection} geojson GeoJSON Features\n * @param {string|number} property Property values are used to create bins\n * @returns {Object} bins with Feature IDs\n * @example\n * var geojson = turf.featureCollection([\n * turf.point([0, 0], {cluster: 0, foo: 'null'}),\n * turf.point([2, 4], {cluster: 1, foo: 'bar'}),\n * turf.point([5, 1], {0: 'foo'}),\n * turf.point([3, 6], {cluster: 1}),\n * ]);\n * createBins(geojson, 'cluster');\n * //= { '0': [ 0 ], '1': [ 1, 3 ] }\n */\nfunction createBins(\n geojson: FeatureCollection<any>,\n property: string | number\n) {\n var bins: Record<string, number[]> = {};\n\n featureEach(geojson, function (feature, i) {\n var properties = feature.properties || {};\n if (Object.prototype.hasOwnProperty.call(properties, String(property))) {\n var value = properties[property];\n if (Object.prototype.hasOwnProperty.call(bins, value))\n bins[value].push(i);\n else bins[value] = [i];\n }\n });\n return bins;\n}\n\n/**\n * Apply Filter\n *\n * @private\n * @param {*} properties Properties\n * @param {*} filter Filter\n * @returns {boolean} applied Filter to properties\n */\nfunction applyFilter(properties: any, filter: any) {\n if (properties === undefined) return false;\n var filterType = typeof filter;\n\n // String & Number\n if (filterType === \"number\" || filterType === \"string\")\n return Object.prototype.hasOwnProperty.call(properties, filter);\n // Array\n else if (Array.isArray(filter)) {\n for (var i = 0; i < filter.length; i++) {\n if (!applyFilter(properties, filter[i])) return false;\n }\n return true;\n // Object\n } else {\n return propertiesContainsFilter(properties, filter);\n }\n}\n\n/**\n * Properties contains filter (does not apply deepEqual operations)\n *\n * @private\n * @param {*} properties Properties\n * @param {Object} filter Filter\n * @returns {boolean} does filter equal Properties\n * @example\n * propertiesContainsFilter({foo: 'bar', cluster: 0}, {cluster: 0})\n * //= true\n * propertiesContainsFilter({foo: 'bar', cluster: 0}, {cluster: 1})\n * //= false\n */\nfunction propertiesContainsFilter(properties: any, filter: any): boolean {\n var keys = Object.keys(filter);\n for (var i = 0; i < keys.length; i++) {\n var key = keys[i];\n if (properties[key] !== filter[key]) return false;\n }\n return true;\n}\n\n/**\n * Filter Properties\n *\n * @private\n * @param {*} properties Properties\n * @param {Array<string>} keys Used to filter Properties\n * @returns {*} filtered Properties\n * @example\n * filterProperties({foo: 'bar', cluster: 0}, ['cluster'])\n * //= {cluster: 0}\n */\nfunction filterProperties(\n properties: Record<string, any>,\n keys: string[]\n): any {\n if (!keys) return {};\n if (!keys.length) return {};\n\n var newProperties: Record<string, any> = {};\n for (var i = 0; i < keys.length; i++) {\n var key = keys[i];\n if (Object.prototype.hasOwnProperty.call(properties, key))\n newProperties[key] = properties[key];\n }\n return newProperties;\n}\n\nexport {\n getCluster,\n clusterEach,\n clusterReduce,\n createBins,\n applyFilter,\n propertiesContainsFilter,\n filterProperties,\n};\n// No default export!\n"]}
|
|
1
|
+
{"version":3,"sources":["/home/runner/work/turf/turf/packages/turf-clusters/dist/cjs/index.cjs","../../index.ts"],"names":[],"mappings":"AAAA;ACMA,kCAA4B;AAC5B,wCAAkC;AA+BlC,SAAS,UAAA,CAGP,OAAA,EAAkC,MAAA,EAAsC;AAExE,EAAA,GAAA,CAAI,CAAC,OAAA,EAAS,MAAM,IAAI,KAAA,CAAM,qBAAqB,CAAA;AACnD,EAAA,GAAA,CAAI,OAAA,CAAQ,KAAA,IAAS,mBAAA;AACnB,IAAA,MAAM,IAAI,KAAA,CAAM,qCAAqC,CAAA;AACvD,EAAA,GAAA,CAAI,OAAA,IAAW,KAAA,EAAA,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AA,IAAM,MAAA,CAAO,GAAG,CAAA,EAAG,OAAO,KAAA;AAAA,EAC9C;AACA,EAAA,OAAO,IAAA;AACT;AAaA,SAAS,gBAAA,CACP,UAAA,EACA,IAAA,EACK;AACL,EAAA,GAAA,CAAI,CAAC,IAAA,EAAM,OAAO,CAAC,CAAA;AACnB,EAAA,GAAA,CAAI,CAAC,IAAA,CAAK,MAAA,EAAQ,OAAO,CAAC,CAAA;AAE1B,EAAA,IAAI,cAAA,EAAqC,CAAC,CAAA;AAC1C,EAAA,IAAA,CAAA,IAAS,EAAA,EAAI,CAAA,EAAG,EAAA,EAAI,IAAA,CAAK,MAAA,EAAQ,CAAA,EAAA,EAAK;AACpC,IAAA,IAAI,IAAA,EAAM,IAAA,CAAK,CAAC,CAAA;AAChB,IAAA,GAAA,CAAI,MAAA,CAAO,SAAA,CAAU,cAAA,CAAe,IAAA,CAAK,UAAA,EAAY,GAAG,CAAA;AACtD,MAAA,aAAA,CAAc,GAAG,EAAA,EAAI,UAAA,CAAW,GAAG,CAAA;AAAA,EACvC;AACA,EAAA,OAAO,aAAA;AACT;ADxPA;AACE;AACA;AACA;AACA;AACA;AACA;AACA;AACF,wRAAC","file":"/home/runner/work/turf/turf/packages/turf-clusters/dist/cjs/index.cjs","sourcesContent":[null,"import {\n Feature,\n FeatureCollection,\n GeoJsonProperties,\n GeometryObject,\n} from \"geojson\";\nimport { featureEach } from \"@turf/meta\";\nimport { featureCollection } from \"@turf/helpers\";\n\n/**\n * Get Cluster\n *\n * @function\n * @param {FeatureCollection} geojson GeoJSON Features\n * @param {*} filter Filter used on GeoJSON properties to get Cluster\n * @returns {FeatureCollection} Single Cluster filtered by GeoJSON Properties\n * @example\n * var geojson = turf.featureCollection([\n * turf.point([0, 0], {'marker-symbol': 'circle'}),\n * turf.point([2, 4], {'marker-symbol': 'star'}),\n * turf.point([3, 6], {'marker-symbol': 'star'}),\n * turf.point([5, 1], {'marker-symbol': 'square'}),\n * turf.point([4, 2], {'marker-symbol': 'circle'})\n * ]);\n *\n * // Create a cluster using K-Means (adds `cluster` to GeoJSON properties)\n * var clustered = turf.clustersKmeans(geojson);\n *\n * // Retrieve first cluster (0)\n * var cluster = turf.getCluster(clustered, {cluster: 0});\n * //= cluster\n *\n * // Retrieve cluster based on custom properties\n * turf.getCluster(clustered, {'marker-symbol': 'circle'}).length;\n * //= 2\n * turf.getCluster(clustered, {'marker-symbol': 'square'}).length;\n * //= 1\n */\nfunction getCluster<\n G extends GeometryObject,\n P extends GeoJsonProperties = GeoJsonProperties,\n>(geojson: FeatureCollection<G, P>, filter: any): FeatureCollection<G, P> {\n // Validation\n if (!geojson) throw new Error(\"geojson is required\");\n if (geojson.type !== \"FeatureCollection\")\n throw new Error(\"geojson must be a FeatureCollection\");\n if (filter === undefined || filter === null)\n throw new Error(\"filter is required\");\n\n // Filter Features\n var features: Feature<G, P>[] = [];\n featureEach<G, P>(geojson, function (feature) {\n if (applyFilter(feature.properties, filter)) features.push(feature);\n });\n return featureCollection(features);\n}\n\n/**\n * Callback for clusterEach\n *\n * @callback clusterEachCallback\n * @param {FeatureCollection} [cluster] The current cluster being processed.\n * @param {any} [clusterValue] Value used to create cluster being processed.\n * @param {number} [currentIndex] The index of the current element being processed in the array.Starts at index 0\n * @returns {void}\n */\n\n/**\n * clusterEach\n *\n * @function\n * @param {FeatureCollection} geojson GeoJSON Features\n * @param {string|number} property GeoJSON property key/value used to create clusters\n * @param {clusterEachCallback} callback a method that takes (cluster, clusterValue, currentIndex)\n * @returns {void}\n * @example\n * var geojson = turf.featureCollection([\n * turf.point([0, 0]),\n * turf.point([2, 4]),\n * turf.point([3, 6]),\n * turf.point([5, 1]),\n * turf.point([4, 2])\n * ]);\n *\n * // Create a cluster using K-Means (adds `cluster` to GeoJSON properties)\n * var clustered = turf.clustersKmeans(geojson);\n *\n * // Iterate over each cluster\n * turf.clusterEach(clustered, 'cluster', function (cluster, clusterValue, currentIndex) {\n * //= cluster\n * //= clusterValue\n * //= currentIndex\n * })\n *\n * // Calculate the total number of clusters\n * var total = 0\n * turf.clusterEach(clustered, 'cluster', function () {\n * total++;\n * });\n *\n * // Create an Array of all the values retrieved from the 'cluster' property\n * var values = []\n * turf.clusterEach(clustered, 'cluster', function (cluster, clusterValue) {\n * values.push(clusterValue);\n * });\n */\nfunction clusterEach<\n G extends GeometryObject,\n P extends GeoJsonProperties = GeoJsonProperties,\n>(\n geojson: FeatureCollection<G, P>,\n property: number | string,\n callback: (\n cluster: FeatureCollection<G, P>,\n clusterValue?: any,\n currentIndex?: number\n ) => void\n): void {\n // Validation\n if (!geojson) throw new Error(\"geojson is required\");\n if (geojson.type !== \"FeatureCollection\")\n throw new Error(\"geojson must be a FeatureCollection\");\n if (property === undefined || property === null)\n throw new Error(\"property is required\");\n\n // Create clusters based on property values\n var bins = createBins(geojson, property);\n var values = Object.keys(bins);\n for (var index = 0; index < values.length; index++) {\n var value = values[index];\n var bin = bins[value];\n var features = [];\n for (var i = 0; i < bin.length; i++) {\n features.push(geojson.features[bin[i]]);\n }\n callback(featureCollection(features), value, index);\n }\n}\n\n/**\n * Callback for clusterReduce\n *\n * The first time the callback function is called, the values provided as arguments depend\n * on whether the reduce method has an initialValue argument.\n *\n * If an initialValue is provided to the reduce method:\n * - The previousValue argument is initialValue.\n * - The currentValue argument is the value of the first element present in the array.\n *\n * If an initialValue is not provided:\n * - The previousValue argument is the value of the first element present in the array.\n * - The currentValue argument is the value of the second element present in the array.\n *\n * @callback clusterReduceCallback\n * @param {*} [previousValue] The accumulated value previously returned in the last invocation\n * of the callback, or initialValue, if supplied.\n * @param {FeatureCollection} [cluster] The current cluster being processed.\n * @param {*} [clusterValue] Value used to create cluster being processed.\n * @param {number} [currentIndex] The index of the current element being processed in the\n * array. Starts at index 0, if an initialValue is provided, and at index 1 otherwise.\n * @returns {void}\n */\n\n/**\n * Reduce clusters in GeoJSON Features, similar to Array.reduce()\n *\n * @function\n * @param {FeatureCollection} geojson GeoJSON Features\n * @param {string|number} property GeoJSON property key/value used to create clusters\n * @param {clusterReduceCallback} callback a method that takes (previousValue, cluster, clusterValue, currentIndex)\n * @param {any} [initialValue] Value to use as the first argument to the first call of the callback.\n * @returns {any} The value that results from the reduction.\n * @example\n * var geojson = turf.featureCollection([\n * turf.point([0, 0]),\n * turf.point([2, 4]),\n * turf.point([3, 6]),\n * turf.point([5, 1]),\n * turf.point([4, 2])\n * ]);\n *\n * // Create a cluster using K-Means (adds `cluster` to GeoJSON properties)\n * var clustered = turf.clustersKmeans(geojson);\n *\n * // Iterate over each cluster and perform a calculation\n * var initialValue = 0\n * turf.clusterReduce(clustered, 'cluster', function (previousValue, cluster, clusterValue, currentIndex) {\n * //=previousValue\n * //=cluster\n * //=clusterValue\n * //=currentIndex\n * return previousValue++;\n * }, initialValue);\n *\n * // Calculate the total number of clusters\n * var total = turf.clusterReduce(clustered, 'cluster', function (previousValue) {\n * return previousValue++;\n * }, 0);\n *\n * // Create an Array of all the values retrieved from the 'cluster' property\n * var values = turf.clusterReduce(clustered, 'cluster', function (previousValue, cluster, clusterValue) {\n * return previousValue.concat(clusterValue);\n * }, []);\n */\nfunction clusterReduce<\n G extends GeometryObject,\n P extends GeoJsonProperties = GeoJsonProperties,\n>(\n geojson: FeatureCollection<G, P>,\n property: number | string,\n callback: (\n previousValue: any | undefined,\n cluster: FeatureCollection<G, P>,\n clusterValue?: any,\n currentIndex?: number\n ) => void,\n initialValue?: any\n): void {\n var previousValue = initialValue;\n clusterEach(\n geojson,\n property,\n function (cluster, clusterValue, currentIndex) {\n if (currentIndex === 0 && initialValue === undefined)\n previousValue = cluster;\n else\n previousValue = callback(\n previousValue,\n cluster,\n clusterValue,\n currentIndex\n );\n }\n );\n return previousValue;\n}\n\n/**\n * Create Bins\n *\n * @private\n * @param {FeatureCollection} geojson GeoJSON Features\n * @param {string|number} property Property values are used to create bins\n * @returns {Object} bins with Feature IDs\n * @example\n * var geojson = turf.featureCollection([\n * turf.point([0, 0], {cluster: 0, foo: 'null'}),\n * turf.point([2, 4], {cluster: 1, foo: 'bar'}),\n * turf.point([5, 1], {0: 'foo'}),\n * turf.point([3, 6], {cluster: 1}),\n * ]);\n * createBins(geojson, 'cluster');\n * //= { '0': [ 0 ], '1': [ 1, 3 ] }\n */\nfunction createBins(\n geojson: FeatureCollection<any>,\n property: string | number\n) {\n var bins: Record<string, number[]> = {};\n\n featureEach(geojson, function (feature, i) {\n var properties = feature.properties || {};\n if (Object.prototype.hasOwnProperty.call(properties, String(property))) {\n var value = properties[property];\n if (Object.prototype.hasOwnProperty.call(bins, value))\n bins[value].push(i);\n else bins[value] = [i];\n }\n });\n return bins;\n}\n\n/**\n * Apply Filter\n *\n * @private\n * @param {*} properties Properties\n * @param {*} filter Filter\n * @returns {boolean} applied Filter to properties\n */\nfunction applyFilter(properties: any, filter: any) {\n if (properties === undefined) return false;\n var filterType = typeof filter;\n\n // String & Number\n if (filterType === \"number\" || filterType === \"string\")\n return Object.prototype.hasOwnProperty.call(properties, filter);\n // Array\n else if (Array.isArray(filter)) {\n for (var i = 0; i < filter.length; i++) {\n if (!applyFilter(properties, filter[i])) return false;\n }\n return true;\n // Object\n } else {\n return propertiesContainsFilter(properties, filter);\n }\n}\n\n/**\n * Properties contains filter (does not apply deepEqual operations)\n *\n * @private\n * @param {*} properties Properties\n * @param {Object} filter Filter\n * @returns {boolean} does filter equal Properties\n * @example\n * propertiesContainsFilter({foo: 'bar', cluster: 0}, {cluster: 0})\n * //= true\n * propertiesContainsFilter({foo: 'bar', cluster: 0}, {cluster: 1})\n * //= false\n */\nfunction propertiesContainsFilter(properties: any, filter: any): boolean {\n var keys = Object.keys(filter);\n for (var i = 0; i < keys.length; i++) {\n var key = keys[i];\n if (properties[key] !== filter[key]) return false;\n }\n return true;\n}\n\n/**\n * Filter Properties\n *\n * @private\n * @param {*} properties Properties\n * @param {Array<string>} keys Used to filter Properties\n * @returns {*} filtered Properties\n * @example\n * filterProperties({foo: 'bar', cluster: 0}, ['cluster'])\n * //= {cluster: 0}\n */\nfunction filterProperties(\n properties: Record<string, any>,\n keys: string[]\n): any {\n if (!keys) return {};\n if (!keys.length) return {};\n\n var newProperties: Record<string, any> = {};\n for (var i = 0; i < keys.length; i++) {\n var key = keys[i];\n if (Object.prototype.hasOwnProperty.call(properties, key))\n newProperties[key] = properties[key];\n }\n return newProperties;\n}\n\nexport {\n getCluster,\n clusterEach,\n clusterReduce,\n createBins,\n applyFilter,\n propertiesContainsFilter,\n filterProperties,\n};\n// No default export!\n"]}
|
package/dist/cjs/index.d.cts
CHANGED
|
@@ -3,7 +3,7 @@ import { GeometryObject, GeoJsonProperties, FeatureCollection } from 'geojson';
|
|
|
3
3
|
/**
|
|
4
4
|
* Get Cluster
|
|
5
5
|
*
|
|
6
|
-
* @
|
|
6
|
+
* @function
|
|
7
7
|
* @param {FeatureCollection} geojson GeoJSON Features
|
|
8
8
|
* @param {*} filter Filter used on GeoJSON properties to get Cluster
|
|
9
9
|
* @returns {FeatureCollection} Single Cluster filtered by GeoJSON Properties
|
|
@@ -35,17 +35,17 @@ declare function getCluster<G extends GeometryObject, P extends GeoJsonPropertie
|
|
|
35
35
|
*
|
|
36
36
|
* @callback clusterEachCallback
|
|
37
37
|
* @param {FeatureCollection} [cluster] The current cluster being processed.
|
|
38
|
-
* @param {
|
|
38
|
+
* @param {any} [clusterValue] Value used to create cluster being processed.
|
|
39
39
|
* @param {number} [currentIndex] The index of the current element being processed in the array.Starts at index 0
|
|
40
40
|
* @returns {void}
|
|
41
41
|
*/
|
|
42
42
|
/**
|
|
43
43
|
* clusterEach
|
|
44
44
|
*
|
|
45
|
-
* @
|
|
45
|
+
* @function
|
|
46
46
|
* @param {FeatureCollection} geojson GeoJSON Features
|
|
47
47
|
* @param {string|number} property GeoJSON property key/value used to create clusters
|
|
48
|
-
* @param {
|
|
48
|
+
* @param {clusterEachCallback} callback a method that takes (cluster, clusterValue, currentIndex)
|
|
49
49
|
* @returns {void}
|
|
50
50
|
* @example
|
|
51
51
|
* var geojson = turf.featureCollection([
|
|
@@ -100,16 +100,17 @@ declare function clusterEach<G extends GeometryObject, P extends GeoJsonProperti
|
|
|
100
100
|
* @param {*} [clusterValue] Value used to create cluster being processed.
|
|
101
101
|
* @param {number} [currentIndex] The index of the current element being processed in the
|
|
102
102
|
* array. Starts at index 0, if an initialValue is provided, and at index 1 otherwise.
|
|
103
|
+
* @returns {void}
|
|
103
104
|
*/
|
|
104
105
|
/**
|
|
105
106
|
* Reduce clusters in GeoJSON Features, similar to Array.reduce()
|
|
106
107
|
*
|
|
107
|
-
* @
|
|
108
|
+
* @function
|
|
108
109
|
* @param {FeatureCollection} geojson GeoJSON Features
|
|
109
110
|
* @param {string|number} property GeoJSON property key/value used to create clusters
|
|
110
|
-
* @param {
|
|
111
|
-
* @param {
|
|
112
|
-
* @returns {
|
|
111
|
+
* @param {clusterReduceCallback} callback a method that takes (previousValue, cluster, clusterValue, currentIndex)
|
|
112
|
+
* @param {any} [initialValue] Value to use as the first argument to the first call of the callback.
|
|
113
|
+
* @returns {any} The value that results from the reduction.
|
|
113
114
|
* @example
|
|
114
115
|
* var geojson = turf.featureCollection([
|
|
115
116
|
* turf.point([0, 0]),
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { GeometryObject, GeoJsonProperties, FeatureCollection } from 'geojson';
|
|
|
3
3
|
/**
|
|
4
4
|
* Get Cluster
|
|
5
5
|
*
|
|
6
|
-
* @
|
|
6
|
+
* @function
|
|
7
7
|
* @param {FeatureCollection} geojson GeoJSON Features
|
|
8
8
|
* @param {*} filter Filter used on GeoJSON properties to get Cluster
|
|
9
9
|
* @returns {FeatureCollection} Single Cluster filtered by GeoJSON Properties
|
|
@@ -35,17 +35,17 @@ declare function getCluster<G extends GeometryObject, P extends GeoJsonPropertie
|
|
|
35
35
|
*
|
|
36
36
|
* @callback clusterEachCallback
|
|
37
37
|
* @param {FeatureCollection} [cluster] The current cluster being processed.
|
|
38
|
-
* @param {
|
|
38
|
+
* @param {any} [clusterValue] Value used to create cluster being processed.
|
|
39
39
|
* @param {number} [currentIndex] The index of the current element being processed in the array.Starts at index 0
|
|
40
40
|
* @returns {void}
|
|
41
41
|
*/
|
|
42
42
|
/**
|
|
43
43
|
* clusterEach
|
|
44
44
|
*
|
|
45
|
-
* @
|
|
45
|
+
* @function
|
|
46
46
|
* @param {FeatureCollection} geojson GeoJSON Features
|
|
47
47
|
* @param {string|number} property GeoJSON property key/value used to create clusters
|
|
48
|
-
* @param {
|
|
48
|
+
* @param {clusterEachCallback} callback a method that takes (cluster, clusterValue, currentIndex)
|
|
49
49
|
* @returns {void}
|
|
50
50
|
* @example
|
|
51
51
|
* var geojson = turf.featureCollection([
|
|
@@ -100,16 +100,17 @@ declare function clusterEach<G extends GeometryObject, P extends GeoJsonProperti
|
|
|
100
100
|
* @param {*} [clusterValue] Value used to create cluster being processed.
|
|
101
101
|
* @param {number} [currentIndex] The index of the current element being processed in the
|
|
102
102
|
* array. Starts at index 0, if an initialValue is provided, and at index 1 otherwise.
|
|
103
|
+
* @returns {void}
|
|
103
104
|
*/
|
|
104
105
|
/**
|
|
105
106
|
* Reduce clusters in GeoJSON Features, similar to Array.reduce()
|
|
106
107
|
*
|
|
107
|
-
* @
|
|
108
|
+
* @function
|
|
108
109
|
* @param {FeatureCollection} geojson GeoJSON Features
|
|
109
110
|
* @param {string|number} property GeoJSON property key/value used to create clusters
|
|
110
|
-
* @param {
|
|
111
|
-
* @param {
|
|
112
|
-
* @returns {
|
|
111
|
+
* @param {clusterReduceCallback} callback a method that takes (previousValue, cluster, clusterValue, currentIndex)
|
|
112
|
+
* @param {any} [initialValue] Value to use as the first argument to the first call of the callback.
|
|
113
|
+
* @returns {any} The value that results from the reduction.
|
|
113
114
|
* @example
|
|
114
115
|
* var geojson = turf.featureCollection([
|
|
115
116
|
* turf.point([0, 0]),
|
package/dist/esm/index.js
CHANGED
|
@@ -2,22 +2,19 @@
|
|
|
2
2
|
import { featureEach } from "@turf/meta";
|
|
3
3
|
import { featureCollection } from "@turf/helpers";
|
|
4
4
|
function getCluster(geojson, filter) {
|
|
5
|
-
if (!geojson)
|
|
6
|
-
throw new Error("geojson is required");
|
|
5
|
+
if (!geojson) throw new Error("geojson is required");
|
|
7
6
|
if (geojson.type !== "FeatureCollection")
|
|
8
7
|
throw new Error("geojson must be a FeatureCollection");
|
|
9
8
|
if (filter === void 0 || filter === null)
|
|
10
9
|
throw new Error("filter is required");
|
|
11
10
|
var features = [];
|
|
12
11
|
featureEach(geojson, function(feature) {
|
|
13
|
-
if (applyFilter(feature.properties, filter))
|
|
14
|
-
features.push(feature);
|
|
12
|
+
if (applyFilter(feature.properties, filter)) features.push(feature);
|
|
15
13
|
});
|
|
16
14
|
return featureCollection(features);
|
|
17
15
|
}
|
|
18
16
|
function clusterEach(geojson, property, callback) {
|
|
19
|
-
if (!geojson)
|
|
20
|
-
throw new Error("geojson is required");
|
|
17
|
+
if (!geojson) throw new Error("geojson is required");
|
|
21
18
|
if (geojson.type !== "FeatureCollection")
|
|
22
19
|
throw new Error("geojson must be a FeatureCollection");
|
|
23
20
|
if (property === void 0 || property === null)
|
|
@@ -61,22 +58,19 @@ function createBins(geojson, property) {
|
|
|
61
58
|
var value = properties[property];
|
|
62
59
|
if (Object.prototype.hasOwnProperty.call(bins, value))
|
|
63
60
|
bins[value].push(i);
|
|
64
|
-
else
|
|
65
|
-
bins[value] = [i];
|
|
61
|
+
else bins[value] = [i];
|
|
66
62
|
}
|
|
67
63
|
});
|
|
68
64
|
return bins;
|
|
69
65
|
}
|
|
70
66
|
function applyFilter(properties, filter) {
|
|
71
|
-
if (properties === void 0)
|
|
72
|
-
return false;
|
|
67
|
+
if (properties === void 0) return false;
|
|
73
68
|
var filterType = typeof filter;
|
|
74
69
|
if (filterType === "number" || filterType === "string")
|
|
75
70
|
return Object.prototype.hasOwnProperty.call(properties, filter);
|
|
76
71
|
else if (Array.isArray(filter)) {
|
|
77
72
|
for (var i = 0; i < filter.length; i++) {
|
|
78
|
-
if (!applyFilter(properties, filter[i]))
|
|
79
|
-
return false;
|
|
73
|
+
if (!applyFilter(properties, filter[i])) return false;
|
|
80
74
|
}
|
|
81
75
|
return true;
|
|
82
76
|
} else {
|
|
@@ -87,16 +81,13 @@ function propertiesContainsFilter(properties, filter) {
|
|
|
87
81
|
var keys = Object.keys(filter);
|
|
88
82
|
for (var i = 0; i < keys.length; i++) {
|
|
89
83
|
var key = keys[i];
|
|
90
|
-
if (properties[key] !== filter[key])
|
|
91
|
-
return false;
|
|
84
|
+
if (properties[key] !== filter[key]) return false;
|
|
92
85
|
}
|
|
93
86
|
return true;
|
|
94
87
|
}
|
|
95
88
|
function filterProperties(properties, keys) {
|
|
96
|
-
if (!keys)
|
|
97
|
-
|
|
98
|
-
if (!keys.length)
|
|
99
|
-
return {};
|
|
89
|
+
if (!keys) return {};
|
|
90
|
+
if (!keys.length) return {};
|
|
100
91
|
var newProperties = {};
|
|
101
92
|
for (var i = 0; i < keys.length; i++) {
|
|
102
93
|
var key = keys[i];
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../index.ts"],"sourcesContent":["import {\n Feature,\n FeatureCollection,\n GeoJsonProperties,\n GeometryObject,\n} from \"geojson\";\nimport { featureEach } from \"@turf/meta\";\nimport { featureCollection } from \"@turf/helpers\";\n\n/**\n * Get Cluster\n *\n * @name getCluster\n * @param {FeatureCollection} geojson GeoJSON Features\n * @param {*} filter Filter used on GeoJSON properties to get Cluster\n * @returns {FeatureCollection} Single Cluster filtered by GeoJSON Properties\n * @example\n * var geojson = turf.featureCollection([\n * turf.point([0, 0], {'marker-symbol': 'circle'}),\n * turf.point([2, 4], {'marker-symbol': 'star'}),\n * turf.point([3, 6], {'marker-symbol': 'star'}),\n * turf.point([5, 1], {'marker-symbol': 'square'}),\n * turf.point([4, 2], {'marker-symbol': 'circle'})\n * ]);\n *\n * // Create a cluster using K-Means (adds `cluster` to GeoJSON properties)\n * var clustered = turf.clustersKmeans(geojson);\n *\n * // Retrieve first cluster (0)\n * var cluster = turf.getCluster(clustered, {cluster: 0});\n * //= cluster\n *\n * // Retrieve cluster based on custom properties\n * turf.getCluster(clustered, {'marker-symbol': 'circle'}).length;\n * //= 2\n * turf.getCluster(clustered, {'marker-symbol': 'square'}).length;\n * //= 1\n */\nfunction getCluster<\n G extends GeometryObject,\n P extends GeoJsonProperties = GeoJsonProperties,\n>(geojson: FeatureCollection<G, P>, filter: any): FeatureCollection<G, P> {\n // Validation\n if (!geojson) throw new Error(\"geojson is required\");\n if (geojson.type !== \"FeatureCollection\")\n throw new Error(\"geojson must be a FeatureCollection\");\n if (filter === undefined || filter === null)\n throw new Error(\"filter is required\");\n\n // Filter Features\n var features: Feature<G, P>[] = [];\n featureEach<G, P>(geojson, function (feature) {\n if (applyFilter(feature.properties, filter)) features.push(feature);\n });\n return featureCollection(features);\n}\n\n/**\n * Callback for clusterEach\n *\n * @callback clusterEachCallback\n * @param {FeatureCollection} [cluster] The current cluster being processed.\n * @param {*} [clusterValue] Value used to create cluster being processed.\n * @param {number} [currentIndex] The index of the current element being processed in the array.Starts at index 0\n * @returns {void}\n */\n\n/**\n * clusterEach\n *\n * @name clusterEach\n * @param {FeatureCollection} geojson GeoJSON Features\n * @param {string|number} property GeoJSON property key/value used to create clusters\n * @param {Function} callback a method that takes (cluster, clusterValue, currentIndex)\n * @returns {void}\n * @example\n * var geojson = turf.featureCollection([\n * turf.point([0, 0]),\n * turf.point([2, 4]),\n * turf.point([3, 6]),\n * turf.point([5, 1]),\n * turf.point([4, 2])\n * ]);\n *\n * // Create a cluster using K-Means (adds `cluster` to GeoJSON properties)\n * var clustered = turf.clustersKmeans(geojson);\n *\n * // Iterate over each cluster\n * turf.clusterEach(clustered, 'cluster', function (cluster, clusterValue, currentIndex) {\n * //= cluster\n * //= clusterValue\n * //= currentIndex\n * })\n *\n * // Calculate the total number of clusters\n * var total = 0\n * turf.clusterEach(clustered, 'cluster', function () {\n * total++;\n * });\n *\n * // Create an Array of all the values retrieved from the 'cluster' property\n * var values = []\n * turf.clusterEach(clustered, 'cluster', function (cluster, clusterValue) {\n * values.push(clusterValue);\n * });\n */\nfunction clusterEach<\n G extends GeometryObject,\n P extends GeoJsonProperties = GeoJsonProperties,\n>(\n geojson: FeatureCollection<G, P>,\n property: number | string,\n callback: (\n cluster: FeatureCollection<G, P>,\n clusterValue?: any,\n currentIndex?: number\n ) => void\n): void {\n // Validation\n if (!geojson) throw new Error(\"geojson is required\");\n if (geojson.type !== \"FeatureCollection\")\n throw new Error(\"geojson must be a FeatureCollection\");\n if (property === undefined || property === null)\n throw new Error(\"property is required\");\n\n // Create clusters based on property values\n var bins = createBins(geojson, property);\n var values = Object.keys(bins);\n for (var index = 0; index < values.length; index++) {\n var value = values[index];\n var bin = bins[value];\n var features = [];\n for (var i = 0; i < bin.length; i++) {\n features.push(geojson.features[bin[i]]);\n }\n callback(featureCollection(features), value, index);\n }\n}\n\n/**\n * Callback for clusterReduce\n *\n * The first time the callback function is called, the values provided as arguments depend\n * on whether the reduce method has an initialValue argument.\n *\n * If an initialValue is provided to the reduce method:\n * - The previousValue argument is initialValue.\n * - The currentValue argument is the value of the first element present in the array.\n *\n * If an initialValue is not provided:\n * - The previousValue argument is the value of the first element present in the array.\n * - The currentValue argument is the value of the second element present in the array.\n *\n * @callback clusterReduceCallback\n * @param {*} [previousValue] The accumulated value previously returned in the last invocation\n * of the callback, or initialValue, if supplied.\n * @param {FeatureCollection} [cluster] The current cluster being processed.\n * @param {*} [clusterValue] Value used to create cluster being processed.\n * @param {number} [currentIndex] The index of the current element being processed in the\n * array. Starts at index 0, if an initialValue is provided, and at index 1 otherwise.\n */\n\n/**\n * Reduce clusters in GeoJSON Features, similar to Array.reduce()\n *\n * @name clusterReduce\n * @param {FeatureCollection} geojson GeoJSON Features\n * @param {string|number} property GeoJSON property key/value used to create clusters\n * @param {Function} callback a method that takes (previousValue, cluster, clusterValue, currentIndex)\n * @param {*} [initialValue] Value to use as the first argument to the first call of the callback.\n * @returns {*} The value that results from the reduction.\n * @example\n * var geojson = turf.featureCollection([\n * turf.point([0, 0]),\n * turf.point([2, 4]),\n * turf.point([3, 6]),\n * turf.point([5, 1]),\n * turf.point([4, 2])\n * ]);\n *\n * // Create a cluster using K-Means (adds `cluster` to GeoJSON properties)\n * var clustered = turf.clustersKmeans(geojson);\n *\n * // Iterate over each cluster and perform a calculation\n * var initialValue = 0\n * turf.clusterReduce(clustered, 'cluster', function (previousValue, cluster, clusterValue, currentIndex) {\n * //=previousValue\n * //=cluster\n * //=clusterValue\n * //=currentIndex\n * return previousValue++;\n * }, initialValue);\n *\n * // Calculate the total number of clusters\n * var total = turf.clusterReduce(clustered, 'cluster', function (previousValue) {\n * return previousValue++;\n * }, 0);\n *\n * // Create an Array of all the values retrieved from the 'cluster' property\n * var values = turf.clusterReduce(clustered, 'cluster', function (previousValue, cluster, clusterValue) {\n * return previousValue.concat(clusterValue);\n * }, []);\n */\nfunction clusterReduce<\n G extends GeometryObject,\n P extends GeoJsonProperties = GeoJsonProperties,\n>(\n geojson: FeatureCollection<G, P>,\n property: number | string,\n callback: (\n previousValue: any | undefined,\n cluster: FeatureCollection<G, P>,\n clusterValue?: any,\n currentIndex?: number\n ) => void,\n initialValue?: any\n): void {\n var previousValue = initialValue;\n clusterEach(\n geojson,\n property,\n function (cluster, clusterValue, currentIndex) {\n if (currentIndex === 0 && initialValue === undefined)\n previousValue = cluster;\n else\n previousValue = callback(\n previousValue,\n cluster,\n clusterValue,\n currentIndex\n );\n }\n );\n return previousValue;\n}\n\n/**\n * Create Bins\n *\n * @private\n * @param {FeatureCollection} geojson GeoJSON Features\n * @param {string|number} property Property values are used to create bins\n * @returns {Object} bins with Feature IDs\n * @example\n * var geojson = turf.featureCollection([\n * turf.point([0, 0], {cluster: 0, foo: 'null'}),\n * turf.point([2, 4], {cluster: 1, foo: 'bar'}),\n * turf.point([5, 1], {0: 'foo'}),\n * turf.point([3, 6], {cluster: 1}),\n * ]);\n * createBins(geojson, 'cluster');\n * //= { '0': [ 0 ], '1': [ 1, 3 ] }\n */\nfunction createBins(\n geojson: FeatureCollection<any>,\n property: string | number\n) {\n var bins: Record<string, number[]> = {};\n\n featureEach(geojson, function (feature, i) {\n var properties = feature.properties || {};\n if (Object.prototype.hasOwnProperty.call(properties, String(property))) {\n var value = properties[property];\n if (Object.prototype.hasOwnProperty.call(bins, value))\n bins[value].push(i);\n else bins[value] = [i];\n }\n });\n return bins;\n}\n\n/**\n * Apply Filter\n *\n * @private\n * @param {*} properties Properties\n * @param {*} filter Filter\n * @returns {boolean} applied Filter to properties\n */\nfunction applyFilter(properties: any, filter: any) {\n if (properties === undefined) return false;\n var filterType = typeof filter;\n\n // String & Number\n if (filterType === \"number\" || filterType === \"string\")\n return Object.prototype.hasOwnProperty.call(properties, filter);\n // Array\n else if (Array.isArray(filter)) {\n for (var i = 0; i < filter.length; i++) {\n if (!applyFilter(properties, filter[i])) return false;\n }\n return true;\n // Object\n } else {\n return propertiesContainsFilter(properties, filter);\n }\n}\n\n/**\n * Properties contains filter (does not apply deepEqual operations)\n *\n * @private\n * @param {*} properties Properties\n * @param {Object} filter Filter\n * @returns {boolean} does filter equal Properties\n * @example\n * propertiesContainsFilter({foo: 'bar', cluster: 0}, {cluster: 0})\n * //= true\n * propertiesContainsFilter({foo: 'bar', cluster: 0}, {cluster: 1})\n * //= false\n */\nfunction propertiesContainsFilter(properties: any, filter: any): boolean {\n var keys = Object.keys(filter);\n for (var i = 0; i < keys.length; i++) {\n var key = keys[i];\n if (properties[key] !== filter[key]) return false;\n }\n return true;\n}\n\n/**\n * Filter Properties\n *\n * @private\n * @param {*} properties Properties\n * @param {Array<string>} keys Used to filter Properties\n * @returns {*} filtered Properties\n * @example\n * filterProperties({foo: 'bar', cluster: 0}, ['cluster'])\n * //= {cluster: 0}\n */\nfunction filterProperties(\n properties: Record<string, any>,\n keys: string[]\n): any {\n if (!keys) return {};\n if (!keys.length) return {};\n\n var newProperties: Record<string, any> = {};\n for (var i = 0; i < keys.length; i++) {\n var key = keys[i];\n if (Object.prototype.hasOwnProperty.call(properties, key))\n newProperties[key] = properties[key];\n }\n return newProperties;\n}\n\nexport {\n getCluster,\n clusterEach,\n clusterReduce,\n createBins,\n applyFilter,\n propertiesContainsFilter,\n filterProperties,\n};\n// No default export!\n"],"mappings":";AAMA,SAAS,mBAAmB;AAC5B,SAAS,yBAAyB;AA+BlC,SAAS,WAGP,SAAkC,QAAsC;AAExE,MAAI,CAAC;AAAS,UAAM,IAAI,MAAM,qBAAqB;AACnD,MAAI,QAAQ,SAAS;AACnB,UAAM,IAAI,MAAM,qCAAqC;AACvD,MAAI,WAAW,UAAa,WAAW;AACrC,UAAM,IAAI,MAAM,oBAAoB;AAGtC,MAAI,WAA4B,CAAC;AACjC,cAAkB,SAAS,SAAU,SAAS;AAC5C,QAAI,YAAY,QAAQ,YAAY,MAAM;AAAG,eAAS,KAAK,OAAO;AAAA,EACpE,CAAC;AACD,SAAO,kBAAkB,QAAQ;AACnC;AAmDA,SAAS,YAIP,SACA,UACA,UAKM;AAEN,MAAI,CAAC;AAAS,UAAM,IAAI,MAAM,qBAAqB;AACnD,MAAI,QAAQ,SAAS;AACnB,UAAM,IAAI,MAAM,qCAAqC;AACvD,MAAI,aAAa,UAAa,aAAa;AACzC,UAAM,IAAI,MAAM,sBAAsB;AAGxC,MAAI,OAAO,WAAW,SAAS,QAAQ;AACvC,MAAI,SAAS,OAAO,KAAK,IAAI;AAC7B,WAAS,QAAQ,GAAG,QAAQ,OAAO,QAAQ,SAAS;AAClD,QAAI,QAAQ,OAAO,KAAK;AACxB,QAAI,MAAM,KAAK,KAAK;AACpB,QAAI,WAAW,CAAC;AAChB,aAAS,IAAI,GAAG,IAAI,IAAI,QAAQ,KAAK;AACnC,eAAS,KAAK,QAAQ,SAAS,IAAI,CAAC,CAAC,CAAC;AAAA,IACxC;AACA,aAAS,kBAAkB,QAAQ,GAAG,OAAO,KAAK;AAAA,EACpD;AACF;AAkEA,SAAS,cAIP,SACA,UACA,UA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AAM,OAAO,GAAG;AAAG,aAAO;AAAA,EAC9C;AACA,SAAO;AACT;AAaA,SAAS,iBACP,YACA,MACK;AACL,MAAI,CAAC;AAAM,WAAO,CAAC;AACnB,MAAI,CAAC,KAAK;AAAQ,WAAO,CAAC;AAE1B,MAAI,gBAAqC,CAAC;AAC1C,WAAS,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK;AACpC,QAAI,MAAM,KAAK,CAAC;AAChB,QAAI,OAAO,UAAU,eAAe,KAAK,YAAY,GAAG;AACtD,oBAAc,GAAG,IAAI,WAAW,GAAG;AAAA,EACvC;AACA,SAAO;AACT;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../index.ts"],"sourcesContent":["import {\n Feature,\n FeatureCollection,\n GeoJsonProperties,\n GeometryObject,\n} from \"geojson\";\nimport { featureEach } from \"@turf/meta\";\nimport { featureCollection } from \"@turf/helpers\";\n\n/**\n * Get Cluster\n *\n * @function\n * @param {FeatureCollection} geojson GeoJSON Features\n * @param {*} filter Filter used on GeoJSON properties to get Cluster\n * @returns {FeatureCollection} Single Cluster filtered by GeoJSON Properties\n * @example\n * var geojson = turf.featureCollection([\n * turf.point([0, 0], {'marker-symbol': 'circle'}),\n * turf.point([2, 4], {'marker-symbol': 'star'}),\n * turf.point([3, 6], {'marker-symbol': 'star'}),\n * turf.point([5, 1], {'marker-symbol': 'square'}),\n * turf.point([4, 2], {'marker-symbol': 'circle'})\n * ]);\n *\n * // Create a cluster using K-Means (adds `cluster` to GeoJSON properties)\n * var clustered = turf.clustersKmeans(geojson);\n *\n * // Retrieve first cluster (0)\n * var cluster = turf.getCluster(clustered, {cluster: 0});\n * //= cluster\n *\n * // Retrieve cluster based on custom properties\n * turf.getCluster(clustered, {'marker-symbol': 'circle'}).length;\n * //= 2\n * turf.getCluster(clustered, {'marker-symbol': 'square'}).length;\n * //= 1\n */\nfunction getCluster<\n G extends GeometryObject,\n P extends GeoJsonProperties = GeoJsonProperties,\n>(geojson: FeatureCollection<G, P>, filter: any): FeatureCollection<G, P> {\n // Validation\n if (!geojson) throw new Error(\"geojson is required\");\n if (geojson.type !== \"FeatureCollection\")\n throw new Error(\"geojson must be a FeatureCollection\");\n if (filter === undefined || filter === null)\n throw new Error(\"filter is required\");\n\n // Filter Features\n var features: Feature<G, P>[] = [];\n featureEach<G, P>(geojson, function (feature) {\n if (applyFilter(feature.properties, filter)) features.push(feature);\n });\n return featureCollection(features);\n}\n\n/**\n * Callback for clusterEach\n *\n * @callback clusterEachCallback\n * @param {FeatureCollection} [cluster] The current cluster being processed.\n * @param {any} [clusterValue] Value used to create cluster being processed.\n * @param {number} [currentIndex] The index of the current element being processed in the array.Starts at index 0\n * @returns {void}\n */\n\n/**\n * clusterEach\n *\n * @function\n * @param {FeatureCollection} geojson GeoJSON Features\n * @param {string|number} property GeoJSON property key/value used to create clusters\n * @param {clusterEachCallback} callback a method that takes (cluster, clusterValue, currentIndex)\n * @returns {void}\n * @example\n * var geojson = turf.featureCollection([\n * turf.point([0, 0]),\n * turf.point([2, 4]),\n * turf.point([3, 6]),\n * turf.point([5, 1]),\n * turf.point([4, 2])\n * ]);\n *\n * // Create a cluster using K-Means (adds `cluster` to GeoJSON properties)\n * var clustered = turf.clustersKmeans(geojson);\n *\n * // Iterate over each cluster\n * turf.clusterEach(clustered, 'cluster', function (cluster, clusterValue, currentIndex) {\n * //= cluster\n * //= clusterValue\n * //= currentIndex\n * })\n *\n * // Calculate the total number of clusters\n * var total = 0\n * turf.clusterEach(clustered, 'cluster', function () {\n * total++;\n * });\n *\n * // Create an Array of all the values retrieved from the 'cluster' property\n * var values = []\n * turf.clusterEach(clustered, 'cluster', function (cluster, clusterValue) {\n * values.push(clusterValue);\n * });\n */\nfunction clusterEach<\n G extends GeometryObject,\n P extends GeoJsonProperties = GeoJsonProperties,\n>(\n geojson: FeatureCollection<G, P>,\n property: number | string,\n callback: (\n cluster: FeatureCollection<G, P>,\n clusterValue?: any,\n currentIndex?: number\n ) => void\n): void {\n // Validation\n if (!geojson) throw new Error(\"geojson is required\");\n if (geojson.type !== \"FeatureCollection\")\n throw new Error(\"geojson must be a FeatureCollection\");\n if (property === undefined || property === null)\n throw new Error(\"property is required\");\n\n // Create clusters based on property values\n var bins = createBins(geojson, property);\n var values = Object.keys(bins);\n for (var index = 0; index < values.length; index++) {\n var value = values[index];\n var bin = bins[value];\n var features = [];\n for (var i = 0; i < bin.length; i++) {\n features.push(geojson.features[bin[i]]);\n }\n callback(featureCollection(features), value, index);\n }\n}\n\n/**\n * Callback for clusterReduce\n *\n * The first time the callback function is called, the values provided as arguments depend\n * on whether the reduce method has an initialValue argument.\n *\n * If an initialValue is provided to the reduce method:\n * - The previousValue argument is initialValue.\n * - The currentValue argument is the value of the first element present in the array.\n *\n * If an initialValue is not provided:\n * - The previousValue argument is the value of the first element present in the array.\n * - The currentValue argument is the value of the second element present in the array.\n *\n * @callback clusterReduceCallback\n * @param {*} [previousValue] The accumulated value previously returned in the last invocation\n * of the callback, or initialValue, if supplied.\n * @param {FeatureCollection} [cluster] The current cluster being processed.\n * @param {*} [clusterValue] Value used to create cluster being processed.\n * @param {number} [currentIndex] The index of the current element being processed in the\n * array. Starts at index 0, if an initialValue is provided, and at index 1 otherwise.\n * @returns {void}\n */\n\n/**\n * Reduce clusters in GeoJSON Features, similar to Array.reduce()\n *\n * @function\n * @param {FeatureCollection} geojson GeoJSON Features\n * @param {string|number} property GeoJSON property key/value used to create clusters\n * @param {clusterReduceCallback} callback a method that takes (previousValue, cluster, clusterValue, currentIndex)\n * @param {any} [initialValue] Value to use as the first argument to the first call of the callback.\n * @returns {any} The value that results from the reduction.\n * @example\n * var geojson = turf.featureCollection([\n * turf.point([0, 0]),\n * turf.point([2, 4]),\n * turf.point([3, 6]),\n * turf.point([5, 1]),\n * turf.point([4, 2])\n * ]);\n *\n * // Create a cluster using K-Means (adds `cluster` to GeoJSON properties)\n * var clustered = turf.clustersKmeans(geojson);\n *\n * // Iterate over each cluster and perform a calculation\n * var initialValue = 0\n * turf.clusterReduce(clustered, 'cluster', function (previousValue, cluster, clusterValue, currentIndex) {\n * //=previousValue\n * //=cluster\n * //=clusterValue\n * //=currentIndex\n * return previousValue++;\n * }, initialValue);\n *\n * // Calculate the total number of clusters\n * var total = turf.clusterReduce(clustered, 'cluster', function (previousValue) {\n * return previousValue++;\n * }, 0);\n *\n * // Create an Array of all the values retrieved from the 'cluster' property\n * var values = turf.clusterReduce(clustered, 'cluster', function (previousValue, cluster, clusterValue) {\n * return previousValue.concat(clusterValue);\n * }, []);\n */\nfunction clusterReduce<\n G extends GeometryObject,\n P extends GeoJsonProperties = GeoJsonProperties,\n>(\n geojson: FeatureCollection<G, P>,\n property: number | string,\n callback: (\n previousValue: any | undefined,\n cluster: FeatureCollection<G, P>,\n clusterValue?: any,\n currentIndex?: number\n ) => void,\n initialValue?: any\n): void {\n var previousValue = initialValue;\n clusterEach(\n geojson,\n property,\n function (cluster, clusterValue, currentIndex) {\n if (currentIndex === 0 && initialValue === undefined)\n previousValue = cluster;\n else\n previousValue = callback(\n previousValue,\n cluster,\n clusterValue,\n currentIndex\n );\n }\n );\n return previousValue;\n}\n\n/**\n * Create Bins\n *\n * @private\n * @param {FeatureCollection} geojson GeoJSON Features\n * @param {string|number} property Property values are used to create bins\n * @returns {Object} bins with Feature IDs\n * @example\n * var geojson = turf.featureCollection([\n * turf.point([0, 0], {cluster: 0, foo: 'null'}),\n * turf.point([2, 4], {cluster: 1, foo: 'bar'}),\n * turf.point([5, 1], {0: 'foo'}),\n * turf.point([3, 6], {cluster: 1}),\n * ]);\n * createBins(geojson, 'cluster');\n * //= { '0': [ 0 ], '1': [ 1, 3 ] }\n */\nfunction createBins(\n geojson: FeatureCollection<any>,\n property: string | number\n) {\n var bins: Record<string, number[]> = {};\n\n featureEach(geojson, function (feature, i) {\n var properties = feature.properties || {};\n if (Object.prototype.hasOwnProperty.call(properties, String(property))) {\n var value = properties[property];\n if (Object.prototype.hasOwnProperty.call(bins, value))\n bins[value].push(i);\n else bins[value] = [i];\n }\n });\n return bins;\n}\n\n/**\n * Apply Filter\n *\n * @private\n * @param {*} properties Properties\n * @param {*} filter Filter\n * @returns {boolean} applied Filter to properties\n */\nfunction applyFilter(properties: any, filter: any) {\n if (properties === undefined) return false;\n var filterType = typeof filter;\n\n // String & Number\n if (filterType === \"number\" || filterType === \"string\")\n return Object.prototype.hasOwnProperty.call(properties, filter);\n // Array\n else if (Array.isArray(filter)) {\n for (var i = 0; i < filter.length; i++) {\n if (!applyFilter(properties, filter[i])) return false;\n }\n return true;\n // Object\n } else {\n return propertiesContainsFilter(properties, filter);\n }\n}\n\n/**\n * Properties contains filter (does not apply deepEqual operations)\n *\n * @private\n * @param {*} properties Properties\n * @param {Object} filter Filter\n * @returns {boolean} does filter equal Properties\n * @example\n * propertiesContainsFilter({foo: 'bar', cluster: 0}, {cluster: 0})\n * //= true\n * propertiesContainsFilter({foo: 'bar', cluster: 0}, {cluster: 1})\n * //= false\n */\nfunction propertiesContainsFilter(properties: any, filter: any): boolean {\n var keys = Object.keys(filter);\n for (var i = 0; i < keys.length; i++) {\n var key = keys[i];\n if (properties[key] !== filter[key]) return false;\n }\n return true;\n}\n\n/**\n * Filter Properties\n *\n * @private\n * @param {*} properties Properties\n * @param {Array<string>} keys Used to filter Properties\n * @returns {*} filtered Properties\n * @example\n * filterProperties({foo: 'bar', cluster: 0}, ['cluster'])\n * //= {cluster: 0}\n */\nfunction filterProperties(\n properties: Record<string, any>,\n keys: string[]\n): any {\n if (!keys) return {};\n if (!keys.length) return {};\n\n var newProperties: Record<string, any> = {};\n for (var i = 0; i < keys.length; i++) {\n var key = keys[i];\n if (Object.prototype.hasOwnProperty.call(properties, key))\n newProperties[key] = properties[key];\n }\n return newProperties;\n}\n\nexport {\n getCluster,\n clusterEach,\n clusterReduce,\n createBins,\n applyFilter,\n propertiesContainsFilter,\n filterProperties,\n};\n// No default export!\n"],"mappings":";AAMA,SAAS,mBAAmB;AAC5B,SAAS,yBAAyB;AA+BlC,SAAS,WAGP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,QAAI,MAAM,KAAK,CAAC;AAChB,QAAI,OAAO,UAAU,eAAe,KAAK,YAAY,GAAG;AACtD,oBAAc,GAAG,IAAI,WAAW,GAAG;AAAA,EACvC;AACA,SAAO;AACT;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@turf/clusters",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.2.0",
|
|
4
4
|
"description": "turf clusters module",
|
|
5
5
|
"author": "Turf Authors",
|
|
6
6
|
"contributors": [
|
|
@@ -57,19 +57,19 @@
|
|
|
57
57
|
},
|
|
58
58
|
"devDependencies": {
|
|
59
59
|
"@types/benchmark": "^2.1.5",
|
|
60
|
-
"@types/tape": "^4.
|
|
60
|
+
"@types/tape": "^4.13.4",
|
|
61
61
|
"benchmark": "^2.1.4",
|
|
62
62
|
"npm-run-all": "^4.1.5",
|
|
63
|
-
"tape": "^5.
|
|
64
|
-
"tsup": "^8.
|
|
65
|
-
"tsx": "^4.
|
|
66
|
-
"typescript": "^5.
|
|
63
|
+
"tape": "^5.9.0",
|
|
64
|
+
"tsup": "^8.3.5",
|
|
65
|
+
"tsx": "^4.19.2",
|
|
66
|
+
"typescript": "^5.5.4"
|
|
67
67
|
},
|
|
68
68
|
"dependencies": {
|
|
69
|
-
"@turf/helpers": "^7.
|
|
70
|
-
"@turf/meta": "^7.
|
|
69
|
+
"@turf/helpers": "^7.2.0",
|
|
70
|
+
"@turf/meta": "^7.2.0",
|
|
71
71
|
"@types/geojson": "^7946.0.10",
|
|
72
|
-
"tslib": "^2.
|
|
72
|
+
"tslib": "^2.8.1"
|
|
73
73
|
},
|
|
74
|
-
"gitHead": "
|
|
74
|
+
"gitHead": "7b0f0374c4668cd569f8904c71e2ae7d941be867"
|
|
75
75
|
}
|