@turf/distance-weight 7.0.0-alpha.0 → 7.0.0-alpha.110

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 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](http://turfjs.org/), an open source
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 Turf module that includes it as a function:
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,85 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // index.ts
5
+ var _centroid = require('@turf/centroid');
6
+ var _invariant = require('@turf/invariant');
7
+ var _meta = require('@turf/meta');
8
+ function pNormDistance(feature1, feature2, p = 2) {
9
+ const coordinate1 = _invariant.getCoord.call(void 0, feature1);
10
+ const coordinate2 = _invariant.getCoord.call(void 0, feature2);
11
+ const xDiff = coordinate1[0] - coordinate2[0];
12
+ const yDiff = coordinate1[1] - coordinate2[1];
13
+ if (p === 1) {
14
+ return Math.abs(xDiff) + Math.abs(yDiff);
15
+ }
16
+ return Math.pow(Math.pow(xDiff, p) + Math.pow(yDiff, p), 1 / p);
17
+ }
18
+ __name(pNormDistance, "pNormDistance");
19
+ function distanceWeight(fc, options) {
20
+ var _a, _b;
21
+ options = options || {};
22
+ const threshold = options.threshold || 1e4;
23
+ const p = options.p || 2;
24
+ const binary = (_a = options.binary) != null ? _a : false;
25
+ const alpha = options.alpha || -1;
26
+ const rowTransform = (_b = options.standardization) != null ? _b : false;
27
+ const features = [];
28
+ _meta.featureEach.call(void 0, fc, (feature) => {
29
+ features.push(_centroid.centroid.call(void 0, feature));
30
+ });
31
+ const weights = [];
32
+ for (let i = 0; i < features.length; i++) {
33
+ weights[i] = [];
34
+ }
35
+ for (let i = 0; i < features.length; i++) {
36
+ for (let j = i; j < features.length; j++) {
37
+ if (i === j) {
38
+ weights[i][j] = 0;
39
+ }
40
+ const dis = pNormDistance(features[i], features[j], p);
41
+ weights[i][j] = dis;
42
+ weights[j][i] = dis;
43
+ }
44
+ }
45
+ for (let i = 0; i < features.length; i++) {
46
+ for (let j = 0; j < features.length; j++) {
47
+ const dis = weights[i][j];
48
+ if (dis === 0) {
49
+ continue;
50
+ }
51
+ if (binary) {
52
+ if (dis <= threshold) {
53
+ weights[i][j] = 1;
54
+ } else {
55
+ weights[i][j] = 0;
56
+ }
57
+ } else {
58
+ if (dis <= threshold) {
59
+ weights[i][j] = Math.pow(dis, alpha);
60
+ } else {
61
+ weights[i][j] = 0;
62
+ }
63
+ }
64
+ }
65
+ }
66
+ if (rowTransform) {
67
+ for (let i = 0; i < features.length; i++) {
68
+ const rowSum = weights[i].reduce((sum, currentVal) => {
69
+ return sum + currentVal;
70
+ }, 0);
71
+ for (let j = 0; j < features.length; j++) {
72
+ weights[i][j] = weights[i][j] / rowSum;
73
+ }
74
+ }
75
+ }
76
+ return weights;
77
+ }
78
+ __name(distanceWeight, "distanceWeight");
79
+ var turf_distance_weight_default = distanceWeight;
80
+
81
+
82
+
83
+
84
+ exports.default = turf_distance_weight_default; exports.distanceWeight = distanceWeight; exports.pNormDistance = pNormDistance;
85
+ //# 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,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;AAbS;AAqCT,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,SAAS,OAAO,CAAC;AAAA,EACjC,CAAC;AAGD,QAAM,UAAsB,CAAC;AAC7B,WAAS,IAAI,GAAG,IAAI,SAAS,QAAQ,KAAK;AACxC,YAAQ,CAAC,IAAI,CAAC;AAAA,EAChB;AAEA,WAAS,IAAI,GAAG,I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Collection, 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"]}
@@ -1,11 +1,12 @@
1
- import { Feature, FeatureCollection, Point } from "geojson";
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
- export declare function pNormDistance(feature1: Feature<Point>, feature2: Feature<Point>, p?: number): number;
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
- export default function distanceWeight(fc: FeatureCollection<any>, options?: {
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,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 };
@@ -0,0 +1,85 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // index.ts
5
+ import { centroid } from "@turf/centroid";
6
+ import { getCoord } from "@turf/invariant";
7
+ import { featureEach } from "@turf/meta";
8
+ function pNormDistance(feature1, feature2, p = 2) {
9
+ const coordinate1 = getCoord(feature1);
10
+ const coordinate2 = getCoord(feature2);
11
+ const xDiff = coordinate1[0] - coordinate2[0];
12
+ const yDiff = coordinate1[1] - coordinate2[1];
13
+ if (p === 1) {
14
+ return Math.abs(xDiff) + Math.abs(yDiff);
15
+ }
16
+ return Math.pow(Math.pow(xDiff, p) + Math.pow(yDiff, p), 1 / p);
17
+ }
18
+ __name(pNormDistance, "pNormDistance");
19
+ function distanceWeight(fc, options) {
20
+ var _a, _b;
21
+ options = options || {};
22
+ const threshold = options.threshold || 1e4;
23
+ const p = options.p || 2;
24
+ const binary = (_a = options.binary) != null ? _a : false;
25
+ const alpha = options.alpha || -1;
26
+ const rowTransform = (_b = options.standardization) != null ? _b : false;
27
+ const features = [];
28
+ featureEach(fc, (feature) => {
29
+ features.push(centroid(feature));
30
+ });
31
+ const weights = [];
32
+ for (let i = 0; i < features.length; i++) {
33
+ weights[i] = [];
34
+ }
35
+ for (let i = 0; i < features.length; i++) {
36
+ for (let j = i; j < features.length; j++) {
37
+ if (i === j) {
38
+ weights[i][j] = 0;
39
+ }
40
+ const dis = pNormDistance(features[i], features[j], p);
41
+ weights[i][j] = dis;
42
+ weights[j][i] = dis;
43
+ }
44
+ }
45
+ for (let i = 0; i < features.length; i++) {
46
+ for (let j = 0; j < features.length; j++) {
47
+ const dis = weights[i][j];
48
+ if (dis === 0) {
49
+ continue;
50
+ }
51
+ if (binary) {
52
+ if (dis <= threshold) {
53
+ weights[i][j] = 1;
54
+ } else {
55
+ weights[i][j] = 0;
56
+ }
57
+ } else {
58
+ if (dis <= threshold) {
59
+ weights[i][j] = Math.pow(dis, alpha);
60
+ } else {
61
+ weights[i][j] = 0;
62
+ }
63
+ }
64
+ }
65
+ }
66
+ if (rowTransform) {
67
+ for (let i = 0; i < features.length; i++) {
68
+ const rowSum = weights[i].reduce((sum, currentVal) => {
69
+ return sum + currentVal;
70
+ }, 0);
71
+ for (let j = 0; j < features.length; j++) {
72
+ weights[i][j] = weights[i][j] / rowSum;
73
+ }
74
+ }
75
+ }
76
+ return weights;
77
+ }
78
+ __name(distanceWeight, "distanceWeight");
79
+ var turf_distance_weight_default = distanceWeight;
80
+ export {
81
+ turf_distance_weight_default as default,
82
+ distanceWeight,
83
+ pNormDistance
84
+ };
85
+ //# sourceMappingURL=index.mjs.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;AACz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,EACF;AAGA,WAAS,IAAI,GAAG,IAAI,SAAS,QAAQ,KAAK;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;AA1ES;AA6ET,IAAO,+BAAQ;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@turf/distance-weight",
3
- "version": "7.0.0-alpha.0",
3
+ "version": "7.0.0-alpha.110+1411d63a7",
4
4
  "description": "turf distance-weight module",
5
5
  "author": "Turf Authors",
6
6
  "contributors": [
@@ -23,46 +23,52 @@
23
23
  "turf",
24
24
  "distance-weight"
25
25
  ],
26
- "main": "dist/js/index.js",
27
- "module": "dist/es/index.js",
26
+ "type": "commonjs",
27
+ "main": "dist/cjs/index.cjs",
28
+ "module": "dist/esm/index.mjs",
29
+ "types": "dist/cjs/index.d.ts",
28
30
  "exports": {
29
31
  "./package.json": "./package.json",
30
32
  ".": {
31
- "import": "./dist/es/index.js",
32
- "require": "./dist/js/index.js"
33
+ "import": {
34
+ "types": "./dist/esm/index.d.mts",
35
+ "default": "./dist/esm/index.mjs"
36
+ },
37
+ "require": {
38
+ "types": "./dist/cjs/index.d.ts",
39
+ "default": "./dist/cjs/index.cjs"
40
+ }
33
41
  }
34
42
  },
35
- "types": "dist/js/index.d.ts",
36
43
  "sideEffects": false,
37
44
  "files": [
38
45
  "dist"
39
46
  ],
40
47
  "scripts": {
41
- "bench": "ts-node bench.js",
42
- "build": "npm-run-all build:*",
43
- "build:es": "tsc --outDir dist/es --module esnext --declaration false && echo '{\"type\":\"module\"}' > dist/es/package.json",
44
- "build:js": "tsc",
45
- "docs": "node ../../scripts/generate-readmes",
46
- "test": "npm-run-all test:*",
47
- "test:tape": "ts-node -r esm 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"
48
53
  },
49
54
  "devDependencies": {
50
- "@types/tape": "*",
51
- "benchmark": "*",
52
- "load-json-file": "*",
53
- "npm-run-all": "*",
54
- "tape": "*",
55
- "ts-node": "*",
56
- "tslint": "*",
57
- "typescript": "*",
58
- "write-json-file": "*"
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"
59
65
  },
60
66
  "dependencies": {
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"
67
+ "@turf/centroid": "^7.0.0-alpha.110+1411d63a7",
68
+ "@turf/helpers": "^7.0.0-alpha.110+1411d63a7",
69
+ "@turf/invariant": "^7.0.0-alpha.110+1411d63a7",
70
+ "@turf/meta": "^7.0.0-alpha.110+1411d63a7",
71
+ "tslib": "^2.6.2"
66
72
  },
67
- "gitHead": "0edc4c491b999e5ace770a61e1cf549f7c004189"
73
+ "gitHead": "1411d63a74c275c9216fe48e9d3cb2d48a359068"
68
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
- }
@@ -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;