@turf/distance-weight 6.4.0 → 7.0.0-alpha.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 +33 -16
- package/dist/es/index.js +23 -24
- package/dist/js/index.d.ts +1 -1
- package/dist/js/index.js +27 -30
- package/package.json +8 -6
package/README.md
CHANGED
|
@@ -6,29 +6,32 @@
|
|
|
6
6
|
|
|
7
7
|
calcualte the Minkowski p-norm distance between two features.
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
### Parameters
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
* `feature1` **[Feature][1]<[Point][2]>** point feature
|
|
12
|
+
* `feature2` **[Feature][1]<[Point][2]>** point feature
|
|
13
|
+
* `p` p-norm 1=\<p<=infinity 1: Manhattan distance 2: Euclidean distance (optional, default `2`)
|
|
14
|
+
|
|
15
|
+
Returns **[number][3]**
|
|
14
16
|
|
|
15
17
|
## distanceWeight
|
|
16
18
|
|
|
17
|
-
|
|
19
|
+
### Parameters
|
|
20
|
+
|
|
21
|
+
* `fc` **[FeatureCollection][4]\<any>** FeatureCollection.
|
|
22
|
+
* `options` **[Object][5]?** option object.
|
|
18
23
|
|
|
19
|
-
|
|
20
|
-
- `options` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)?** option object.
|
|
21
|
-
- `options.threshold` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** If the distance between neighbor and
|
|
24
|
+
* `options.threshold` **[number][3]** If the distance between neighbor and
|
|
22
25
|
target features is greater than threshold, the weight of that neighbor is 0. (optional, default `10000`)
|
|
23
|
-
|
|
24
|
-
1: Manhattan distance. 2: Euclidean distance. 1
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
* `options.p` **[number][3]** Minkowski p-norm distance parameter.
|
|
27
|
+
1: Manhattan distance. 2: Euclidean distance. 1=\<p<=infinity. (optional, default `2`)
|
|
28
|
+
* `options.binary` **[boolean][6]** If true, weight=1 if d <= threshold otherwise weight=0.
|
|
29
|
+
If false, weight=Math.pow(d, alpha). (optional, default `false`)
|
|
30
|
+
* `options.alpha` **[number][3]** distance decay parameter.
|
|
28
31
|
A big value means the weight decay quickly as distance increases. (optional, default `-1`)
|
|
29
|
-
|
|
32
|
+
* `options.standardization` **[boolean][6]** row standardization. (optional, default `false`)
|
|
30
33
|
|
|
31
|
-
|
|
34
|
+
### Examples
|
|
32
35
|
|
|
33
36
|
```javascript
|
|
34
37
|
var bbox = [-65, 40, -63, 42];
|
|
@@ -36,7 +39,21 @@ var dataset = turf.randomPoint(100, { bbox: bbox });
|
|
|
36
39
|
var result = turf.distanceWeight(dataset);
|
|
37
40
|
```
|
|
38
41
|
|
|
39
|
-
Returns **[Array]
|
|
42
|
+
Returns **[Array][7]<[Array][7]<[number][3]>>** distance weight matrix.
|
|
43
|
+
|
|
44
|
+
[1]: https://tools.ietf.org/html/rfc7946#section-3.2
|
|
45
|
+
|
|
46
|
+
[2]: https://tools.ietf.org/html/rfc7946#section-3.1.2
|
|
47
|
+
|
|
48
|
+
[3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
|
|
49
|
+
|
|
50
|
+
[4]: https://tools.ietf.org/html/rfc7946#section-3.3
|
|
51
|
+
|
|
52
|
+
[5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
|
|
53
|
+
|
|
54
|
+
[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
|
|
55
|
+
|
|
56
|
+
[7]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array
|
|
40
57
|
|
|
41
58
|
<!-- This file is automatically generated. Please don't edit it directly:
|
|
42
59
|
if you find an error, edit the source file (likely index.js), and re-run
|
package/dist/es/index.js
CHANGED
|
@@ -7,12 +7,11 @@ import { featureEach } from "@turf/meta";
|
|
|
7
7
|
* @param feature2 point feature
|
|
8
8
|
* @param p p-norm 1=<p<=infinity 1: Manhattan distance 2: Euclidean distance
|
|
9
9
|
*/
|
|
10
|
-
export function pNormDistance(feature1, feature2, p) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
var yDiff = coordinate1[1] - coordinate2[1];
|
|
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];
|
|
16
15
|
if (p === 1) {
|
|
17
16
|
return Math.abs(xDiff) + Math.abs(yDiff);
|
|
18
17
|
}
|
|
@@ -42,34 +41,34 @@ export function pNormDistance(feature1, feature2, p) {
|
|
|
42
41
|
*/
|
|
43
42
|
export default function distanceWeight(fc, options) {
|
|
44
43
|
options = options || {};
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
featureEach(fc,
|
|
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) => {
|
|
52
51
|
features.push(centroid(feature));
|
|
53
52
|
});
|
|
54
53
|
// computing the distance between the features
|
|
55
|
-
|
|
56
|
-
for (
|
|
54
|
+
const weights = [];
|
|
55
|
+
for (let i = 0; i < features.length; i++) {
|
|
57
56
|
weights[i] = [];
|
|
58
57
|
}
|
|
59
|
-
for (
|
|
60
|
-
for (
|
|
58
|
+
for (let i = 0; i < features.length; i++) {
|
|
59
|
+
for (let j = i; j < features.length; j++) {
|
|
61
60
|
if (i === j) {
|
|
62
61
|
weights[i][j] = 0;
|
|
63
62
|
}
|
|
64
|
-
|
|
63
|
+
const dis = pNormDistance(features[i], features[j], p);
|
|
65
64
|
weights[i][j] = dis;
|
|
66
65
|
weights[j][i] = dis;
|
|
67
66
|
}
|
|
68
67
|
}
|
|
69
68
|
// binary or distance decay
|
|
70
|
-
for (
|
|
71
|
-
for (
|
|
72
|
-
|
|
69
|
+
for (let i = 0; i < features.length; i++) {
|
|
70
|
+
for (let j = 0; j < features.length; j++) {
|
|
71
|
+
const dis = weights[i][j];
|
|
73
72
|
if (dis === 0) {
|
|
74
73
|
continue;
|
|
75
74
|
}
|
|
@@ -92,11 +91,11 @@ export default function distanceWeight(fc, options) {
|
|
|
92
91
|
}
|
|
93
92
|
}
|
|
94
93
|
if (rowTransform) {
|
|
95
|
-
for (
|
|
96
|
-
|
|
94
|
+
for (let i = 0; i < features.length; i++) {
|
|
95
|
+
const rowSum = weights[i].reduce((sum, currentVal) => {
|
|
97
96
|
return sum + currentVal;
|
|
98
97
|
}, 0);
|
|
99
|
-
for (
|
|
98
|
+
for (let j = 0; j < features.length; j++) {
|
|
100
99
|
weights[i][j] = weights[i][j] / rowSum;
|
|
101
100
|
}
|
|
102
101
|
}
|
package/dist/js/index.d.ts
CHANGED
package/dist/js/index.js
CHANGED
|
@@ -1,23 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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");
|
|
9
7
|
/**
|
|
10
8
|
* calcualte the Minkowski p-norm distance between two features.
|
|
11
9
|
* @param feature1 point feature
|
|
12
10
|
* @param feature2 point feature
|
|
13
11
|
* @param p p-norm 1=<p<=infinity 1: Manhattan distance 2: Euclidean distance
|
|
14
12
|
*/
|
|
15
|
-
function pNormDistance(feature1, feature2, p) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
var yDiff = coordinate1[1] - coordinate2[1];
|
|
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];
|
|
21
18
|
if (p === 1) {
|
|
22
19
|
return Math.abs(xDiff) + Math.abs(yDiff);
|
|
23
20
|
}
|
|
@@ -48,34 +45,34 @@ exports.pNormDistance = pNormDistance;
|
|
|
48
45
|
*/
|
|
49
46
|
function distanceWeight(fc, options) {
|
|
50
47
|
options = options || {};
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
meta_1.featureEach(fc,
|
|
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) => {
|
|
58
55
|
features.push(centroid_1.default(feature));
|
|
59
56
|
});
|
|
60
57
|
// computing the distance between the features
|
|
61
|
-
|
|
62
|
-
for (
|
|
58
|
+
const weights = [];
|
|
59
|
+
for (let i = 0; i < features.length; i++) {
|
|
63
60
|
weights[i] = [];
|
|
64
61
|
}
|
|
65
|
-
for (
|
|
66
|
-
for (
|
|
62
|
+
for (let i = 0; i < features.length; i++) {
|
|
63
|
+
for (let j = i; j < features.length; j++) {
|
|
67
64
|
if (i === j) {
|
|
68
65
|
weights[i][j] = 0;
|
|
69
66
|
}
|
|
70
|
-
|
|
67
|
+
const dis = pNormDistance(features[i], features[j], p);
|
|
71
68
|
weights[i][j] = dis;
|
|
72
69
|
weights[j][i] = dis;
|
|
73
70
|
}
|
|
74
71
|
}
|
|
75
72
|
// binary or distance decay
|
|
76
|
-
for (
|
|
77
|
-
for (
|
|
78
|
-
|
|
73
|
+
for (let i = 0; i < features.length; i++) {
|
|
74
|
+
for (let j = 0; j < features.length; j++) {
|
|
75
|
+
const dis = weights[i][j];
|
|
79
76
|
if (dis === 0) {
|
|
80
77
|
continue;
|
|
81
78
|
}
|
|
@@ -98,11 +95,11 @@ function distanceWeight(fc, options) {
|
|
|
98
95
|
}
|
|
99
96
|
}
|
|
100
97
|
if (rowTransform) {
|
|
101
|
-
for (
|
|
102
|
-
|
|
98
|
+
for (let i = 0; i < features.length; i++) {
|
|
99
|
+
const rowSum = weights[i].reduce((sum, currentVal) => {
|
|
103
100
|
return sum + currentVal;
|
|
104
101
|
}, 0);
|
|
105
|
-
for (
|
|
102
|
+
for (let j = 0; j < features.length; j++) {
|
|
106
103
|
weights[i][j] = weights[i][j] / rowSum;
|
|
107
104
|
}
|
|
108
105
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@turf/distance-weight",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "7.0.0-alpha.0",
|
|
4
4
|
"description": "turf distance-weight module",
|
|
5
5
|
"author": "Turf Authors",
|
|
6
6
|
"contributors": [
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
"type": "git",
|
|
16
16
|
"url": "git://github.com/Turfjs/turf.git"
|
|
17
17
|
},
|
|
18
|
+
"funding": "https://opencollective.com/turf",
|
|
18
19
|
"publishConfig": {
|
|
19
20
|
"access": "public"
|
|
20
21
|
},
|
|
@@ -57,10 +58,11 @@
|
|
|
57
58
|
"write-json-file": "*"
|
|
58
59
|
},
|
|
59
60
|
"dependencies": {
|
|
60
|
-
"@turf/centroid": "^
|
|
61
|
-
"@turf/helpers": "^
|
|
62
|
-
"@turf/invariant": "^
|
|
63
|
-
"@turf/meta": "^
|
|
61
|
+
"@turf/centroid": "^7.0.0-alpha.0",
|
|
62
|
+
"@turf/helpers": "^7.0.0-alpha.0",
|
|
63
|
+
"@turf/invariant": "^7.0.0-alpha.0",
|
|
64
|
+
"@turf/meta": "^7.0.0-alpha.0",
|
|
65
|
+
"tslib": "^2.3.0"
|
|
64
66
|
},
|
|
65
|
-
"gitHead": "
|
|
67
|
+
"gitHead": "0edc4c491b999e5ace770a61e1cf549f7c004189"
|
|
66
68
|
}
|