@turf/directional-mean 7.0.0-alpha.1 → 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
@@ -56,26 +56,21 @@ Returns **[DirectionalMeanLine][6]** Directional Mean Line
56
56
 
57
57
  [6]: #directionalmeanline
58
58
 
59
- <!-- This file is automatically generated. Please don't edit it directly:
60
- if you find an error, edit the source file (likely index.js), and re-run
61
- ./scripts/generate-readmes in the turf project. -->
59
+ <!-- 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. -->
62
60
 
63
61
  ---
64
62
 
65
- This module is part of the [Turfjs project](http://turfjs.org/), an open source
66
- module collection dedicated to geographic algorithms. It is maintained in the
67
- [Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create
68
- PRs and issues.
63
+ 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.
69
64
 
70
65
  ### Installation
71
66
 
72
- Install this module individually:
67
+ Install this single module individually:
73
68
 
74
69
  ```sh
75
70
  $ npm install @turf/directional-mean
76
71
  ```
77
72
 
78
- Or install the Turf module that includes it as a function:
73
+ Or install the all-encompassing @turf/turf module that includes all modules as functions:
79
74
 
80
75
  ```sh
81
76
  $ npm install @turf/turf
@@ -0,0 +1,211 @@
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 _bearing = require('@turf/bearing');
6
+ var _centroid = require('@turf/centroid');
7
+ var _destination = require('@turf/destination');
8
+ var _helpers = require('@turf/helpers');
9
+ var _invariant = require('@turf/invariant');
10
+ var _length = require('@turf/length');
11
+ var _meta = require('@turf/meta');
12
+ function directionalMean(lines, options = {}) {
13
+ var _a;
14
+ const isPlanar = !!options.planar;
15
+ const isSegment = (_a = options.segment) != null ? _a : false;
16
+ let sigmaSin = 0;
17
+ let sigmaCos = 0;
18
+ let countOfLines = 0;
19
+ let sumOfLen = 0;
20
+ const centroidList = [];
21
+ if (isSegment) {
22
+ _meta.segmentEach.call(void 0, lines, (currentSegment) => {
23
+ const [sin1, cos1] = getCosAndSin(
24
+ currentSegment.geometry.coordinates,
25
+ isPlanar
26
+ );
27
+ const lenOfLine = getLengthOfLineString(currentSegment, isPlanar);
28
+ if (isNaN(sin1) || isNaN(cos1)) {
29
+ return;
30
+ } else {
31
+ sigmaSin += sin1;
32
+ sigmaCos += cos1;
33
+ countOfLines += 1;
34
+ sumOfLen += lenOfLine;
35
+ centroidList.push(_centroid.centroid.call(void 0, currentSegment));
36
+ }
37
+ });
38
+ } else {
39
+ _meta.featureEach.call(void 0, lines, (currentFeature) => {
40
+ if (currentFeature.geometry.type !== "LineString") {
41
+ throw new Error("shold to support MultiLineString?");
42
+ }
43
+ const [sin1, cos1] = getCosAndSin(
44
+ currentFeature.geometry.coordinates,
45
+ isPlanar
46
+ );
47
+ const lenOfLine = getLengthOfLineString(currentFeature, isPlanar);
48
+ if (isNaN(sin1) || isNaN(cos1)) {
49
+ return;
50
+ } else {
51
+ sigmaSin += sin1;
52
+ sigmaCos += cos1;
53
+ countOfLines += 1;
54
+ sumOfLen += lenOfLine;
55
+ centroidList.push(_centroid.centroid.call(void 0, currentFeature));
56
+ }
57
+ });
58
+ }
59
+ const cartesianAngle = getAngleBySinAndCos(sigmaSin, sigmaCos);
60
+ const bearingAngle = bearingToCartesian(cartesianAngle);
61
+ const circularVariance = getCircularVariance(
62
+ sigmaSin,
63
+ sigmaCos,
64
+ countOfLines
65
+ );
66
+ const averageLength = sumOfLen / countOfLines;
67
+ const centroidOfLines = _centroid.centroid.call(void 0, _helpers.featureCollection.call(void 0, centroidList));
68
+ const [averageX, averageY] = _invariant.getCoord.call(void 0, centroidOfLines);
69
+ let meanLinestring;
70
+ if (isPlanar) {
71
+ meanLinestring = getMeanLineString(
72
+ [averageX, averageY],
73
+ cartesianAngle,
74
+ averageLength,
75
+ isPlanar
76
+ );
77
+ } else {
78
+ meanLinestring = getMeanLineString(
79
+ [averageX, averageY],
80
+ bearingAngle,
81
+ averageLength,
82
+ isPlanar
83
+ );
84
+ }
85
+ return _helpers.lineString.call(void 0, meanLinestring, {
86
+ averageLength,
87
+ averageX,
88
+ averageY,
89
+ bearingAngle,
90
+ cartesianAngle,
91
+ circularVariance,
92
+ countOfLines
93
+ });
94
+ }
95
+ __name(directionalMean, "directionalMean");
96
+ function euclideanDistance(coords) {
97
+ const [x0, y0] = coords[0];
98
+ const [x1, y1] = coords[1];
99
+ const dx = x1 - x0;
100
+ const dy = y1 - y0;
101
+ return Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
102
+ }
103
+ __name(euclideanDistance, "euclideanDistance");
104
+ function getLengthOfLineString(line, isPlanar) {
105
+ if (isPlanar) {
106
+ return _meta.segmentReduce.call(void 0,
107
+ line,
108
+ (previousValue, segment) => {
109
+ const coords = segment.geometry.coordinates;
110
+ return previousValue + euclideanDistance(coords);
111
+ },
112
+ 0
113
+ );
114
+ } else {
115
+ return _length.length.call(void 0, line, {
116
+ units: "meters"
117
+ });
118
+ }
119
+ }
120
+ __name(getLengthOfLineString, "getLengthOfLineString");
121
+ function bearingToCartesian(angle) {
122
+ let result = 90 - angle;
123
+ if (result > 180) {
124
+ result -= 360;
125
+ }
126
+ return result;
127
+ }
128
+ __name(bearingToCartesian, "bearingToCartesian");
129
+ function getCosAndSin(coordinates, isPlanar) {
130
+ const beginPoint = coordinates[0];
131
+ const endPoint = coordinates[coordinates.length - 1];
132
+ if (isPlanar) {
133
+ const [x0, y0] = beginPoint;
134
+ const [x1, y1] = endPoint;
135
+ const dx = x1 - x0;
136
+ const dy = y1 - y0;
137
+ const h = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
138
+ if (h < 1e-9) {
139
+ return [NaN, NaN];
140
+ }
141
+ const sin1 = dy / h;
142
+ const cos1 = dx / h;
143
+ return [sin1, cos1];
144
+ } else {
145
+ const angle = bearingToCartesian(_bearing.bearing.call(void 0, beginPoint, endPoint));
146
+ const radian = angle * Math.PI / 180;
147
+ return [Math.sin(radian), Math.cos(radian)];
148
+ }
149
+ }
150
+ __name(getCosAndSin, "getCosAndSin");
151
+ function getAngleBySinAndCos(sin1, cos1) {
152
+ let angle = 0;
153
+ if (Math.abs(cos1) < 1e-9) {
154
+ angle = 90;
155
+ } else {
156
+ angle = Math.atan2(sin1, cos1) * 180 / Math.PI;
157
+ }
158
+ if (sin1 >= 0) {
159
+ if (cos1 < 0) {
160
+ angle += 180;
161
+ }
162
+ } else {
163
+ if (cos1 < 0) {
164
+ angle -= 180;
165
+ }
166
+ }
167
+ return angle;
168
+ }
169
+ __name(getAngleBySinAndCos, "getAngleBySinAndCos");
170
+ function getCircularVariance(sin1, cos1, len) {
171
+ if (len === 0) {
172
+ throw new Error("the size of the features set must be greater than 0");
173
+ }
174
+ return 1 - Math.sqrt(Math.pow(sin1, 2) + Math.pow(cos1, 2)) / len;
175
+ }
176
+ __name(getCircularVariance, "getCircularVariance");
177
+ function getMeanLineString(centroidOfLine, angle, lenOfLine, isPlanar) {
178
+ if (isPlanar) {
179
+ const [averageX, averageY] = centroidOfLine;
180
+ let beginX;
181
+ let beginY;
182
+ let endX;
183
+ let endY;
184
+ const r = angle * Math.PI / 180;
185
+ const sin = Math.sin(r);
186
+ const cos = Math.cos(r);
187
+ beginX = averageX - lenOfLine / 2 * cos;
188
+ beginY = averageY - lenOfLine / 2 * sin;
189
+ endX = averageX + lenOfLine / 2 * cos;
190
+ endY = averageY + lenOfLine / 2 * sin;
191
+ return [
192
+ [beginX, beginY],
193
+ [endX, endY]
194
+ ];
195
+ } else {
196
+ const end = _destination.destination.call(void 0, _helpers.point.call(void 0, centroidOfLine), lenOfLine / 2, angle, {
197
+ units: "meters"
198
+ });
199
+ const begin = _destination.destination.call(void 0, _helpers.point.call(void 0, centroidOfLine), -lenOfLine / 2, angle, {
200
+ units: "meters"
201
+ });
202
+ return [_invariant.getCoord.call(void 0, begin), _invariant.getCoord.call(void 0, end)];
203
+ }
204
+ }
205
+ __name(getMeanLineString, "getMeanLineString");
206
+ var turf_directional_mean_default = directionalMean;
207
+
208
+
209
+
210
+ exports.default = turf_directional_mean_default; exports.directionalMean = directionalMean;
211
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../index.ts"],"names":[],"mappings":";;;;AACA,SAAS,eAAe;AACxB,SAAS,gBAAgB;AACzB,SAAS,mBAAmB;AAC5B,SAAS,mBAAmB,YAAY,aAAa;AACrD,SAAS,gBAAgB;AACzB,SAAS,cAAc;AACvB,SAAS,aAAa,aAAa,qBAAqB;AA8CxD,SAAS,gBACP,OACA,UAGI,CAAC,GACgB;AA3DvB;AA4DE,QAAM,WAAW,CAAC,CAAC,QAAQ;AAC3B,QAAM,aAAqB,aAAQ,YAAR,YAAmB;AAC9C,MAAI,WAAW;AACf,MAAI,WAAW;AACf,MAAI,eAAe;AACnB,MAAI,WAAW;AACf,QAAM,eAAsC,CAAC;AAE7C,MAAI,WAAW;AACb,gBAAY,OAAO,CAAC,mBAAwB;AAE1C,YAAM,CAAC,MAAM,IAAI,IAAsB;AAAA,QACrC,eAAe,SAAS;AAAA,QACxB;AAAA,MACF;AACA,YAAM,YAAY,sBAAsB,gBAAgB,QAAQ;AAChE,UAAI,MAAM,IAAI,KAAK,MAAM,IAAI,GAAG;AAC9B;AAAA,MACF,OAAO;AACL,oBAAY;AACZ,oBAAY;AACZ,wBAAgB;AAChB,oBAAY;AACZ,qBAAa,KAAK,SAAS,cAAc,CAAC;AAAA,MAC5C;AAAA,IACF,CAAC;AAAA,EAEH,OAAO;AAEL,gBAAY,OAAO,CAAC,mBAAwC;AAC1D,UAAI,eAAe,SAAS,SAAS,cAAc;AACjD,cAAM,IAAI,MAAM,mCAAm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{ Feature, FeatureCollection, LineString, Point } from \"geojson\";\nimport { bearing } from \"@turf/bearing\";\nimport { centroid } from \"@turf/centroid\";\nimport { destination } from \"@turf/destination\";\nimport { featureCollection, lineString, point } from \"@turf/helpers\";\nimport { getCoord } from \"@turf/invariant\";\nimport { length } from \"@turf/length\";\nimport { featureEach, segmentEach, segmentReduce } from \"@turf/meta\";\n\ninterface DirectionalMeanLine extends Feature<LineString> {\n properties: {\n cartesianAngle: number;\n bearingAngle: number;\n circularVariance: number;\n averageX: number;\n averageY: number;\n averageLength: number;\n countOfLines: number;\n [key: string]: any;\n };\n}\n\n/**\n * @typedef {Object} DirectionalMeanLine\n * @property {number} cartesianAngle the mean angle of all lines. (measure from due earth counterclockwise).\n * @property {number} bearingAngle the mean angle of all lines. (bearing).\n * @property {number} circularVariance the extent to which features all point in the same direction.\n * the value ranges 0-1, the bigger the value, the more variation in directions between lines.\n * @property {number} averageX the centroid of all lines.\n * @property {number} averageY the centroid of all line.\n * @property {number} averageLength the average length of line.\n * @property {number} countOfLines the count of features.\n */\n\n/**\n * This module calculate the average angle of a set of lines, measuring the trend of it.\n * It can be used in both project coordinate system and geography coordinate system.\n * It can handle segments of line or the whole line.\n * @name directionalMean\n * @param {FeatureCollection<LineString>} lines\n * @param {object} [options={}]\n * @param {boolean} [options.planar=true] whether the spatial reference system is projected or geographical.\n * @param {boolean} [options.segment=false] whether treat a LineString as a whole or a set of segments.\n * @returns {DirectionalMeanLine} Directional Mean Line\n * @example\n *\n * var lines = turf.lineStrings([\n * [[110, 45], [120, 50]],\n * [[100, 50], [115, 55]],\n * ])\n * var directionalMeanLine = turf.directionalMean(lines);\n * // => directionalMeanLine\n */\nfunction directionalMean(\n lines: FeatureCollection<LineString>,\n options: {\n planar?: boolean;\n segment?: boolean;\n } = {}\n): DirectionalMeanLine {\n const isPlanar = !!options.planar; // you can't use options.planar || true here.\n const isSegment: boolean = options.segment ?? false;\n let sigmaSin = 0;\n let sigmaCos = 0;\n let countOfLines = 0;\n let sumOfLen = 0;\n const centroidList: Array<Feature<Point>> = [];\n\n if (isSegment) {\n segmentEach(lines, (currentSegment: any) => {\n // todo fix turf-meta's declaration file\n const [sin1, cos1]: [number, number] = getCosAndSin(\n currentSegment.geometry.coordinates,\n isPlanar\n );\n const lenOfLine = getLengthOfLineString(currentSegment, isPlanar);\n if (isNaN(sin1) || isNaN(cos1)) {\n return;\n } else {\n sigmaSin += sin1;\n sigmaCos += cos1;\n countOfLines += 1;\n sumOfLen += lenOfLine;\n centroidList.push(centroid(currentSegment));\n }\n });\n // planar and segment\n } else {\n // planar and non-segment\n featureEach(lines, (currentFeature: Feature<LineString>) => {\n if (currentFeature.geometry.type !== \"LineString\") {\n throw new Error(\"shold to support MultiLineString?\");\n }\n const [sin1, cos1]: [number, number] = getCosAndSin(\n currentFeature.geometry.coordinates,\n isPlanar\n );\n const lenOfLine = getLengthOfLineString(currentFeature, isPlanar);\n if (isNaN(sin1) || isNaN(cos1)) {\n return;\n } else {\n sigmaSin += sin1;\n sigmaCos += cos1;\n countOfLines += 1;\n sumOfLen += lenOfLine;\n centroidList.push(centroid(currentFeature));\n }\n });\n }\n\n const cartesianAngle: number = getAngleBySinAndCos(sigmaSin, sigmaCos);\n const bearingAngle: number = bearingToCartesian(cartesianAngle);\n const circularVariance = getCircularVariance(\n sigmaSin,\n sigmaCos,\n countOfLines\n );\n const averageLength = sumOfLen / countOfLines;\n const centroidOfLines = centroid(featureCollection(centroidList));\n const [averageX, averageY]: number[] = getCoord(centroidOfLines);\n let meanLinestring;\n if (isPlanar) {\n meanLinestring = getMeanLineString(\n [averageX, averageY],\n cartesianAngle,\n averageLength,\n isPlanar\n );\n } else {\n meanLinestring = getMeanLineString(\n [averageX, averageY],\n bearingAngle,\n averageLength,\n isPlanar\n );\n }\n\n return lineString(meanLinestring, {\n averageLength,\n averageX,\n averageY,\n bearingAngle,\n cartesianAngle,\n circularVariance,\n countOfLines,\n });\n}\n\n/**\n * get euclidean distance between two points.\n * @private\n * @name euclideanDistance\n * @param coords\n */\nfunction euclideanDistance(coords: number[][]) {\n const [x0, y0]: number[] = coords[0];\n const [x1, y1]: number[] = coords[1];\n const dx: number = x1 - x0;\n const dy: number = y1 - y0;\n return Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));\n}\n\n/**\n * get the length of a LineString, both in projected or geographical coordinate system.\n * @private\n * @name getLengthOfLineString\n * @param {Feature<LineString>} line\n * @param {boolean} isPlanar\n */\nfunction getLengthOfLineString(line: Feature<LineString>, isPlanar: boolean) {\n if (isPlanar) {\n return segmentReduce<number>(\n line,\n (previousValue?: number, segment?: Feature<LineString>): number => {\n const coords = segment!.geometry.coordinates; // the signatrue of segmentReduce has problem ?\n return previousValue! + euclideanDistance(coords);\n },\n 0\n );\n } else {\n return length(line, {\n units: \"meters\",\n });\n }\n}\n\n/**\n * bearing to xy(from due earth counterclockwise 0-180)\n * convert between two forms\n * @private\n * @name bearingToCartesian\n * @param angle\n */\nfunction bearingToCartesian(angle: number): number {\n let result = 90 - angle;\n if (result > 180) {\n result -= 360;\n }\n return result;\n}\n\n/**\n * @private\n * @name getCosAndSin\n * @param {Array<Array<number>>} coordinates\n * @returns {Array<number>} [cos, sin]\n */\nfunction getCosAndSin(\n coordinates: number[][],\n isPlanar: boolean\n): [number, number] {\n const beginPoint: number[] = coordinates[0];\n const endPoint: number[] = coordinates[coordinates.length - 1];\n if (isPlanar) {\n const [x0, y0]: number[] = beginPoint;\n const [x1, y1]: number[] = endPoint;\n const dx: number = x1 - x0;\n const dy: number = y1 - y0;\n const h = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));\n if (h < 0.000000001) {\n return [NaN, NaN];\n }\n const sin1 = dy / h;\n const cos1 = dx / h;\n return [sin1, cos1];\n } else {\n const angle = bearingToCartesian(bearing(beginPoint, endPoint));\n const radian = (angle * Math.PI) / 180;\n return [Math.sin(radian), Math.cos(radian)];\n }\n}\n\nfunction getAngleBySinAndCos(sin1: number, cos1: number): number {\n let angle = 0;\n if (Math.abs(cos1) < 0.000000001) {\n angle = 90;\n } else {\n angle = (Math.atan2(sin1, cos1) * 180) / Math.PI;\n }\n if (sin1 >= 0) {\n if (cos1 < 0) {\n angle += 180;\n }\n } else {\n if (cos1 < 0) {\n angle -= 180;\n }\n }\n return angle;\n}\n\nfunction getCircularVariance(sin1: number, cos1: number, len: number) {\n if (len === 0) {\n throw new Error(\"the size of the features set must be greater than 0\");\n }\n return 1 - Math.sqrt(Math.pow(sin1, 2) + Math.pow(cos1, 2)) / len;\n}\n\nfunction getMeanLineString(\n centroidOfLine: number[],\n angle: number,\n lenOfLine: number,\n isPlanar: boolean\n) {\n if (isPlanar) {\n const [averageX, averageY]: number[] = centroidOfLine;\n let beginX: number;\n let beginY: number;\n let endX: number;\n let endY: number;\n const r: number = (angle * Math.PI) / 180;\n const sin: number = Math.sin(r);\n const cos: number = Math.cos(r);\n beginX = averageX - (lenOfLine / 2) * cos;\n beginY = averageY - (lenOfLine / 2) * sin;\n endX = averageX + (lenOfLine / 2) * cos;\n endY = averageY + (lenOfLine / 2) * sin;\n return [\n [beginX, beginY],\n [endX, endY],\n ];\n } else {\n const end = destination(point(centroidOfLine), lenOfLine / 2, angle, {\n units: \"meters\",\n });\n const begin = destination(point(centroidOfLine), -lenOfLine / 2, angle, {\n units: \"meters\",\n });\n return [getCoord(begin), getCoord(end)];\n }\n}\n\nexport { directionalMean, DirectionalMeanLine };\nexport default directionalMean;\n"]}
@@ -1,5 +1,6 @@
1
- import { Feature, FeatureCollection, LineString } from "geojson";
2
- export interface DirectionalMeanLine extends Feature<LineString> {
1
+ import { Feature, LineString, FeatureCollection } from 'geojson';
2
+
3
+ interface DirectionalMeanLine extends Feature<LineString> {
3
4
  properties: {
4
5
  cartesianAngle: number;
5
6
  bearingAngle: number;
@@ -41,7 +42,9 @@ export interface DirectionalMeanLine extends Feature<LineString> {
41
42
  * var directionalMeanLine = turf.directionalMean(lines);
42
43
  * // => directionalMeanLine
43
44
  */
44
- export default function directionalMean(lines: FeatureCollection<LineString>, options?: {
45
+ declare function directionalMean(lines: FeatureCollection<LineString>, options?: {
45
46
  planar?: boolean;
46
47
  segment?: boolean;
47
48
  }): DirectionalMeanLine;
49
+
50
+ export { type DirectionalMeanLine, directionalMean as default, directionalMean };
@@ -0,0 +1,50 @@
1
+ import { Feature, LineString, FeatureCollection } from 'geojson';
2
+
3
+ interface DirectionalMeanLine extends Feature<LineString> {
4
+ properties: {
5
+ cartesianAngle: number;
6
+ bearingAngle: number;
7
+ circularVariance: number;
8
+ averageX: number;
9
+ averageY: number;
10
+ averageLength: number;
11
+ countOfLines: number;
12
+ [key: string]: any;
13
+ };
14
+ }
15
+ /**
16
+ * @typedef {Object} DirectionalMeanLine
17
+ * @property {number} cartesianAngle the mean angle of all lines. (measure from due earth counterclockwise).
18
+ * @property {number} bearingAngle the mean angle of all lines. (bearing).
19
+ * @property {number} circularVariance the extent to which features all point in the same direction.
20
+ * the value ranges 0-1, the bigger the value, the more variation in directions between lines.
21
+ * @property {number} averageX the centroid of all lines.
22
+ * @property {number} averageY the centroid of all line.
23
+ * @property {number} averageLength the average length of line.
24
+ * @property {number} countOfLines the count of features.
25
+ */
26
+ /**
27
+ * This module calculate the average angle of a set of lines, measuring the trend of it.
28
+ * It can be used in both project coordinate system and geography coordinate system.
29
+ * It can handle segments of line or the whole line.
30
+ * @name directionalMean
31
+ * @param {FeatureCollection<LineString>} lines
32
+ * @param {object} [options={}]
33
+ * @param {boolean} [options.planar=true] whether the spatial reference system is projected or geographical.
34
+ * @param {boolean} [options.segment=false] whether treat a LineString as a whole or a set of segments.
35
+ * @returns {DirectionalMeanLine} Directional Mean Line
36
+ * @example
37
+ *
38
+ * var lines = turf.lineStrings([
39
+ * [[110, 45], [120, 50]],
40
+ * [[100, 50], [115, 55]],
41
+ * ])
42
+ * var directionalMeanLine = turf.directionalMean(lines);
43
+ * // => directionalMeanLine
44
+ */
45
+ declare function directionalMean(lines: FeatureCollection<LineString>, options?: {
46
+ planar?: boolean;
47
+ segment?: boolean;
48
+ }): DirectionalMeanLine;
49
+
50
+ export { type DirectionalMeanLine, directionalMean as default, directionalMean };
@@ -0,0 +1,211 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // index.ts
5
+ import { bearing } from "@turf/bearing";
6
+ import { centroid } from "@turf/centroid";
7
+ import { destination } from "@turf/destination";
8
+ import { featureCollection, lineString, point } from "@turf/helpers";
9
+ import { getCoord } from "@turf/invariant";
10
+ import { length } from "@turf/length";
11
+ import { featureEach, segmentEach, segmentReduce } from "@turf/meta";
12
+ function directionalMean(lines, options = {}) {
13
+ var _a;
14
+ const isPlanar = !!options.planar;
15
+ const isSegment = (_a = options.segment) != null ? _a : false;
16
+ let sigmaSin = 0;
17
+ let sigmaCos = 0;
18
+ let countOfLines = 0;
19
+ let sumOfLen = 0;
20
+ const centroidList = [];
21
+ if (isSegment) {
22
+ segmentEach(lines, (currentSegment) => {
23
+ const [sin1, cos1] = getCosAndSin(
24
+ currentSegment.geometry.coordinates,
25
+ isPlanar
26
+ );
27
+ const lenOfLine = getLengthOfLineString(currentSegment, isPlanar);
28
+ if (isNaN(sin1) || isNaN(cos1)) {
29
+ return;
30
+ } else {
31
+ sigmaSin += sin1;
32
+ sigmaCos += cos1;
33
+ countOfLines += 1;
34
+ sumOfLen += lenOfLine;
35
+ centroidList.push(centroid(currentSegment));
36
+ }
37
+ });
38
+ } else {
39
+ featureEach(lines, (currentFeature) => {
40
+ if (currentFeature.geometry.type !== "LineString") {
41
+ throw new Error("shold to support MultiLineString?");
42
+ }
43
+ const [sin1, cos1] = getCosAndSin(
44
+ currentFeature.geometry.coordinates,
45
+ isPlanar
46
+ );
47
+ const lenOfLine = getLengthOfLineString(currentFeature, isPlanar);
48
+ if (isNaN(sin1) || isNaN(cos1)) {
49
+ return;
50
+ } else {
51
+ sigmaSin += sin1;
52
+ sigmaCos += cos1;
53
+ countOfLines += 1;
54
+ sumOfLen += lenOfLine;
55
+ centroidList.push(centroid(currentFeature));
56
+ }
57
+ });
58
+ }
59
+ const cartesianAngle = getAngleBySinAndCos(sigmaSin, sigmaCos);
60
+ const bearingAngle = bearingToCartesian(cartesianAngle);
61
+ const circularVariance = getCircularVariance(
62
+ sigmaSin,
63
+ sigmaCos,
64
+ countOfLines
65
+ );
66
+ const averageLength = sumOfLen / countOfLines;
67
+ const centroidOfLines = centroid(featureCollection(centroidList));
68
+ const [averageX, averageY] = getCoord(centroidOfLines);
69
+ let meanLinestring;
70
+ if (isPlanar) {
71
+ meanLinestring = getMeanLineString(
72
+ [averageX, averageY],
73
+ cartesianAngle,
74
+ averageLength,
75
+ isPlanar
76
+ );
77
+ } else {
78
+ meanLinestring = getMeanLineString(
79
+ [averageX, averageY],
80
+ bearingAngle,
81
+ averageLength,
82
+ isPlanar
83
+ );
84
+ }
85
+ return lineString(meanLinestring, {
86
+ averageLength,
87
+ averageX,
88
+ averageY,
89
+ bearingAngle,
90
+ cartesianAngle,
91
+ circularVariance,
92
+ countOfLines
93
+ });
94
+ }
95
+ __name(directionalMean, "directionalMean");
96
+ function euclideanDistance(coords) {
97
+ const [x0, y0] = coords[0];
98
+ const [x1, y1] = coords[1];
99
+ const dx = x1 - x0;
100
+ const dy = y1 - y0;
101
+ return Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
102
+ }
103
+ __name(euclideanDistance, "euclideanDistance");
104
+ function getLengthOfLineString(line, isPlanar) {
105
+ if (isPlanar) {
106
+ return segmentReduce(
107
+ line,
108
+ (previousValue, segment) => {
109
+ const coords = segment.geometry.coordinates;
110
+ return previousValue + euclideanDistance(coords);
111
+ },
112
+ 0
113
+ );
114
+ } else {
115
+ return length(line, {
116
+ units: "meters"
117
+ });
118
+ }
119
+ }
120
+ __name(getLengthOfLineString, "getLengthOfLineString");
121
+ function bearingToCartesian(angle) {
122
+ let result = 90 - angle;
123
+ if (result > 180) {
124
+ result -= 360;
125
+ }
126
+ return result;
127
+ }
128
+ __name(bearingToCartesian, "bearingToCartesian");
129
+ function getCosAndSin(coordinates, isPlanar) {
130
+ const beginPoint = coordinates[0];
131
+ const endPoint = coordinates[coordinates.length - 1];
132
+ if (isPlanar) {
133
+ const [x0, y0] = beginPoint;
134
+ const [x1, y1] = endPoint;
135
+ const dx = x1 - x0;
136
+ const dy = y1 - y0;
137
+ const h = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
138
+ if (h < 1e-9) {
139
+ return [NaN, NaN];
140
+ }
141
+ const sin1 = dy / h;
142
+ const cos1 = dx / h;
143
+ return [sin1, cos1];
144
+ } else {
145
+ const angle = bearingToCartesian(bearing(beginPoint, endPoint));
146
+ const radian = angle * Math.PI / 180;
147
+ return [Math.sin(radian), Math.cos(radian)];
148
+ }
149
+ }
150
+ __name(getCosAndSin, "getCosAndSin");
151
+ function getAngleBySinAndCos(sin1, cos1) {
152
+ let angle = 0;
153
+ if (Math.abs(cos1) < 1e-9) {
154
+ angle = 90;
155
+ } else {
156
+ angle = Math.atan2(sin1, cos1) * 180 / Math.PI;
157
+ }
158
+ if (sin1 >= 0) {
159
+ if (cos1 < 0) {
160
+ angle += 180;
161
+ }
162
+ } else {
163
+ if (cos1 < 0) {
164
+ angle -= 180;
165
+ }
166
+ }
167
+ return angle;
168
+ }
169
+ __name(getAngleBySinAndCos, "getAngleBySinAndCos");
170
+ function getCircularVariance(sin1, cos1, len) {
171
+ if (len === 0) {
172
+ throw new Error("the size of the features set must be greater than 0");
173
+ }
174
+ return 1 - Math.sqrt(Math.pow(sin1, 2) + Math.pow(cos1, 2)) / len;
175
+ }
176
+ __name(getCircularVariance, "getCircularVariance");
177
+ function getMeanLineString(centroidOfLine, angle, lenOfLine, isPlanar) {
178
+ if (isPlanar) {
179
+ const [averageX, averageY] = centroidOfLine;
180
+ let beginX;
181
+ let beginY;
182
+ let endX;
183
+ let endY;
184
+ const r = angle * Math.PI / 180;
185
+ const sin = Math.sin(r);
186
+ const cos = Math.cos(r);
187
+ beginX = averageX - lenOfLine / 2 * cos;
188
+ beginY = averageY - lenOfLine / 2 * sin;
189
+ endX = averageX + lenOfLine / 2 * cos;
190
+ endY = averageY + lenOfLine / 2 * sin;
191
+ return [
192
+ [beginX, beginY],
193
+ [endX, endY]
194
+ ];
195
+ } else {
196
+ const end = destination(point(centroidOfLine), lenOfLine / 2, angle, {
197
+ units: "meters"
198
+ });
199
+ const begin = destination(point(centroidOfLine), -lenOfLine / 2, angle, {
200
+ units: "meters"
201
+ });
202
+ return [getCoord(begin), getCoord(end)];
203
+ }
204
+ }
205
+ __name(getMeanLineString, "getMeanLineString");
206
+ var turf_directional_mean_default = directionalMean;
207
+ export {
208
+ turf_directional_mean_default as default,
209
+ directionalMean
210
+ };
211
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../index.ts"],"sourcesContent":["import { Feature, FeatureCollection, LineString, Point } from \"geojson\";\nimport { bearing } from \"@turf/bearing\";\nimport { centroid } from \"@turf/centroid\";\nimport { destination } from \"@turf/destination\";\nimport { featureCollection, lineString, point } from \"@turf/helpers\";\nimport { getCoord } from \"@turf/invariant\";\nimport { length } from \"@turf/length\";\nimport { featureEach, segmentEach, segmentReduce } from \"@turf/meta\";\n\ninterface DirectionalMeanLine extends Feature<LineString> {\n properties: {\n cartesianAngle: number;\n bearingAngle: number;\n circularVariance: number;\n averageX: number;\n averageY: number;\n averageLength: number;\n countOfLines: number;\n [key: string]: any;\n };\n}\n\n/**\n * @typedef {Object} DirectionalMeanLine\n * @property {number} cartesianAngle the mean angle of all lines. (measure from due earth counterclockwise).\n * @property {number} bearingAngle the mean angle of all lines. (bearing).\n * @property {number} circularVariance the extent to which features all point in the same direction.\n * the value ranges 0-1, the bigger the value, the more variation in directions between lines.\n * @property {number} averageX the centroid of all lines.\n * @property {number} averageY the centroid of all line.\n * @property {number} averageLength the average length of line.\n * @property {number} countOfLines the count of features.\n */\n\n/**\n * This module calculate the average angle of a set of lines, measuring the trend of it.\n * It can be used in both project coordinate system and geography coordinate system.\n * It can handle segments of line or the whole line.\n * @name directionalMean\n * @param {FeatureCollection<LineString>} lines\n * @param {object} [options={}]\n * @param {boolean} [options.planar=true] whether the spatial reference system is projected or geographical.\n * @param {boolean} [options.segment=false] whether treat a LineString as a whole or a set of segments.\n * @returns {DirectionalMeanLine} Directional Mean Line\n * @example\n *\n * var lines = turf.lineStrings([\n * [[110, 45], [120, 50]],\n * [[100, 50], [115, 55]],\n * ])\n * var directionalMeanLine = turf.directionalMean(lines);\n * // => directionalMeanLine\n */\nfunction directionalMean(\n lines: FeatureCollection<LineString>,\n options: {\n planar?: boolean;\n segment?: boolean;\n } = {}\n): DirectionalMeanLine {\n const isPlanar = !!options.planar; // you can't use options.planar || true here.\n const isSegment: boolean = options.segment ?? false;\n let sigmaSin = 0;\n let sigmaCos = 0;\n let countOfLines = 0;\n let sumOfLen = 0;\n const centroidList: Array<Feature<Point>> = [];\n\n if (isSegment) {\n segmentEach(lines, (currentSegment: any) => {\n // todo fix turf-meta's declaration file\n const [sin1, cos1]: [number, number] = getCosAndSin(\n currentSegment.geometry.coordinates,\n isPlanar\n );\n const lenOfLine = getLengthOfLineString(currentSegment, isPlanar);\n if (isNaN(sin1) || isNaN(cos1)) {\n return;\n } else {\n sigmaSin += sin1;\n sigmaCos += cos1;\n countOfLines += 1;\n sumOfLen += lenOfLine;\n centroidList.push(centroid(currentSegment));\n }\n });\n // planar and segment\n } else {\n // planar and non-segment\n featureEach(lines, (currentFeature: Feature<LineString>) => {\n if (currentFeature.geometry.type !== \"LineString\") {\n throw new Error(\"shold to support MultiLineString?\");\n }\n const [sin1, cos1]: [number, number] = getCosAndSin(\n currentFeature.geometry.coordinates,\n isPlanar\n );\n const lenOfLine = getLengthOfLineString(currentFeature, isPlanar);\n if (isNaN(sin1) || isNaN(cos1)) {\n return;\n } else {\n sigmaSin += sin1;\n sigmaCos += cos1;\n countOfLines += 1;\n sumOfLen += lenOfLine;\n centroidList.push(centroid(currentFeature));\n }\n });\n }\n\n const cartesianAngle: number = getAngleBySinAndCos(sigmaSin, sigmaCos);\n const bearingAngle: number = bearingToCartesian(cartesianAngle);\n const circularVariance = getCircularVariance(\n sigmaSin,\n sigmaCos,\n countOfLines\n );\n const averageLength = sumOfLen / countOfLines;\n const centroidOfLines = centroid(featureCollection(centroidList));\n const [averageX, averageY]: number[] = getCoord(centroidOfLines);\n let meanLinestring;\n if (isPlanar) {\n meanLinestring = getMeanLineString(\n [averageX, averageY],\n cartesianAngle,\n averageLength,\n isPlanar\n );\n } else {\n meanLinestring = getMeanLineString(\n [averageX, averageY],\n bearingAngle,\n averageLength,\n isPlanar\n );\n }\n\n return lineString(meanLinestring, {\n averageLength,\n averageX,\n averageY,\n bearingAngle,\n cartesianAngle,\n circularVariance,\n countOfLines,\n });\n}\n\n/**\n * get euclidean distance between two points.\n * @private\n * @name euclideanDistance\n * @param coords\n */\nfunction euclideanDistance(coords: number[][]) {\n const [x0, y0]: number[] = coords[0];\n const [x1, y1]: number[] = coords[1];\n const dx: number = x1 - x0;\n const dy: number = y1 - y0;\n return Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));\n}\n\n/**\n * get the length of a LineString, both in projected or geographical coordinate system.\n * @private\n * @name getLengthOfLineString\n * @param {Feature<LineString>} line\n * @param {boolean} isPlanar\n */\nfunction getLengthOfLineString(line: Feature<LineString>, isPlanar: boolean) {\n if (isPlanar) {\n return segmentReduce<number>(\n line,\n (previousValue?: number, segment?: Feature<LineString>): number => {\n const coords = segment!.geometry.coordinates; // the signatrue of segmentReduce has problem ?\n return previousValue! + euclideanDistance(coords);\n },\n 0\n );\n } else {\n return length(line, {\n units: \"meters\",\n });\n }\n}\n\n/**\n * bearing to xy(from due earth counterclockwise 0-180)\n * convert between two forms\n * @private\n * @name bearingToCartesian\n * @param angle\n */\nfunction bearingToCartesian(angle: number): number {\n let result = 90 - angle;\n if (result > 180) {\n result -= 360;\n }\n return result;\n}\n\n/**\n * @private\n * @name getCosAndSin\n * @param {Array<Array<number>>} coordinates\n * @returns {Array<number>} [cos, sin]\n */\nfunction getCosAndSin(\n coordinates: number[][],\n isPlanar: boolean\n): [number, number] {\n const beginPoint: number[] = coordinates[0];\n const endPoint: number[] = coordinates[coordinates.length - 1];\n if (isPlanar) {\n const [x0, y0]: number[] = beginPoint;\n const [x1, y1]: number[] = endPoint;\n const dx: number = x1 - x0;\n const dy: number = y1 - y0;\n const h = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));\n if (h < 0.000000001) {\n return [NaN, NaN];\n }\n const sin1 = dy / h;\n const cos1 = dx / h;\n return [sin1, cos1];\n } else {\n const angle = bearingToCartesian(bearing(beginPoint, endPoint));\n const radian = (angle * Math.PI) / 180;\n return [Math.sin(radian), Math.cos(radian)];\n }\n}\n\nfunction getAngleBySinAndCos(sin1: number, cos1: number): number {\n let angle = 0;\n if (Math.abs(cos1) < 0.000000001) {\n angle = 90;\n } else {\n angle = (Math.atan2(sin1, cos1) * 180) / Math.PI;\n }\n if (sin1 >= 0) {\n if (cos1 < 0) {\n angle += 180;\n }\n } else {\n if (cos1 < 0) {\n angle -= 180;\n }\n }\n return angle;\n}\n\nfunction getCircularVariance(sin1: number, cos1: number, len: number) {\n if (len === 0) {\n throw new Error(\"the size of the features set must be greater than 0\");\n }\n return 1 - Math.sqrt(Math.pow(sin1, 2) + Math.pow(cos1, 2)) / len;\n}\n\nfunction getMeanLineString(\n centroidOfLine: number[],\n angle: number,\n lenOfLine: number,\n isPlanar: boolean\n) {\n if (isPlanar) {\n const [averageX, averageY]: number[] = centroidOfLine;\n let beginX: number;\n let beginY: number;\n let endX: number;\n let endY: number;\n const r: number = (angle * Math.PI) / 180;\n const sin: number = Math.sin(r);\n const cos: number = Math.cos(r);\n beginX = averageX - (lenOfLine / 2) * cos;\n beginY = averageY - (lenOfLine / 2) * sin;\n endX = averageX + (lenOfLine / 2) * cos;\n endY = averageY + (lenOfLine / 2) * sin;\n return [\n [beginX, beginY],\n [endX, endY],\n ];\n } else {\n const end = destination(point(centroidOfLine), lenOfLine / 2, angle, {\n units: \"meters\",\n });\n const begin = destination(point(centroidOfLine), -lenOfLine / 2, angle, {\n units: \"meters\",\n });\n return [getCoord(begin), getCoord(end)];\n }\n}\n\nexport { directionalMean, DirectionalMeanLine };\nexport default directionalMean;\n"],"mappings":";;;;AACA,SAAS,eAAe;AACxB,SAAS,gBAAgB;AACzB,SAAS,mBAAmB;AAC5B,SAAS,mBAAmB,YAAY,aAAa;AACrD,SAAS,gBAAgB;AACzB,SAAS,cAAc;AACvB,SAAS,aAAa,aAAa,qBAAqB;AA8CxD,SAAS,gBACP,OACA,UAGI,CAAC,GACgB;AA3DvB;AA4DE,QAAM,WAAW,CAAC,CAAC,QAAQ;AAC3B,QAAM,aAAqB,aAAQ,YAAR,YAAmB;AAC9C,MAAI,WAAW;AACf,MAAI,WAAW;AACf,MAAI,eAAe;AACnB,MAAI,WAAW;AACf,QAAM,eAAsC,CAAC;AAE7C,MAAI,WAAW;AACb,gBAAY,OAAO,CAAC,mBAAwB;AAE1C,YAAM,CAAC,MAAM,IAAI,IAAsB;AAAA,QACrC,eAAe,SAAS;AAAA,QACxB;AAAA,MACF;AACA,YAAM,YAAY,sBAAsB,gBAAgB,QAAQ;AAChE,UAAI,MAAM,IAAI,KAAK,MAAM,IAAI,GAAG;AAC9B;AAAA,MACF,OAAO;AACL,oBAAY;AACZ,oBAAY;AACZ,wBAAgB;AAChB,oBAAY;AACZ,qBAAa,KAAK,SAAS,cAAc,CAAC;AAAA,MAC5C;AAAA,IACF,CAAC;AAAA,EAEH,OAAO;AAEL,gBAAY,OAAO,CAAC,mBAAwC;AAC1D,UAAI,eAAe,SAAS,SAAS,cAAc;AACjD,cAAM,IAAI,MAAM,mCAAmC;AAAA,MACrD;AACA,YAAM,CAAC,MAAM,IAAI,IAAsB;AAAA,QACrC,eAAe,SAAS;AAAA,QACxB;AAAA,MACF;AACA,YAAM,YAAY,sBAAsB,gBAAgB,QAAQ;AAChE,UA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AAA,EACF,OAAO;AACL,QAAI,OAAO,GAAG;AACZ,eAAS;AAAA,IACX;AAAA,EACF;AACA,SAAO;AACT;AAjBS;AAmBT,SAAS,oBAAoB,MAAc,MAAc,KAAa;AACpE,MAAI,QAAQ,GAAG;AACb,UAAM,IAAI,MAAM,qDAAqD;AAAA,EACvE;AACA,SAAO,IAAI,KAAK,KAAK,KAAK,IAAI,MAAM,CAAC,IAAI,KAAK,IAAI,MAAM,CAAC,CAAC,IAAI;AAChE;AALS;AAOT,SAAS,kBACP,gBACA,OACA,WACA,UACA;AACA,MAAI,UAAU;AACZ,UAAM,CAAC,UAAU,QAAQ,IAAc;AACvC,QAAI;AACJ,QAAI;AACJ,QAAI;AACJ,QAAI;AACJ,UAAM,IAAa,QAAQ,KAAK,KAAM;AACtC,UAAM,MAAc,KAAK,IAAI,CAAC;AAC9B,UAAM,MAAc,KAAK,IAAI,CAAC;AAC9B,aAAS,WAAY,YAAY,IAAK;AACtC,aAAS,WAAY,YAAY,IAAK;AACtC,WAAO,WAAY,YAAY,IAAK;AACpC,WAAO,WAAY,YAAY,IAAK;AACpC,WAAO;AAAA,MACL,CAAC,QAAQ,MAAM;AAAA,MACf,CAAC,MAAM,IAAI;AAAA,IACb;AAAA,EACF,OAAO;AACL,UAAM,MAAM,YAAY,MAAM,cAAc,GAAG,YAAY,GAAG,OAAO;AAAA,MACnE,OAAO;AAAA,IACT,CAAC;AACD,UAAM,QAAQ,YAAY,MAAM,cAAc,GAAG,CAAC,YAAY,GAAG,OAAO;AAAA,MACtE,OAAO;AAAA,IACT,CAAC;AACD,WAAO,CAAC,SAAS,KAAK,GAAG,SAAS,GAAG,CAAC;AAAA,EACxC;AACF;AAhCS;AAmCT,IAAO,gCAAQ;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@turf/directional-mean",
3
- "version": "7.0.0-alpha.1",
3
+ "version": "7.0.0-alpha.110+1411d63a7",
4
4
  "description": "turf directional-mean module",
5
5
  "author": "Turf Authors",
6
6
  "contributors": [
@@ -23,50 +23,55 @@
23
23
  "turf",
24
24
  "directional-mean"
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
- "types": "./dist/js/index.d.ts",
32
- "import": "./dist/es/index.js",
33
- "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
+ }
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.js",
43
- "build": "npm-run-all build:*",
44
- "build:es": "tsc --outDir dist/es --module esnext --declaration false && echo '{\"type\":\"module\"}' > dist/es/package.json",
45
- "build:js": "tsc",
46
- "docs": "tsx ../../scripts/generate-readmes",
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/tape": "*",
52
- "benchmark": "*",
53
- "load-json-file": "*",
54
- "npm-run-all": "*",
55
- "tape": "*",
56
- "tslint": "*",
57
- "tsx": "*",
58
- "typescript": "*",
59
- "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"
60
65
  },
61
66
  "dependencies": {
62
- "@turf/bearing": "^7.0.0-alpha.1",
63
- "@turf/centroid": "^7.0.0-alpha.1",
64
- "@turf/destination": "^7.0.0-alpha.1",
65
- "@turf/helpers": "^7.0.0-alpha.1",
66
- "@turf/invariant": "^7.0.0-alpha.1",
67
- "@turf/length": "^7.0.0-alpha.1",
68
- "@turf/meta": "^7.0.0-alpha.1",
69
- "tslib": "^2.3.0"
67
+ "@turf/bearing": "^7.0.0-alpha.110+1411d63a7",
68
+ "@turf/centroid": "^7.0.0-alpha.110+1411d63a7",
69
+ "@turf/destination": "^7.0.0-alpha.110+1411d63a7",
70
+ "@turf/helpers": "^7.0.0-alpha.110+1411d63a7",
71
+ "@turf/invariant": "^7.0.0-alpha.110+1411d63a7",
72
+ "@turf/length": "^7.0.0-alpha.110+1411d63a7",
73
+ "@turf/meta": "^7.0.0-alpha.110+1411d63a7",
74
+ "tslib": "^2.6.2"
70
75
  },
71
- "gitHead": "cf7a0c507b017ca066acffd0ce23bda5b393fb5a"
76
+ "gitHead": "1411d63a74c275c9216fe48e9d3cb2d48a359068"
72
77
  }
package/dist/es/index.js DELETED
@@ -1,236 +0,0 @@
1
- import bearing from "@turf/bearing";
2
- import centroid from "@turf/centroid";
3
- import destination from "@turf/destination";
4
- import { featureCollection, lineString, point } from "@turf/helpers";
5
- import { getCoord } from "@turf/invariant";
6
- import length from "@turf/length";
7
- import { featureEach, segmentEach, segmentReduce } from "@turf/meta";
8
- /**
9
- * @typedef {Object} DirectionalMeanLine
10
- * @property {number} cartesianAngle the mean angle of all lines. (measure from due earth counterclockwise).
11
- * @property {number} bearingAngle the mean angle of all lines. (bearing).
12
- * @property {number} circularVariance the extent to which features all point in the same direction.
13
- * the value ranges 0-1, the bigger the value, the more variation in directions between lines.
14
- * @property {number} averageX the centroid of all lines.
15
- * @property {number} averageY the centroid of all line.
16
- * @property {number} averageLength the average length of line.
17
- * @property {number} countOfLines the count of features.
18
- */
19
- /**
20
- * This module calculate the average angle of a set of lines, measuring the trend of it.
21
- * It can be used in both project coordinate system and geography coordinate system.
22
- * It can handle segments of line or the whole line.
23
- * @name directionalMean
24
- * @param {FeatureCollection<LineString>} lines
25
- * @param {object} [options={}]
26
- * @param {boolean} [options.planar=true] whether the spatial reference system is projected or geographical.
27
- * @param {boolean} [options.segment=false] whether treat a LineString as a whole or a set of segments.
28
- * @returns {DirectionalMeanLine} Directional Mean Line
29
- * @example
30
- *
31
- * var lines = turf.lineStrings([
32
- * [[110, 45], [120, 50]],
33
- * [[100, 50], [115, 55]],
34
- * ])
35
- * var directionalMeanLine = turf.directionalMean(lines);
36
- * // => directionalMeanLine
37
- */
38
- export default function directionalMean(lines, options = {}) {
39
- const isPlanar = !!options.planar; // you can't use options.planar || true here.
40
- const isSegment = options.segment || false;
41
- let sigmaSin = 0;
42
- let sigmaCos = 0;
43
- let countOfLines = 0;
44
- let sumOfLen = 0;
45
- const centroidList = [];
46
- if (isSegment) {
47
- segmentEach(lines, (currentSegment) => {
48
- // todo fix turf-meta's declaration file
49
- const [sin1, cos1] = getCosAndSin(currentSegment.geometry.coordinates, isPlanar);
50
- const lenOfLine = getLengthOfLineString(currentSegment, isPlanar);
51
- if (isNaN(sin1) || isNaN(cos1)) {
52
- return;
53
- }
54
- else {
55
- sigmaSin += sin1;
56
- sigmaCos += cos1;
57
- countOfLines += 1;
58
- sumOfLen += lenOfLine;
59
- centroidList.push(centroid(currentSegment));
60
- }
61
- });
62
- // planar and segment
63
- }
64
- else {
65
- // planar and non-segment
66
- featureEach(lines, (currentFeature) => {
67
- if (currentFeature.geometry.type !== "LineString") {
68
- throw new Error("shold to support MultiLineString?");
69
- }
70
- const [sin1, cos1] = getCosAndSin(currentFeature.geometry.coordinates, isPlanar);
71
- const lenOfLine = getLengthOfLineString(currentFeature, isPlanar);
72
- if (isNaN(sin1) || isNaN(cos1)) {
73
- return;
74
- }
75
- else {
76
- sigmaSin += sin1;
77
- sigmaCos += cos1;
78
- countOfLines += 1;
79
- sumOfLen += lenOfLine;
80
- centroidList.push(centroid(currentFeature));
81
- }
82
- });
83
- }
84
- const cartesianAngle = getAngleBySinAndCos(sigmaSin, sigmaCos);
85
- const bearingAngle = bearingToCartesian(cartesianAngle);
86
- const circularVariance = getCircularVariance(sigmaSin, sigmaCos, countOfLines);
87
- const averageLength = sumOfLen / countOfLines;
88
- const centroidOfLines = centroid(featureCollection(centroidList));
89
- const [averageX, averageY] = getCoord(centroidOfLines);
90
- let meanLinestring;
91
- if (isPlanar) {
92
- meanLinestring = getMeanLineString([averageX, averageY], cartesianAngle, averageLength, isPlanar);
93
- }
94
- else {
95
- meanLinestring = getMeanLineString([averageX, averageY], bearingAngle, averageLength, isPlanar);
96
- }
97
- return lineString(meanLinestring, {
98
- averageLength,
99
- averageX,
100
- averageY,
101
- bearingAngle,
102
- cartesianAngle,
103
- circularVariance,
104
- countOfLines,
105
- });
106
- }
107
- /**
108
- * get euclidean distance between two points.
109
- * @private
110
- * @name euclideanDistance
111
- * @param coords
112
- */
113
- function euclideanDistance(coords) {
114
- const [x0, y0] = coords[0];
115
- const [x1, y1] = coords[1];
116
- const dx = x1 - x0;
117
- const dy = y1 - y0;
118
- return Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
119
- }
120
- /**
121
- * get the length of a LineString, both in projected or geographical coordinate system.
122
- * @private
123
- * @name getLengthOfLineString
124
- * @param {Feature<LineString>} line
125
- * @param {boolean} isPlanar
126
- */
127
- function getLengthOfLineString(line, isPlanar) {
128
- if (isPlanar) {
129
- return segmentReduce(line, (previousValue, segment) => {
130
- const coords = segment.geometry.coordinates; // the signatrue of segmentReduce has problem ?
131
- return previousValue + euclideanDistance(coords);
132
- }, 0);
133
- }
134
- else {
135
- return length(line, {
136
- units: "meters",
137
- });
138
- }
139
- }
140
- /**
141
- * bearing to xy(from due earth counterclockwise 0-180)
142
- * convert between two forms
143
- * @private
144
- * @name bearingToCartesian
145
- * @param angle
146
- */
147
- function bearingToCartesian(angle) {
148
- let result = 90 - angle;
149
- if (result > 180) {
150
- result -= 360;
151
- }
152
- return result;
153
- }
154
- /**
155
- * @private
156
- * @name getCosAndSin
157
- * @param {Array<Array<number>>} coordinates
158
- * @returns {Array<number>} [cos, sin]
159
- */
160
- function getCosAndSin(coordinates, isPlanar) {
161
- const beginPoint = coordinates[0];
162
- const endPoint = coordinates[coordinates.length - 1];
163
- if (isPlanar) {
164
- const [x0, y0] = beginPoint;
165
- const [x1, y1] = endPoint;
166
- const dx = x1 - x0;
167
- const dy = y1 - y0;
168
- const h = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
169
- if (h < 0.000000001) {
170
- return [NaN, NaN];
171
- }
172
- const sin1 = dy / h;
173
- const cos1 = dx / h;
174
- return [sin1, cos1];
175
- }
176
- else {
177
- const angle = bearingToCartesian(bearing(beginPoint, endPoint));
178
- const radian = (angle * Math.PI) / 180;
179
- return [Math.sin(radian), Math.cos(radian)];
180
- }
181
- }
182
- function getAngleBySinAndCos(sin1, cos1) {
183
- let angle = 0;
184
- if (Math.abs(cos1) < 0.000000001) {
185
- angle = 90;
186
- }
187
- else {
188
- angle = (Math.atan2(sin1, cos1) * 180) / Math.PI;
189
- }
190
- if (sin1 >= 0) {
191
- if (cos1 < 0) {
192
- angle += 180;
193
- }
194
- }
195
- else {
196
- if (cos1 < 0) {
197
- angle -= 180;
198
- }
199
- }
200
- return angle;
201
- }
202
- function getCircularVariance(sin1, cos1, len) {
203
- if (len === 0) {
204
- throw new Error("the size of the features set must be greater than 0");
205
- }
206
- return 1 - Math.sqrt(Math.pow(sin1, 2) + Math.pow(cos1, 2)) / len;
207
- }
208
- function getMeanLineString(centroidOfLine, angle, lenOfLine, isPlanar) {
209
- if (isPlanar) {
210
- const [averageX, averageY] = centroidOfLine;
211
- let beginX;
212
- let beginY;
213
- let endX;
214
- let endY;
215
- const r = (angle * Math.PI) / 180;
216
- const sin = Math.sin(r);
217
- const cos = Math.cos(r);
218
- beginX = averageX - (lenOfLine / 2) * cos;
219
- beginY = averageY - (lenOfLine / 2) * sin;
220
- endX = averageX + (lenOfLine / 2) * cos;
221
- endY = averageY + (lenOfLine / 2) * sin;
222
- return [
223
- [beginX, beginY],
224
- [endX, endY],
225
- ];
226
- }
227
- else {
228
- const end = destination(point(centroidOfLine), lenOfLine / 2, angle, {
229
- units: "meters",
230
- });
231
- const begin = destination(point(centroidOfLine), -lenOfLine / 2, angle, {
232
- units: "meters",
233
- });
234
- return [getCoord(begin), getCoord(end)];
235
- }
236
- }
@@ -1 +0,0 @@
1
- {"type":"module"}
package/dist/js/index.js DELETED
@@ -1,240 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const tslib_1 = require("tslib");
4
- const bearing_1 = tslib_1.__importDefault(require("@turf/bearing"));
5
- const centroid_1 = tslib_1.__importDefault(require("@turf/centroid"));
6
- const destination_1 = tslib_1.__importDefault(require("@turf/destination"));
7
- const helpers_1 = require("@turf/helpers");
8
- const invariant_1 = require("@turf/invariant");
9
- const length_1 = tslib_1.__importDefault(require("@turf/length"));
10
- const meta_1 = require("@turf/meta");
11
- /**
12
- * @typedef {Object} DirectionalMeanLine
13
- * @property {number} cartesianAngle the mean angle of all lines. (measure from due earth counterclockwise).
14
- * @property {number} bearingAngle the mean angle of all lines. (bearing).
15
- * @property {number} circularVariance the extent to which features all point in the same direction.
16
- * the value ranges 0-1, the bigger the value, the more variation in directions between lines.
17
- * @property {number} averageX the centroid of all lines.
18
- * @property {number} averageY the centroid of all line.
19
- * @property {number} averageLength the average length of line.
20
- * @property {number} countOfLines the count of features.
21
- */
22
- /**
23
- * This module calculate the average angle of a set of lines, measuring the trend of it.
24
- * It can be used in both project coordinate system and geography coordinate system.
25
- * It can handle segments of line or the whole line.
26
- * @name directionalMean
27
- * @param {FeatureCollection<LineString>} lines
28
- * @param {object} [options={}]
29
- * @param {boolean} [options.planar=true] whether the spatial reference system is projected or geographical.
30
- * @param {boolean} [options.segment=false] whether treat a LineString as a whole or a set of segments.
31
- * @returns {DirectionalMeanLine} Directional Mean Line
32
- * @example
33
- *
34
- * var lines = turf.lineStrings([
35
- * [[110, 45], [120, 50]],
36
- * [[100, 50], [115, 55]],
37
- * ])
38
- * var directionalMeanLine = turf.directionalMean(lines);
39
- * // => directionalMeanLine
40
- */
41
- function directionalMean(lines, options = {}) {
42
- const isPlanar = !!options.planar; // you can't use options.planar || true here.
43
- const isSegment = options.segment || false;
44
- let sigmaSin = 0;
45
- let sigmaCos = 0;
46
- let countOfLines = 0;
47
- let sumOfLen = 0;
48
- const centroidList = [];
49
- if (isSegment) {
50
- meta_1.segmentEach(lines, (currentSegment) => {
51
- // todo fix turf-meta's declaration file
52
- const [sin1, cos1] = getCosAndSin(currentSegment.geometry.coordinates, isPlanar);
53
- const lenOfLine = getLengthOfLineString(currentSegment, isPlanar);
54
- if (isNaN(sin1) || isNaN(cos1)) {
55
- return;
56
- }
57
- else {
58
- sigmaSin += sin1;
59
- sigmaCos += cos1;
60
- countOfLines += 1;
61
- sumOfLen += lenOfLine;
62
- centroidList.push(centroid_1.default(currentSegment));
63
- }
64
- });
65
- // planar and segment
66
- }
67
- else {
68
- // planar and non-segment
69
- meta_1.featureEach(lines, (currentFeature) => {
70
- if (currentFeature.geometry.type !== "LineString") {
71
- throw new Error("shold to support MultiLineString?");
72
- }
73
- const [sin1, cos1] = getCosAndSin(currentFeature.geometry.coordinates, isPlanar);
74
- const lenOfLine = getLengthOfLineString(currentFeature, isPlanar);
75
- if (isNaN(sin1) || isNaN(cos1)) {
76
- return;
77
- }
78
- else {
79
- sigmaSin += sin1;
80
- sigmaCos += cos1;
81
- countOfLines += 1;
82
- sumOfLen += lenOfLine;
83
- centroidList.push(centroid_1.default(currentFeature));
84
- }
85
- });
86
- }
87
- const cartesianAngle = getAngleBySinAndCos(sigmaSin, sigmaCos);
88
- const bearingAngle = bearingToCartesian(cartesianAngle);
89
- const circularVariance = getCircularVariance(sigmaSin, sigmaCos, countOfLines);
90
- const averageLength = sumOfLen / countOfLines;
91
- const centroidOfLines = centroid_1.default(helpers_1.featureCollection(centroidList));
92
- const [averageX, averageY] = invariant_1.getCoord(centroidOfLines);
93
- let meanLinestring;
94
- if (isPlanar) {
95
- meanLinestring = getMeanLineString([averageX, averageY], cartesianAngle, averageLength, isPlanar);
96
- }
97
- else {
98
- meanLinestring = getMeanLineString([averageX, averageY], bearingAngle, averageLength, isPlanar);
99
- }
100
- return helpers_1.lineString(meanLinestring, {
101
- averageLength,
102
- averageX,
103
- averageY,
104
- bearingAngle,
105
- cartesianAngle,
106
- circularVariance,
107
- countOfLines,
108
- });
109
- }
110
- exports.default = directionalMean;
111
- /**
112
- * get euclidean distance between two points.
113
- * @private
114
- * @name euclideanDistance
115
- * @param coords
116
- */
117
- function euclideanDistance(coords) {
118
- const [x0, y0] = coords[0];
119
- const [x1, y1] = coords[1];
120
- const dx = x1 - x0;
121
- const dy = y1 - y0;
122
- return Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
123
- }
124
- /**
125
- * get the length of a LineString, both in projected or geographical coordinate system.
126
- * @private
127
- * @name getLengthOfLineString
128
- * @param {Feature<LineString>} line
129
- * @param {boolean} isPlanar
130
- */
131
- function getLengthOfLineString(line, isPlanar) {
132
- if (isPlanar) {
133
- return meta_1.segmentReduce(line, (previousValue, segment) => {
134
- const coords = segment.geometry.coordinates; // the signatrue of segmentReduce has problem ?
135
- return previousValue + euclideanDistance(coords);
136
- }, 0);
137
- }
138
- else {
139
- return length_1.default(line, {
140
- units: "meters",
141
- });
142
- }
143
- }
144
- /**
145
- * bearing to xy(from due earth counterclockwise 0-180)
146
- * convert between two forms
147
- * @private
148
- * @name bearingToCartesian
149
- * @param angle
150
- */
151
- function bearingToCartesian(angle) {
152
- let result = 90 - angle;
153
- if (result > 180) {
154
- result -= 360;
155
- }
156
- return result;
157
- }
158
- /**
159
- * @private
160
- * @name getCosAndSin
161
- * @param {Array<Array<number>>} coordinates
162
- * @returns {Array<number>} [cos, sin]
163
- */
164
- function getCosAndSin(coordinates, isPlanar) {
165
- const beginPoint = coordinates[0];
166
- const endPoint = coordinates[coordinates.length - 1];
167
- if (isPlanar) {
168
- const [x0, y0] = beginPoint;
169
- const [x1, y1] = endPoint;
170
- const dx = x1 - x0;
171
- const dy = y1 - y0;
172
- const h = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
173
- if (h < 0.000000001) {
174
- return [NaN, NaN];
175
- }
176
- const sin1 = dy / h;
177
- const cos1 = dx / h;
178
- return [sin1, cos1];
179
- }
180
- else {
181
- const angle = bearingToCartesian(bearing_1.default(beginPoint, endPoint));
182
- const radian = (angle * Math.PI) / 180;
183
- return [Math.sin(radian), Math.cos(radian)];
184
- }
185
- }
186
- function getAngleBySinAndCos(sin1, cos1) {
187
- let angle = 0;
188
- if (Math.abs(cos1) < 0.000000001) {
189
- angle = 90;
190
- }
191
- else {
192
- angle = (Math.atan2(sin1, cos1) * 180) / Math.PI;
193
- }
194
- if (sin1 >= 0) {
195
- if (cos1 < 0) {
196
- angle += 180;
197
- }
198
- }
199
- else {
200
- if (cos1 < 0) {
201
- angle -= 180;
202
- }
203
- }
204
- return angle;
205
- }
206
- function getCircularVariance(sin1, cos1, len) {
207
- if (len === 0) {
208
- throw new Error("the size of the features set must be greater than 0");
209
- }
210
- return 1 - Math.sqrt(Math.pow(sin1, 2) + Math.pow(cos1, 2)) / len;
211
- }
212
- function getMeanLineString(centroidOfLine, angle, lenOfLine, isPlanar) {
213
- if (isPlanar) {
214
- const [averageX, averageY] = centroidOfLine;
215
- let beginX;
216
- let beginY;
217
- let endX;
218
- let endY;
219
- const r = (angle * Math.PI) / 180;
220
- const sin = Math.sin(r);
221
- const cos = Math.cos(r);
222
- beginX = averageX - (lenOfLine / 2) * cos;
223
- beginY = averageY - (lenOfLine / 2) * sin;
224
- endX = averageX + (lenOfLine / 2) * cos;
225
- endY = averageY + (lenOfLine / 2) * sin;
226
- return [
227
- [beginX, beginY],
228
- [endX, endY],
229
- ];
230
- }
231
- else {
232
- const end = destination_1.default(helpers_1.point(centroidOfLine), lenOfLine / 2, angle, {
233
- units: "meters",
234
- });
235
- const begin = destination_1.default(helpers_1.point(centroidOfLine), -lenOfLine / 2, angle, {
236
- units: "meters",
237
- });
238
- return [invariant_1.getCoord(begin), invariant_1.getCoord(end)];
239
- }
240
- }