@turf/distance-weight 7.0.0-alpha.2 → 7.1.0-alpha.7
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 +80 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.cts +40 -0
- package/dist/{js → esm}/index.d.ts +6 -3
- package/dist/esm/index.js +80 -0
- package/dist/esm/index.js.map +1 -0
- package/package.json +34 -29
- package/dist/es/index.js +0 -104
- package/dist/es/package.json +0 -1
- package/dist/js/index.js +0 -109
package/README.md
CHANGED
|
@@ -55,26 +55,21 @@ Returns **[Array][7]<[Array][7]<[number][3]>>** distance weight matrix.
|
|
|
55
55
|
|
|
56
56
|
[7]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array
|
|
57
57
|
|
|
58
|
-
<!-- This file is automatically generated. Please don't edit it directly
|
|
59
|
-
if you find an error, edit the source file (likely index.js), and re-run
|
|
60
|
-
./scripts/generate-readmes in the turf project. -->
|
|
58
|
+
<!-- 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. -->
|
|
61
59
|
|
|
62
60
|
---
|
|
63
61
|
|
|
64
|
-
This module is part of the [Turfjs project](
|
|
65
|
-
module collection dedicated to geographic algorithms. It is maintained in the
|
|
66
|
-
[Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create
|
|
67
|
-
PRs and issues.
|
|
62
|
+
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.
|
|
68
63
|
|
|
69
64
|
### Installation
|
|
70
65
|
|
|
71
|
-
Install this module individually:
|
|
66
|
+
Install this single module individually:
|
|
72
67
|
|
|
73
68
|
```sh
|
|
74
69
|
$ npm install @turf/distance-weight
|
|
75
70
|
```
|
|
76
71
|
|
|
77
|
-
Or install the
|
|
72
|
+
Or install the all-encompassing @turf/turf module that includes all modules as functions:
|
|
78
73
|
|
|
79
74
|
```sh
|
|
80
75
|
$ npm install @turf/turf
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});// index.ts
|
|
2
|
+
var _centroid = require('@turf/centroid');
|
|
3
|
+
var _invariant = require('@turf/invariant');
|
|
4
|
+
var _meta = require('@turf/meta');
|
|
5
|
+
function pNormDistance(feature1, feature2, p = 2) {
|
|
6
|
+
const coordinate1 = _invariant.getCoord.call(void 0, feature1);
|
|
7
|
+
const coordinate2 = _invariant.getCoord.call(void 0, feature2);
|
|
8
|
+
const xDiff = coordinate1[0] - coordinate2[0];
|
|
9
|
+
const yDiff = coordinate1[1] - coordinate2[1];
|
|
10
|
+
if (p === 1) {
|
|
11
|
+
return Math.abs(xDiff) + Math.abs(yDiff);
|
|
12
|
+
}
|
|
13
|
+
return Math.pow(Math.pow(xDiff, p) + Math.pow(yDiff, p), 1 / p);
|
|
14
|
+
}
|
|
15
|
+
function distanceWeight(fc, options) {
|
|
16
|
+
var _a, _b;
|
|
17
|
+
options = options || {};
|
|
18
|
+
const threshold = options.threshold || 1e4;
|
|
19
|
+
const p = options.p || 2;
|
|
20
|
+
const binary = (_a = options.binary) != null ? _a : false;
|
|
21
|
+
const alpha = options.alpha || -1;
|
|
22
|
+
const rowTransform = (_b = options.standardization) != null ? _b : false;
|
|
23
|
+
const features = [];
|
|
24
|
+
_meta.featureEach.call(void 0, fc, (feature) => {
|
|
25
|
+
features.push(_centroid.centroid.call(void 0, feature));
|
|
26
|
+
});
|
|
27
|
+
const weights = [];
|
|
28
|
+
for (let i = 0; i < features.length; i++) {
|
|
29
|
+
weights[i] = [];
|
|
30
|
+
}
|
|
31
|
+
for (let i = 0; i < features.length; i++) {
|
|
32
|
+
for (let j = i; j < features.length; j++) {
|
|
33
|
+
if (i === j) {
|
|
34
|
+
weights[i][j] = 0;
|
|
35
|
+
}
|
|
36
|
+
const dis = pNormDistance(features[i], features[j], p);
|
|
37
|
+
weights[i][j] = dis;
|
|
38
|
+
weights[j][i] = dis;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
for (let i = 0; i < features.length; i++) {
|
|
42
|
+
for (let j = 0; j < features.length; j++) {
|
|
43
|
+
const dis = weights[i][j];
|
|
44
|
+
if (dis === 0) {
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (binary) {
|
|
48
|
+
if (dis <= threshold) {
|
|
49
|
+
weights[i][j] = 1;
|
|
50
|
+
} else {
|
|
51
|
+
weights[i][j] = 0;
|
|
52
|
+
}
|
|
53
|
+
} else {
|
|
54
|
+
if (dis <= threshold) {
|
|
55
|
+
weights[i][j] = Math.pow(dis, alpha);
|
|
56
|
+
} else {
|
|
57
|
+
weights[i][j] = 0;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (rowTransform) {
|
|
63
|
+
for (let i = 0; i < features.length; i++) {
|
|
64
|
+
const rowSum = weights[i].reduce((sum, currentVal) => {
|
|
65
|
+
return sum + currentVal;
|
|
66
|
+
}, 0);
|
|
67
|
+
for (let j = 0; j < features.length; j++) {
|
|
68
|
+
weights[i][j] = weights[i][j] / rowSum;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return weights;
|
|
73
|
+
}
|
|
74
|
+
var turf_distance_weight_default = distanceWeight;
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
exports.default = turf_distance_weight_default; exports.distanceWeight = distanceWeight; exports.pNormDistance = pNormDistance;
|
|
80
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../index.ts"],"names":[],"mappings":";AACA,SAAS,gBAAgB;AACzB,SAAS,gBAAgB;AACzB,SAAS,mBAAmB;AAQ5B,SAA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AACxC,aAAS,IAAI,GAAG,IAAI,SAAS,QAAQ,KAAK;AACxC,YAAM,MAAc,QAAQ,CAAC,EAAE,CAAC;AAChC,UAAI,QAAQ,GAAG;AACb;AAAA,MACF;AACA,UAAI,QAAQ;AACV,YAAI,OAAO,WAAW;AACpB,kBAAQ,CAAC,EAAE,CAAC,IAAI;AAAA,QAClB,OAAO;AACL,kBAAQ,CAAC,EAAE,CAAC,IAAI;AAAA,QAClB;AAAA,MACF,OAAO;AACL,YAAI,OAAO,WAAW;AACpB,kBAAQ,CAAC,EAAE,CAAC,IAAI,KAAK,IAAI,KAAK,KAAK;AAAA,QACrC,OAAO;AACL,kBAAQ,CAAC,EAAE,CAAC,IAAI;AAAA,QAClB;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAEA,MAAI,cAAc;AAChB,aAAS,IAAI,GAAG,IAAI,SAAS,QAAQ,KAAK;AACxC,YAAM,SAAS,QAAQ,CAAC,EAAE,OAAO,CAAC,KAAa,eAAuB;AACpE,eAAO,MAAM;AAAA,MACf,GAAG,CAAC;AACJ,eAAS,IAAI,GAAG,IAAI,SAAS,QAAQ,KAAK;AACxC,gBAAQ,CAAC,EAAE,CAAC,IAAI,QAAQ,CAAC,EAAE,CAAC,IAAI;AAAA,MAClC;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AACT;AAGA,IAAO,+BAAQ","sourcesContent":["import { Feature, FeatureCollection, Point } from \"geojson\";\nimport { centroid } from \"@turf/centroid\";\nimport { getCoord } from \"@turf/invariant\";\nimport { featureEach } from \"@turf/meta\";\n\n/**\n * calcualte the Minkowski p-norm distance between two features.\n * @param feature1 point feature\n * @param feature2 point feature\n * @param p p-norm 1=<p<=infinity 1: Manhattan distance 2: Euclidean distance\n */\nfunction pNormDistance(\n feature1: Feature<Point>,\n feature2: Feature<Point>,\n p = 2\n): number {\n const coordinate1 = getCoord(feature1);\n const coordinate2 = getCoord(feature2);\n const xDiff = coordinate1[0] - coordinate2[0];\n const yDiff = coordinate1[1] - coordinate2[1];\n if (p === 1) {\n return Math.abs(xDiff) + Math.abs(yDiff);\n }\n return Math.pow(Math.pow(xDiff, p) + Math.pow(yDiff, p), 1 / p);\n}\n\n/**\n *\n *\n * @name distanceWeight\n * @param {FeatureCollection<any>} fc FeatureCollection.\n * @param {Object} [options] option object.\n * @param {number} [options.threshold=10000] If the distance between neighbor and\n * target features is greater than threshold, the weight of that neighbor is 0.\n * @param {number} [options.p=2] Minkowski p-norm distance parameter.\n * 1: Manhattan distance. 2: Euclidean distance. 1=<p<=infinity.\n * @param {boolean} [options.binary=false] If true, weight=1 if d <= threshold otherwise weight=0.\n * If false, weight=Math.pow(d, alpha).\n * @param {number} [options.alpha=-1] distance decay parameter.\n * A big value means the weight decay quickly as distance increases.\n * @param {boolean} [options.standardization=false] row standardization.\n * @returns {Array<Array<number>>} distance weight matrix.\n * @example\n *\n * var bbox = [-65, 40, -63, 42];\n * var dataset = turf.randomPoint(100, { bbox: bbox });\n * var result = turf.distanceWeight(dataset);\n */\nfunction distanceWeight(\n fc: FeatureCollection<any>,\n options?: {\n threshold?: number;\n p?: number;\n binary?: boolean;\n alpha?: number;\n standardization?: boolean;\n }\n): number[][] {\n options = options || {};\n const threshold = options.threshold || 10000;\n const p = options.p || 2;\n const binary = options.binary ?? false;\n const alpha = options.alpha || -1;\n const rowTransform = options.standardization ?? false;\n\n const features: Array<Feature<Point>> = [];\n featureEach(fc, (feature) => {\n features.push(centroid(feature));\n });\n\n // computing the distance between the features\n const weights: number[][] = [];\n for (let i = 0; i < features.length; i++) {\n weights[i] = [];\n }\n\n for (let i = 0; i < features.length; i++) {\n for (let j = i; j < features.length; j++) {\n if (i === j) {\n weights[i][j] = 0;\n }\n const dis = pNormDistance(features[i], features[j], p);\n weights[i][j] = dis;\n weights[j][i] = dis;\n }\n }\n\n // binary or distance decay\n for (let i = 0; i < features.length; i++) {\n for (let j = 0; j < features.length; j++) {\n const dis: number = weights[i][j];\n if (dis === 0) {\n continue;\n }\n if (binary) {\n if (dis <= threshold) {\n weights[i][j] = 1.0;\n } else {\n weights[i][j] = 0.0;\n }\n } else {\n if (dis <= threshold) {\n weights[i][j] = Math.pow(dis, alpha);\n } else {\n weights[i][j] = 0.0;\n }\n }\n }\n }\n\n if (rowTransform) {\n for (let i = 0; i < features.length; i++) {\n const rowSum = weights[i].reduce((sum: number, currentVal: number) => {\n return sum + currentVal;\n }, 0);\n for (let j = 0; j < features.length; j++) {\n weights[i][j] = weights[i][j] / rowSum;\n }\n }\n }\n\n return weights;\n}\n\nexport { pNormDistance, distanceWeight };\nexport default distanceWeight;\n"]}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { Feature, Point, FeatureCollection } from 'geojson';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* calcualte the Minkowski p-norm distance between two features.
|
|
5
|
+
* @param feature1 point feature
|
|
6
|
+
* @param feature2 point feature
|
|
7
|
+
* @param p p-norm 1=<p<=infinity 1: Manhattan distance 2: Euclidean distance
|
|
8
|
+
*/
|
|
9
|
+
declare function pNormDistance(feature1: Feature<Point>, feature2: Feature<Point>, p?: number): number;
|
|
10
|
+
/**
|
|
11
|
+
*
|
|
12
|
+
*
|
|
13
|
+
* @name distanceWeight
|
|
14
|
+
* @param {FeatureCollection<any>} fc FeatureCollection.
|
|
15
|
+
* @param {Object} [options] option object.
|
|
16
|
+
* @param {number} [options.threshold=10000] If the distance between neighbor and
|
|
17
|
+
* target features is greater than threshold, the weight of that neighbor is 0.
|
|
18
|
+
* @param {number} [options.p=2] Minkowski p-norm distance parameter.
|
|
19
|
+
* 1: Manhattan distance. 2: Euclidean distance. 1=<p<=infinity.
|
|
20
|
+
* @param {boolean} [options.binary=false] If true, weight=1 if d <= threshold otherwise weight=0.
|
|
21
|
+
* If false, weight=Math.pow(d, alpha).
|
|
22
|
+
* @param {number} [options.alpha=-1] distance decay parameter.
|
|
23
|
+
* A big value means the weight decay quickly as distance increases.
|
|
24
|
+
* @param {boolean} [options.standardization=false] row standardization.
|
|
25
|
+
* @returns {Array<Array<number>>} distance weight matrix.
|
|
26
|
+
* @example
|
|
27
|
+
*
|
|
28
|
+
* var bbox = [-65, 40, -63, 42];
|
|
29
|
+
* var dataset = turf.randomPoint(100, { bbox: bbox });
|
|
30
|
+
* var result = turf.distanceWeight(dataset);
|
|
31
|
+
*/
|
|
32
|
+
declare function distanceWeight(fc: FeatureCollection<any>, options?: {
|
|
33
|
+
threshold?: number;
|
|
34
|
+
p?: number;
|
|
35
|
+
binary?: boolean;
|
|
36
|
+
alpha?: number;
|
|
37
|
+
standardization?: boolean;
|
|
38
|
+
}): number[][];
|
|
39
|
+
|
|
40
|
+
export { distanceWeight as default, distanceWeight, pNormDistance };
|
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import { Feature,
|
|
1
|
+
import { Feature, Point, FeatureCollection } from 'geojson';
|
|
2
|
+
|
|
2
3
|
/**
|
|
3
4
|
* calcualte the Minkowski p-norm distance between two features.
|
|
4
5
|
* @param feature1 point feature
|
|
5
6
|
* @param feature2 point feature
|
|
6
7
|
* @param p p-norm 1=<p<=infinity 1: Manhattan distance 2: Euclidean distance
|
|
7
8
|
*/
|
|
8
|
-
|
|
9
|
+
declare function pNormDistance(feature1: Feature<Point>, feature2: Feature<Point>, p?: number): number;
|
|
9
10
|
/**
|
|
10
11
|
*
|
|
11
12
|
*
|
|
@@ -28,10 +29,12 @@ export declare function pNormDistance(feature1: Feature<Point>, feature2: Featur
|
|
|
28
29
|
* var dataset = turf.randomPoint(100, { bbox: bbox });
|
|
29
30
|
* var result = turf.distanceWeight(dataset);
|
|
30
31
|
*/
|
|
31
|
-
|
|
32
|
+
declare function distanceWeight(fc: FeatureCollection<any>, options?: {
|
|
32
33
|
threshold?: number;
|
|
33
34
|
p?: number;
|
|
34
35
|
binary?: boolean;
|
|
35
36
|
alpha?: number;
|
|
36
37
|
standardization?: boolean;
|
|
37
38
|
}): number[][];
|
|
39
|
+
|
|
40
|
+
export { distanceWeight as default, distanceWeight, pNormDistance };
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// index.ts
|
|
2
|
+
import { centroid } from "@turf/centroid";
|
|
3
|
+
import { getCoord } from "@turf/invariant";
|
|
4
|
+
import { featureEach } from "@turf/meta";
|
|
5
|
+
function pNormDistance(feature1, feature2, p = 2) {
|
|
6
|
+
const coordinate1 = getCoord(feature1);
|
|
7
|
+
const coordinate2 = getCoord(feature2);
|
|
8
|
+
const xDiff = coordinate1[0] - coordinate2[0];
|
|
9
|
+
const yDiff = coordinate1[1] - coordinate2[1];
|
|
10
|
+
if (p === 1) {
|
|
11
|
+
return Math.abs(xDiff) + Math.abs(yDiff);
|
|
12
|
+
}
|
|
13
|
+
return Math.pow(Math.pow(xDiff, p) + Math.pow(yDiff, p), 1 / p);
|
|
14
|
+
}
|
|
15
|
+
function distanceWeight(fc, options) {
|
|
16
|
+
var _a, _b;
|
|
17
|
+
options = options || {};
|
|
18
|
+
const threshold = options.threshold || 1e4;
|
|
19
|
+
const p = options.p || 2;
|
|
20
|
+
const binary = (_a = options.binary) != null ? _a : false;
|
|
21
|
+
const alpha = options.alpha || -1;
|
|
22
|
+
const rowTransform = (_b = options.standardization) != null ? _b : false;
|
|
23
|
+
const features = [];
|
|
24
|
+
featureEach(fc, (feature) => {
|
|
25
|
+
features.push(centroid(feature));
|
|
26
|
+
});
|
|
27
|
+
const weights = [];
|
|
28
|
+
for (let i = 0; i < features.length; i++) {
|
|
29
|
+
weights[i] = [];
|
|
30
|
+
}
|
|
31
|
+
for (let i = 0; i < features.length; i++) {
|
|
32
|
+
for (let j = i; j < features.length; j++) {
|
|
33
|
+
if (i === j) {
|
|
34
|
+
weights[i][j] = 0;
|
|
35
|
+
}
|
|
36
|
+
const dis = pNormDistance(features[i], features[j], p);
|
|
37
|
+
weights[i][j] = dis;
|
|
38
|
+
weights[j][i] = dis;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
for (let i = 0; i < features.length; i++) {
|
|
42
|
+
for (let j = 0; j < features.length; j++) {
|
|
43
|
+
const dis = weights[i][j];
|
|
44
|
+
if (dis === 0) {
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (binary) {
|
|
48
|
+
if (dis <= threshold) {
|
|
49
|
+
weights[i][j] = 1;
|
|
50
|
+
} else {
|
|
51
|
+
weights[i][j] = 0;
|
|
52
|
+
}
|
|
53
|
+
} else {
|
|
54
|
+
if (dis <= threshold) {
|
|
55
|
+
weights[i][j] = Math.pow(dis, alpha);
|
|
56
|
+
} else {
|
|
57
|
+
weights[i][j] = 0;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (rowTransform) {
|
|
63
|
+
for (let i = 0; i < features.length; i++) {
|
|
64
|
+
const rowSum = weights[i].reduce((sum, currentVal) => {
|
|
65
|
+
return sum + currentVal;
|
|
66
|
+
}, 0);
|
|
67
|
+
for (let j = 0; j < features.length; j++) {
|
|
68
|
+
weights[i][j] = weights[i][j] / rowSum;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return weights;
|
|
73
|
+
}
|
|
74
|
+
var turf_distance_weight_default = distanceWeight;
|
|
75
|
+
export {
|
|
76
|
+
turf_distance_weight_default as default,
|
|
77
|
+
distanceWeight,
|
|
78
|
+
pNormDistance
|
|
79
|
+
};
|
|
80
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../index.ts"],"sourcesContent":["import { Feature, FeatureCollection, Point } from \"geojson\";\nimport { centroid } from \"@turf/centroid\";\nimport { getCoord } from \"@turf/invariant\";\nimport { featureEach } from \"@turf/meta\";\n\n/**\n * calcualte the Minkowski p-norm distance between two features.\n * @param feature1 point feature\n * @param feature2 point feature\n * @param p p-norm 1=<p<=infinity 1: Manhattan distance 2: Euclidean distance\n */\nfunction pNormDistance(\n feature1: Feature<Point>,\n feature2: Feature<Point>,\n p = 2\n): number {\n const coordinate1 = getCoord(feature1);\n const coordinate2 = getCoord(feature2);\n const xDiff = coordinate1[0] - coordinate2[0];\n const yDiff = coordinate1[1] - coordinate2[1];\n if (p === 1) {\n return Math.abs(xDiff) + Math.abs(yDiff);\n }\n return Math.pow(Math.pow(xDiff, p) + Math.pow(yDiff, p), 1 / p);\n}\n\n/**\n *\n *\n * @name distanceWeight\n * @param {FeatureCollection<any>} fc FeatureCollection.\n * @param {Object} [options] option object.\n * @param {number} [options.threshold=10000] If the distance between neighbor and\n * target features is greater than threshold, the weight of that neighbor is 0.\n * @param {number} [options.p=2] Minkowski p-norm distance parameter.\n * 1: Manhattan distance. 2: Euclidean distance. 1=<p<=infinity.\n * @param {boolean} [options.binary=false] If true, weight=1 if d <= threshold otherwise weight=0.\n * If false, weight=Math.pow(d, alpha).\n * @param {number} [options.alpha=-1] distance decay parameter.\n * A big value means the weight decay quickly as distance increases.\n * @param {boolean} [options.standardization=false] row standardization.\n * @returns {Array<Array<number>>} distance weight matrix.\n * @example\n *\n * var bbox = [-65, 40, -63, 42];\n * var dataset = turf.randomPoint(100, { bbox: bbox });\n * var result = turf.distanceWeight(dataset);\n */\nfunction distanceWeight(\n fc: FeatureCollection<any>,\n options?: {\n threshold?: number;\n p?: number;\n binary?: boolean;\n alpha?: number;\n standardization?: boolean;\n }\n): number[][] {\n options = options || {};\n const threshold = options.threshold || 10000;\n const p = options.p || 2;\n const binary = options.binary ?? false;\n const alpha = options.alpha || -1;\n const rowTransform = options.standardization ?? false;\n\n const features: Array<Feature<Point>> = [];\n featureEach(fc, (feature) => {\n features.push(centroid(feature));\n });\n\n // computing the distance between the features\n const weights: number[][] = [];\n for (let i = 0; i < features.length; i++) {\n weights[i] = [];\n }\n\n for (let i = 0; i < features.length; i++) {\n for (let j = i; j < features.length; j++) {\n if (i === j) {\n weights[i][j] = 0;\n }\n const dis = pNormDistance(features[i], features[j], p);\n weights[i][j] = dis;\n weights[j][i] = dis;\n }\n }\n\n // binary or distance decay\n for (let i = 0; i < features.length; i++) {\n for (let j = 0; j < features.length; j++) {\n const dis: number = weights[i][j];\n if (dis === 0) {\n continue;\n }\n if (binary) {\n if (dis <= threshold) {\n weights[i][j] = 1.0;\n } else {\n weights[i][j] = 0.0;\n }\n } else {\n if (dis <= threshold) {\n weights[i][j] = Math.pow(dis, alpha);\n } else {\n weights[i][j] = 0.0;\n }\n }\n }\n }\n\n if (rowTransform) {\n for (let i = 0; i < features.length; i++) {\n const rowSum = weights[i].reduce((sum: number, currentVal: number) => {\n return sum + currentVal;\n }, 0);\n for (let j = 0; j < features.length; j++) {\n weights[i][j] = weights[i][j] / rowSum;\n }\n }\n }\n\n return weights;\n}\n\nexport { pNormDistance, distanceWeight };\nexport default distanceWeight;\n"],"mappings":";AACA,SAAS,gBAAgB;AACzB,SAAS,gBAAgB;AACzB,SAAS,mBAAmB;AAQ5B,SAAS,cACP,UACA,UACA,IAAI,GACI;AACR,QAAM,cAAc,SAAS,QAAQ;AACrC,QAAM,cAAc,SAAS,QAAQ;AACrC,QAAM,QAAQ,YAAY,CAAC,IAAI,YAAY,CAAC;AAC5C,QAAM,QAAQ,YAAY,CAAC,IAAI,YAAY,CAAC;AAC5C,MAAI,MAAM,GAAG;AACX,WAAO,KAAK,IAAI,KAAK,IAAI,KAAK,IAAI,KAAK;AAAA,EACzC;AACA,SAAO,KAAK,IAAI,KAAK,IAAI,OAAO,CAAC,IAAI,KAAK,IAAI,OAAO,CAAC,GAAG,IAAI,CAAC;AAChE;AAwBA,SAAS,eACP,IACA,SAOY;AAzDd;AA0DE,YAAU,WAAW,CAAC;AACtB,QAAM,YAAY,QAAQ,aAAa;AACvC,QAAM,IAAI,QAAQ,KAAK;AACvB,QAAM,UAAS,aAAQ,WAAR,YAAkB;AACjC,QAAM,QAAQ,QAAQ,SAAS;AAC/B,QAAM,gBAAe,aAAQ,oBAAR,YAA2B;AAEhD,QAAM,WAAkC,CAAC;AACzC,cAAY,IAAI,CAAC,YAAY;AAC3B,aAAS,KAAK,SAA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KAAK;AACxC,gBAAQ,CAAC,EAAE,CAAC,IAAI,QAAQ,CAAC,EAAE,CAAC,IAAI;AAAA,MAClC;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AACT;AAGA,IAAO,+BAAQ;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@turf/distance-weight",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.1.0-alpha.7+0ce6ecca0",
|
|
4
4
|
"description": "turf distance-weight module",
|
|
5
5
|
"author": "Turf Authors",
|
|
6
6
|
"contributors": [
|
|
@@ -23,47 +23,52 @@
|
|
|
23
23
|
"turf",
|
|
24
24
|
"distance-weight"
|
|
25
25
|
],
|
|
26
|
-
"
|
|
27
|
-
"
|
|
26
|
+
"type": "module",
|
|
27
|
+
"main": "dist/cjs/index.cjs",
|
|
28
|
+
"module": "dist/esm/index.js",
|
|
29
|
+
"types": "dist/esm/index.d.ts",
|
|
28
30
|
"exports": {
|
|
29
31
|
"./package.json": "./package.json",
|
|
30
32
|
".": {
|
|
31
|
-
"
|
|
32
|
-
|
|
33
|
-
|
|
33
|
+
"import": {
|
|
34
|
+
"types": "./dist/esm/index.d.ts",
|
|
35
|
+
"default": "./dist/esm/index.js"
|
|
36
|
+
},
|
|
37
|
+
"require": {
|
|
38
|
+
"types": "./dist/cjs/index.d.cts",
|
|
39
|
+
"default": "./dist/cjs/index.cjs"
|
|
40
|
+
}
|
|
34
41
|
}
|
|
35
42
|
},
|
|
36
|
-
"types": "dist/js/index.d.ts",
|
|
37
43
|
"sideEffects": false,
|
|
38
44
|
"files": [
|
|
39
45
|
"dist"
|
|
40
46
|
],
|
|
41
47
|
"scripts": {
|
|
42
|
-
"bench": "tsx bench.
|
|
43
|
-
"build": "
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"test": "npm-run-all test:*",
|
|
48
|
-
"test:tape": "tsx test.js"
|
|
48
|
+
"bench": "tsx bench.ts",
|
|
49
|
+
"build": "tsup --config ../../tsup.config.ts",
|
|
50
|
+
"docs": "tsx ../../scripts/generate-readmes.ts",
|
|
51
|
+
"test": "npm-run-all --npm-path npm test:*",
|
|
52
|
+
"test:tape": "tsx test.ts"
|
|
49
53
|
},
|
|
50
54
|
"devDependencies": {
|
|
51
|
-
"@types/
|
|
52
|
-
"
|
|
53
|
-
"
|
|
54
|
-
"
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
"
|
|
55
|
+
"@types/benchmark": "^2.1.5",
|
|
56
|
+
"@types/tape": "^4.2.32",
|
|
57
|
+
"benchmark": "^2.1.4",
|
|
58
|
+
"load-json-file": "^7.0.1",
|
|
59
|
+
"npm-run-all": "^4.1.5",
|
|
60
|
+
"tape": "^5.7.2",
|
|
61
|
+
"tsup": "^8.0.1",
|
|
62
|
+
"tsx": "^4.6.2",
|
|
63
|
+
"typescript": "^5.2.2",
|
|
64
|
+
"write-json-file": "^5.0.0"
|
|
60
65
|
},
|
|
61
66
|
"dependencies": {
|
|
62
|
-
"@turf/centroid": "^7.
|
|
63
|
-
"@turf/helpers": "^7.
|
|
64
|
-
"@turf/invariant": "^7.
|
|
65
|
-
"@turf/meta": "^7.
|
|
66
|
-
"tslib": "^2.
|
|
67
|
+
"@turf/centroid": "^7.1.0-alpha.7+0ce6ecca0",
|
|
68
|
+
"@turf/helpers": "^7.1.0-alpha.7+0ce6ecca0",
|
|
69
|
+
"@turf/invariant": "^7.1.0-alpha.7+0ce6ecca0",
|
|
70
|
+
"@turf/meta": "^7.1.0-alpha.7+0ce6ecca0",
|
|
71
|
+
"tslib": "^2.6.2"
|
|
67
72
|
},
|
|
68
|
-
"gitHead": "
|
|
73
|
+
"gitHead": "0ce6ecca05829690270fec6d6bed2003495fe0ea"
|
|
69
74
|
}
|
package/dist/es/index.js
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
import centroid from "@turf/centroid";
|
|
2
|
-
import { getCoord } from "@turf/invariant";
|
|
3
|
-
import { featureEach } from "@turf/meta";
|
|
4
|
-
/**
|
|
5
|
-
* calcualte the Minkowski p-norm distance between two features.
|
|
6
|
-
* @param feature1 point feature
|
|
7
|
-
* @param feature2 point feature
|
|
8
|
-
* @param p p-norm 1=<p<=infinity 1: Manhattan distance 2: Euclidean distance
|
|
9
|
-
*/
|
|
10
|
-
export function pNormDistance(feature1, feature2, p = 2) {
|
|
11
|
-
const coordinate1 = getCoord(feature1);
|
|
12
|
-
const coordinate2 = getCoord(feature2);
|
|
13
|
-
const xDiff = coordinate1[0] - coordinate2[0];
|
|
14
|
-
const yDiff = coordinate1[1] - coordinate2[1];
|
|
15
|
-
if (p === 1) {
|
|
16
|
-
return Math.abs(xDiff) + Math.abs(yDiff);
|
|
17
|
-
}
|
|
18
|
-
return Math.pow(Math.pow(xDiff, p) + Math.pow(yDiff, p), 1 / p);
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
* @name distanceWeight
|
|
24
|
-
* @param {FeatureCollection<any>} fc FeatureCollection.
|
|
25
|
-
* @param {Object} [options] option object.
|
|
26
|
-
* @param {number} [options.threshold=10000] If the distance between neighbor and
|
|
27
|
-
* target features is greater than threshold, the weight of that neighbor is 0.
|
|
28
|
-
* @param {number} [options.p=2] Minkowski p-norm distance parameter.
|
|
29
|
-
* 1: Manhattan distance. 2: Euclidean distance. 1=<p<=infinity.
|
|
30
|
-
* @param {boolean} [options.binary=false] If true, weight=1 if d <= threshold otherwise weight=0.
|
|
31
|
-
* If false, weight=Math.pow(d, alpha).
|
|
32
|
-
* @param {number} [options.alpha=-1] distance decay parameter.
|
|
33
|
-
* A big value means the weight decay quickly as distance increases.
|
|
34
|
-
* @param {boolean} [options.standardization=false] row standardization.
|
|
35
|
-
* @returns {Array<Array<number>>} distance weight matrix.
|
|
36
|
-
* @example
|
|
37
|
-
*
|
|
38
|
-
* var bbox = [-65, 40, -63, 42];
|
|
39
|
-
* var dataset = turf.randomPoint(100, { bbox: bbox });
|
|
40
|
-
* var result = turf.distanceWeight(dataset);
|
|
41
|
-
*/
|
|
42
|
-
export default function distanceWeight(fc, options) {
|
|
43
|
-
options = options || {};
|
|
44
|
-
const threshold = options.threshold || 10000;
|
|
45
|
-
const p = options.p || 2;
|
|
46
|
-
const binary = options.binary || false;
|
|
47
|
-
const alpha = options.alpha || -1;
|
|
48
|
-
const rowTransform = options.standardization || false;
|
|
49
|
-
const features = [];
|
|
50
|
-
featureEach(fc, (feature) => {
|
|
51
|
-
features.push(centroid(feature));
|
|
52
|
-
});
|
|
53
|
-
// computing the distance between the features
|
|
54
|
-
const weights = [];
|
|
55
|
-
for (let i = 0; i < features.length; i++) {
|
|
56
|
-
weights[i] = [];
|
|
57
|
-
}
|
|
58
|
-
for (let i = 0; i < features.length; i++) {
|
|
59
|
-
for (let j = i; j < features.length; j++) {
|
|
60
|
-
if (i === j) {
|
|
61
|
-
weights[i][j] = 0;
|
|
62
|
-
}
|
|
63
|
-
const dis = pNormDistance(features[i], features[j], p);
|
|
64
|
-
weights[i][j] = dis;
|
|
65
|
-
weights[j][i] = dis;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
// binary or distance decay
|
|
69
|
-
for (let i = 0; i < features.length; i++) {
|
|
70
|
-
for (let j = 0; j < features.length; j++) {
|
|
71
|
-
const dis = weights[i][j];
|
|
72
|
-
if (dis === 0) {
|
|
73
|
-
continue;
|
|
74
|
-
}
|
|
75
|
-
if (binary) {
|
|
76
|
-
if (dis <= threshold) {
|
|
77
|
-
weights[i][j] = 1.0;
|
|
78
|
-
}
|
|
79
|
-
else {
|
|
80
|
-
weights[i][j] = 0.0;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
else {
|
|
84
|
-
if (dis <= threshold) {
|
|
85
|
-
weights[i][j] = Math.pow(dis, alpha);
|
|
86
|
-
}
|
|
87
|
-
else {
|
|
88
|
-
weights[i][j] = 0.0;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
if (rowTransform) {
|
|
94
|
-
for (let i = 0; i < features.length; i++) {
|
|
95
|
-
const rowSum = weights[i].reduce((sum, currentVal) => {
|
|
96
|
-
return sum + currentVal;
|
|
97
|
-
}, 0);
|
|
98
|
-
for (let j = 0; j < features.length; j++) {
|
|
99
|
-
weights[i][j] = weights[i][j] / rowSum;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
return weights;
|
|
104
|
-
}
|
package/dist/es/package.json
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"type":"module"}
|
package/dist/js/index.js
DELETED
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const tslib_1 = require("tslib");
|
|
4
|
-
const centroid_1 = tslib_1.__importDefault(require("@turf/centroid"));
|
|
5
|
-
const invariant_1 = require("@turf/invariant");
|
|
6
|
-
const meta_1 = require("@turf/meta");
|
|
7
|
-
/**
|
|
8
|
-
* calcualte the Minkowski p-norm distance between two features.
|
|
9
|
-
* @param feature1 point feature
|
|
10
|
-
* @param feature2 point feature
|
|
11
|
-
* @param p p-norm 1=<p<=infinity 1: Manhattan distance 2: Euclidean distance
|
|
12
|
-
*/
|
|
13
|
-
function pNormDistance(feature1, feature2, p = 2) {
|
|
14
|
-
const coordinate1 = invariant_1.getCoord(feature1);
|
|
15
|
-
const coordinate2 = invariant_1.getCoord(feature2);
|
|
16
|
-
const xDiff = coordinate1[0] - coordinate2[0];
|
|
17
|
-
const yDiff = coordinate1[1] - coordinate2[1];
|
|
18
|
-
if (p === 1) {
|
|
19
|
-
return Math.abs(xDiff) + Math.abs(yDiff);
|
|
20
|
-
}
|
|
21
|
-
return Math.pow(Math.pow(xDiff, p) + Math.pow(yDiff, p), 1 / p);
|
|
22
|
-
}
|
|
23
|
-
exports.pNormDistance = pNormDistance;
|
|
24
|
-
/**
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
* @name distanceWeight
|
|
28
|
-
* @param {FeatureCollection<any>} fc FeatureCollection.
|
|
29
|
-
* @param {Object} [options] option object.
|
|
30
|
-
* @param {number} [options.threshold=10000] If the distance between neighbor and
|
|
31
|
-
* target features is greater than threshold, the weight of that neighbor is 0.
|
|
32
|
-
* @param {number} [options.p=2] Minkowski p-norm distance parameter.
|
|
33
|
-
* 1: Manhattan distance. 2: Euclidean distance. 1=<p<=infinity.
|
|
34
|
-
* @param {boolean} [options.binary=false] If true, weight=1 if d <= threshold otherwise weight=0.
|
|
35
|
-
* If false, weight=Math.pow(d, alpha).
|
|
36
|
-
* @param {number} [options.alpha=-1] distance decay parameter.
|
|
37
|
-
* A big value means the weight decay quickly as distance increases.
|
|
38
|
-
* @param {boolean} [options.standardization=false] row standardization.
|
|
39
|
-
* @returns {Array<Array<number>>} distance weight matrix.
|
|
40
|
-
* @example
|
|
41
|
-
*
|
|
42
|
-
* var bbox = [-65, 40, -63, 42];
|
|
43
|
-
* var dataset = turf.randomPoint(100, { bbox: bbox });
|
|
44
|
-
* var result = turf.distanceWeight(dataset);
|
|
45
|
-
*/
|
|
46
|
-
function distanceWeight(fc, options) {
|
|
47
|
-
options = options || {};
|
|
48
|
-
const threshold = options.threshold || 10000;
|
|
49
|
-
const p = options.p || 2;
|
|
50
|
-
const binary = options.binary || false;
|
|
51
|
-
const alpha = options.alpha || -1;
|
|
52
|
-
const rowTransform = options.standardization || false;
|
|
53
|
-
const features = [];
|
|
54
|
-
meta_1.featureEach(fc, (feature) => {
|
|
55
|
-
features.push(centroid_1.default(feature));
|
|
56
|
-
});
|
|
57
|
-
// computing the distance between the features
|
|
58
|
-
const weights = [];
|
|
59
|
-
for (let i = 0; i < features.length; i++) {
|
|
60
|
-
weights[i] = [];
|
|
61
|
-
}
|
|
62
|
-
for (let i = 0; i < features.length; i++) {
|
|
63
|
-
for (let j = i; j < features.length; j++) {
|
|
64
|
-
if (i === j) {
|
|
65
|
-
weights[i][j] = 0;
|
|
66
|
-
}
|
|
67
|
-
const dis = pNormDistance(features[i], features[j], p);
|
|
68
|
-
weights[i][j] = dis;
|
|
69
|
-
weights[j][i] = dis;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
// binary or distance decay
|
|
73
|
-
for (let i = 0; i < features.length; i++) {
|
|
74
|
-
for (let j = 0; j < features.length; j++) {
|
|
75
|
-
const dis = weights[i][j];
|
|
76
|
-
if (dis === 0) {
|
|
77
|
-
continue;
|
|
78
|
-
}
|
|
79
|
-
if (binary) {
|
|
80
|
-
if (dis <= threshold) {
|
|
81
|
-
weights[i][j] = 1.0;
|
|
82
|
-
}
|
|
83
|
-
else {
|
|
84
|
-
weights[i][j] = 0.0;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
else {
|
|
88
|
-
if (dis <= threshold) {
|
|
89
|
-
weights[i][j] = Math.pow(dis, alpha);
|
|
90
|
-
}
|
|
91
|
-
else {
|
|
92
|
-
weights[i][j] = 0.0;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
if (rowTransform) {
|
|
98
|
-
for (let i = 0; i < features.length; i++) {
|
|
99
|
-
const rowSum = weights[i].reduce((sum, currentVal) => {
|
|
100
|
-
return sum + currentVal;
|
|
101
|
-
}, 0);
|
|
102
|
-
for (let j = 0; j < features.length; j++) {
|
|
103
|
-
weights[i][j] = weights[i][j] / rowSum;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
return weights;
|
|
108
|
-
}
|
|
109
|
-
exports.default = distanceWeight;
|