@turf/line-chunk 7.0.0-alpha.2 → 7.1.0-alpha.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -49,26 +49,21 @@ Returns **[FeatureCollection][2]<[LineString][5]>** collection of line segments
49
49
 
50
50
  [10]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
51
51
 
52
- <!-- This file is automatically generated. Please don't edit it directly:
53
- if you find an error, edit the source file (likely index.js), and re-run
54
- ./scripts/generate-readmes in the turf project. -->
52
+ <!-- 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. -->
55
53
 
56
54
  ---
57
55
 
58
- This module is part of the [Turfjs project](http://turfjs.org/), an open source
59
- module collection dedicated to geographic algorithms. It is maintained in the
60
- [Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create
61
- PRs and issues.
56
+ 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.
62
57
 
63
58
  ### Installation
64
59
 
65
- Install this module individually:
60
+ Install this single module individually:
66
61
 
67
62
  ```sh
68
63
  $ npm install @turf/line-chunk
69
64
  ```
70
65
 
71
- Or install the Turf module that includes it as a function:
66
+ Or install the all-encompassing @turf/turf module that includes all modules as functions:
72
67
 
73
68
  ```sh
74
69
  $ npm install @turf/turf
@@ -0,0 +1,49 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});// index.js
2
+ var _length = require('@turf/length');
3
+ var _lineslicealong = require('@turf/line-slice-along');
4
+ var _meta = require('@turf/meta');
5
+ var _helpers = require('@turf/helpers');
6
+ function lineChunk(geojson, segmentLength, options) {
7
+ options = options || {};
8
+ if (!_helpers.isObject.call(void 0, options))
9
+ throw new Error("options is invalid");
10
+ var units = options.units;
11
+ var reverse = options.reverse;
12
+ if (!geojson)
13
+ throw new Error("geojson is required");
14
+ if (segmentLength <= 0)
15
+ throw new Error("segmentLength must be greater than 0");
16
+ var results = [];
17
+ _meta.flattenEach.call(void 0, geojson, function(feature) {
18
+ if (reverse)
19
+ feature.geometry.coordinates = feature.geometry.coordinates.reverse();
20
+ sliceLineSegments(feature, segmentLength, units, function(segment) {
21
+ results.push(segment);
22
+ });
23
+ });
24
+ return _helpers.featureCollection.call(void 0, results);
25
+ }
26
+ function sliceLineSegments(line, segmentLength, units, callback) {
27
+ var lineLength = _length.length.call(void 0, line, { units });
28
+ if (lineLength <= segmentLength)
29
+ return callback(line);
30
+ var numberOfSegments = lineLength / segmentLength;
31
+ if (!Number.isInteger(numberOfSegments)) {
32
+ numberOfSegments = Math.floor(numberOfSegments) + 1;
33
+ }
34
+ for (var i = 0; i < numberOfSegments; i++) {
35
+ var outline = _lineslicealong.lineSliceAlong.call(void 0,
36
+ line,
37
+ segmentLength * i,
38
+ segmentLength * (i + 1),
39
+ { units }
40
+ );
41
+ callback(outline, i);
42
+ }
43
+ }
44
+ var turf_line_chunk_default = lineChunk;
45
+
46
+
47
+
48
+ exports.default = turf_line_chunk_default; exports.lineChunk = lineChunk;
49
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../index.js"],"names":[],"mappings":";AAAA,SAAS,cAAc;AACvB,SAAS,sBAAsB;AAC/B,SAAS,mBAAmB;AAC5B,SAAS,mBAAmB,gBAAgB;AAqB5C,SAAS,UAAU,SAAS,eAAe,SAAS;AAElD,YAAU,WAAW,CAAC;AACtB,MAAI,CAAC,SAAS,OAAO;AAAG,UAAM,IAAI,MAAM,oBAAoB;AAC5D,MAAI,QAAQ,QAAQ;AACpB,MAAI,UAAU,QAAQ;AAGtB,MAAI,CAAC;AAAS,UAAM,IAAI,MAAM,qBAAqB;AACnD,MAAI,iBAAiB;AACnB,UAAM,IAAI,MAAM,sCAAsC;AAGxD,MAAI,UAAU,CAAC;AAGf,cAAY,SAAS,SAAU,SAAS;AAEtC,QAAI;AACF,cAAQ,SAAS,cAAc,QAAQ,SAAS,YAAY,QAAQ;AAEtE,sBAAkB,SAAS,eAAe,OAAO,SAAU,SAAS;AAClE,cAAQ,KAAK,OAAO;AAAA,IACtB,CAAC;AAAA,EACH,CAAC;AACD,SAAO,kBAAkB,OAAO;AAClC;AAYA,SAAS,kBAAkB,MAAM,eAAe,OAAO,UAAU;AAC/D,MAAI,aAAa,OAAO,MAAM,EAAE,MAAa,CAAC;AAG9C,MAAI,cAAc;AAAe,WAAO,SAAS,IAAI;AAErD,MAAI,mBAAmB,aAAa;AAGpC,MAAI,CAAC,OAAO,UAAU,gBAAgB,GAAG;AACvC,uBAAmB,KAAK,MAAM,gBAAgB,IAAI;AAAA,EACpD;AAEA,WAAS,IAAI,GAAG,IAAI,kBAAkB,KAAK;AACzC,QAAI,UAAU;AAAA,MACZ;AAAA,MACA,gBAAgB;AAAA,MAChB,iBAAiB,IAAI;AAAA,MACrB,EAAE,MAAa;AAAA,IACjB;AACA,aAAS,SAAS,CAAC;AAAA,EACrB;AACF;AAGA,IAAO,0BAAQ","sourcesContent":["import { length } from \"@turf/length\";\nimport { lineSliceAlong } from \"@turf/line-slice-along\";\nimport { flattenEach } from \"@turf/meta\";\nimport { featureCollection, isObject } from \"@turf/helpers\";\n\n/**\n * Divides a {@link LineString} into chunks of a specified length.\n * If the line is shorter than the segment length then the original line is returned.\n *\n * @name lineChunk\n * @param {FeatureCollection|Geometry|Feature<LineString|MultiLineString>} geojson the lines to split\n * @param {number} segmentLength how long to make each segment\n * @param {Object} [options={}] Optional parameters\n * @param {string} [options.units='kilometers'] units can be degrees, radians, miles, or kilometers\n * @param {boolean} [options.reverse=false] reverses coordinates to start the first chunked segment at the end\n * @returns {FeatureCollection<LineString>} collection of line segments\n * @example\n * var line = turf.lineString([[-95, 40], [-93, 45], [-85, 50]]);\n *\n * var chunk = turf.lineChunk(line, 15, {units: 'miles'});\n *\n * //addToMap\n * var addToMap = [chunk];\n */\nfunction lineChunk(geojson, segmentLength, options) {\n // Optional parameters\n options = options || {};\n if (!isObject(options)) throw new Error(\"options is invalid\");\n var units = options.units;\n var reverse = options.reverse;\n\n // Validation\n if (!geojson) throw new Error(\"geojson is required\");\n if (segmentLength <= 0)\n throw new Error(\"segmentLength must be greater than 0\");\n\n // Container\n var results = [];\n\n // Flatten each feature to simple LineString\n flattenEach(geojson, function (feature) {\n // reverses coordinates to start the first chunked segment at the end\n if (reverse)\n feature.geometry.coordinates = feature.geometry.coordinates.reverse();\n\n sliceLineSegments(feature, segmentLength, units, function (segment) {\n results.push(segment);\n });\n });\n return featureCollection(results);\n}\n\n/**\n * Slice Line Segments\n *\n * @private\n * @param {Feature<LineString>} line GeoJSON LineString\n * @param {number} segmentLength how long to make each segment\n * @param {string}[units='kilometers'] units can be degrees, radians, miles, or kilometers\n * @param {Function} callback iterate over sliced line segments\n * @returns {void}\n */\nfunction sliceLineSegments(line, segmentLength, units, callback) {\n var lineLength = length(line, { units: units });\n\n // If the line is shorter than the segment length then the orginal line is returned.\n if (lineLength <= segmentLength) return callback(line);\n\n var numberOfSegments = lineLength / segmentLength;\n\n // If numberOfSegments is integer, no need to plus 1\n if (!Number.isInteger(numberOfSegments)) {\n numberOfSegments = Math.floor(numberOfSegments) + 1;\n }\n\n for (var i = 0; i < numberOfSegments; i++) {\n var outline = lineSliceAlong(\n line,\n segmentLength * i,\n segmentLength * (i + 1),\n { units: units }\n );\n callback(outline, i);\n }\n}\n\nexport { lineChunk };\nexport default lineChunk;\n"]}
@@ -0,0 +1,21 @@
1
+ import { LineString, MultiLineString, Feature, FeatureCollection, GeometryCollection } from 'geojson';
2
+ import { Units } from '@turf/helpers';
3
+
4
+ /**
5
+ * http://turfjs.org/docs/#lineChunk
6
+ */
7
+ declare function lineChunk<T extends LineString | MultiLineString>(
8
+ geojson:
9
+ | Feature<T>
10
+ | FeatureCollection<T>
11
+ | T
12
+ | GeometryCollection
13
+ | Feature<GeometryCollection>,
14
+ segmentLength: number,
15
+ options?: {
16
+ units?: Units;
17
+ reverse?: boolean;
18
+ }
19
+ ): FeatureCollection<LineString>;
20
+
21
+ export { lineChunk as default, lineChunk };
@@ -1,16 +1,10 @@
1
- import {
2
- LineString,
3
- MultiLineString,
4
- GeometryCollection,
5
- Feature,
6
- FeatureCollection,
7
- } from "geojson";
8
- import { Units } from "@turf/helpers";
1
+ import { LineString, MultiLineString, Feature, FeatureCollection, GeometryCollection } from 'geojson';
2
+ import { Units } from '@turf/helpers';
9
3
 
10
4
  /**
11
5
  * http://turfjs.org/docs/#lineChunk
12
6
  */
13
- export default function lineChunk<T extends LineString | MultiLineString>(
7
+ declare function lineChunk<T extends LineString | MultiLineString>(
14
8
  geojson:
15
9
  | Feature<T>
16
10
  | FeatureCollection<T>
@@ -23,3 +17,5 @@ export default function lineChunk<T extends LineString | MultiLineString>(
23
17
  reverse?: boolean;
24
18
  }
25
19
  ): FeatureCollection<LineString>;
20
+
21
+ export { lineChunk as default, lineChunk };
@@ -0,0 +1,49 @@
1
+ // index.js
2
+ import { length } from "@turf/length";
3
+ import { lineSliceAlong } from "@turf/line-slice-along";
4
+ import { flattenEach } from "@turf/meta";
5
+ import { featureCollection, isObject } from "@turf/helpers";
6
+ function lineChunk(geojson, segmentLength, options) {
7
+ options = options || {};
8
+ if (!isObject(options))
9
+ throw new Error("options is invalid");
10
+ var units = options.units;
11
+ var reverse = options.reverse;
12
+ if (!geojson)
13
+ throw new Error("geojson is required");
14
+ if (segmentLength <= 0)
15
+ throw new Error("segmentLength must be greater than 0");
16
+ var results = [];
17
+ flattenEach(geojson, function(feature) {
18
+ if (reverse)
19
+ feature.geometry.coordinates = feature.geometry.coordinates.reverse();
20
+ sliceLineSegments(feature, segmentLength, units, function(segment) {
21
+ results.push(segment);
22
+ });
23
+ });
24
+ return featureCollection(results);
25
+ }
26
+ function sliceLineSegments(line, segmentLength, units, callback) {
27
+ var lineLength = length(line, { units });
28
+ if (lineLength <= segmentLength)
29
+ return callback(line);
30
+ var numberOfSegments = lineLength / segmentLength;
31
+ if (!Number.isInteger(numberOfSegments)) {
32
+ numberOfSegments = Math.floor(numberOfSegments) + 1;
33
+ }
34
+ for (var i = 0; i < numberOfSegments; i++) {
35
+ var outline = lineSliceAlong(
36
+ line,
37
+ segmentLength * i,
38
+ segmentLength * (i + 1),
39
+ { units }
40
+ );
41
+ callback(outline, i);
42
+ }
43
+ }
44
+ var turf_line_chunk_default = lineChunk;
45
+ export {
46
+ turf_line_chunk_default as default,
47
+ lineChunk
48
+ };
49
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../index.js"],"sourcesContent":["import { length } from \"@turf/length\";\nimport { lineSliceAlong } from \"@turf/line-slice-along\";\nimport { flattenEach } from \"@turf/meta\";\nimport { featureCollection, isObject } from \"@turf/helpers\";\n\n/**\n * Divides a {@link LineString} into chunks of a specified length.\n * If the line is shorter than the segment length then the original line is returned.\n *\n * @name lineChunk\n * @param {FeatureCollection|Geometry|Feature<LineString|MultiLineString>} geojson the lines to split\n * @param {number} segmentLength how long to make each segment\n * @param {Object} [options={}] Optional parameters\n * @param {string} [options.units='kilometers'] units can be degrees, radians, miles, or kilometers\n * @param {boolean} [options.reverse=false] reverses coordinates to start the first chunked segment at the end\n * @returns {FeatureCollection<LineString>} collection of line segments\n * @example\n * var line = turf.lineString([[-95, 40], [-93, 45], [-85, 50]]);\n *\n * var chunk = turf.lineChunk(line, 15, {units: 'miles'});\n *\n * //addToMap\n * var addToMap = [chunk];\n */\nfunction lineChunk(geojson, segmentLength, options) {\n // Optional parameters\n options = options || {};\n if (!isObject(options)) throw new Error(\"options is invalid\");\n var units = options.units;\n var reverse = options.reverse;\n\n // Validation\n if (!geojson) throw new Error(\"geojson is required\");\n if (segmentLength <= 0)\n throw new Error(\"segmentLength must be greater than 0\");\n\n // Container\n var results = [];\n\n // Flatten each feature to simple LineString\n flattenEach(geojson, function (feature) {\n // reverses coordinates to start the first chunked segment at the end\n if (reverse)\n feature.geometry.coordinates = feature.geometry.coordinates.reverse();\n\n sliceLineSegments(feature, segmentLength, units, function (segment) {\n results.push(segment);\n });\n });\n return featureCollection(results);\n}\n\n/**\n * Slice Line Segments\n *\n * @private\n * @param {Feature<LineString>} line GeoJSON LineString\n * @param {number} segmentLength how long to make each segment\n * @param {string}[units='kilometers'] units can be degrees, radians, miles, or kilometers\n * @param {Function} callback iterate over sliced line segments\n * @returns {void}\n */\nfunction sliceLineSegments(line, segmentLength, units, callback) {\n var lineLength = length(line, { units: units });\n\n // If the line is shorter than the segment length then the orginal line is returned.\n if (lineLength <= segmentLength) return callback(line);\n\n var numberOfSegments = lineLength / segmentLength;\n\n // If numberOfSegments is integer, no need to plus 1\n if (!Number.isInteger(numberOfSegments)) {\n numberOfSegments = Math.floor(numberOfSegments) + 1;\n }\n\n for (var i = 0; i < numberOfSegments; i++) {\n var outline = lineSliceAlong(\n line,\n segmentLength * i,\n segmentLength * (i + 1),\n { units: units }\n );\n callback(outline, i);\n }\n}\n\nexport { lineChunk };\nexport default lineChunk;\n"],"mappings":";AAAA,SAAS,cAAc;AACvB,SAAS,sBAAsB;AAC/B,SAAS,mBAAmB;AAC5B,SAAS,mBAAmB,gBAAgB;AAqB5C,SAAS,UAAU,SAAS,eAAe,SAAS;AAElD,YAAU,WAAW,CAAC;AACtB,MAAI,CAAC,SAAS,OAAO;AAAG,UAAM,IAAI,MAAM,oBAAoB;AAC5D,MAAI,QAAQ,QAAQ;AACpB,MAAI,UAAU,QAAQ;AAGtB,MAAI,CAAC;AAAS,UAAM,IAAI,MAAM,qBAAqB;AACnD,MAAI,iBAAiB;AACnB,UAAM,IAAI,MAAM,sCAAsC;AAGxD,MAAI,UAAU,CAAC;AAGf,cAAY,SAAS,SAAU,SAAS;AAEtC,QAAI;AACF,cAAQ,SAAS,cAAc,QAAQ,SAAS,YAAY,QAAQ;AAEtE,sBAAkB,SAAS,eAAe,OAAO,SAAU,SAAS;AAClE,cAAQ,KAAK,OAAO;AAAA,IACtB,CAAC;AAAA,EACH,CAAC;AACD,SAAO,kBAAkB,OAAO;AAClC;AAYA,SAAS,kBAAkB,MAAM,eAAe,OAAO,UAAU;AAC/D,MAAI,aAAa,OAAO,MAAM,EAAE,MAAa,CAAC;AAG9C,MAAI,cAAc;AAAe,WAAO,SAAS,IAAI;AAErD,MAAI,mBAAmB,aAAa;AAGpC,MAAI,CAAC,OAAO,UAAU,gBAAgB,GAAG;AACvC,uBAAmB,KAAK,MAAM,gBAAgB,IAAI;AAAA,EACpD;AAEA,WAAS,IAAI,GAAG,IAAI,kBAAkB,KAAK;AACzC,QAAI,UAAU;AAAA,MACZ;AAAA,MACA,gBAAgB;AAAA,MAChB,iBAAiB,IAAI;AAAA,MACrB,EAAE,MAAa;AAAA,IACjB;AACA,aAAS,SAAS,CAAC;AAAA,EACrB;AACF;AAGA,IAAO,0BAAQ;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@turf/line-chunk",
3
- "version": "7.0.0-alpha.2",
3
+ "version": "7.1.0-alpha.7+0ce6ecca0",
4
4
  "description": "turf line-chunk module",
5
5
  "author": "Turf Authors",
6
6
  "contributors": [
@@ -29,45 +29,52 @@
29
29
  "linestring",
30
30
  "line segment"
31
31
  ],
32
- "main": "dist/js/index.js",
33
- "module": "dist/es/index.js",
32
+ "type": "module",
33
+ "main": "dist/cjs/index.cjs",
34
+ "module": "dist/esm/index.js",
35
+ "types": "dist/esm/index.d.ts",
34
36
  "exports": {
35
37
  "./package.json": "./package.json",
36
38
  ".": {
37
- "types": "./index.d.ts",
38
- "import": "./dist/es/index.js",
39
- "require": "./dist/js/index.js"
39
+ "import": {
40
+ "types": "./dist/esm/index.d.ts",
41
+ "default": "./dist/esm/index.js"
42
+ },
43
+ "require": {
44
+ "types": "./dist/cjs/index.d.cts",
45
+ "default": "./dist/cjs/index.cjs"
46
+ }
40
47
  }
41
48
  },
42
- "types": "index.d.ts",
43
49
  "sideEffects": false,
44
50
  "files": [
45
- "dist",
46
- "index.d.ts"
51
+ "dist"
47
52
  ],
48
53
  "scripts": {
49
- "bench": "tsx bench.js",
50
- "build": "rollup -c ../../rollup.config.js && echo '{\"type\":\"module\"}' > dist/es/package.json",
51
- "docs": "tsx ../../scripts/generate-readmes",
52
- "test": "npm-run-all test:*",
53
- "test:tape": "tsx test.js",
54
- "test:types": "tsc --esModuleInterop --noEmit --strict types.ts"
54
+ "bench": "tsx bench.ts",
55
+ "build": "tsup --config ../../tsup.config.ts",
56
+ "docs": "tsx ../../scripts/generate-readmes.ts",
57
+ "test": "npm-run-all --npm-path npm test:*",
58
+ "test:tape": "tsx test.ts",
59
+ "test:types": "tsc --esModuleInterop --module node16 --moduleResolution node16 --noEmit --strict types.ts"
55
60
  },
56
61
  "devDependencies": {
57
- "@turf/truncate": "^7.0.0-alpha.2",
58
- "benchmark": "*",
59
- "load-json-file": "*",
60
- "npm-run-all": "*",
61
- "rollup": "*",
62
- "tape": "*",
63
- "tsx": "*",
64
- "write-json-file": "*"
62
+ "@turf/truncate": "^7.1.0-alpha.7+0ce6ecca0",
63
+ "@types/benchmark": "^2.1.5",
64
+ "@types/tape": "^4.2.32",
65
+ "benchmark": "^2.1.4",
66
+ "load-json-file": "^7.0.1",
67
+ "npm-run-all": "^4.1.5",
68
+ "tape": "^5.7.2",
69
+ "tsup": "^8.0.1",
70
+ "tsx": "^4.6.2",
71
+ "write-json-file": "^5.0.0"
65
72
  },
66
73
  "dependencies": {
67
- "@turf/helpers": "^7.0.0-alpha.2",
68
- "@turf/length": "^7.0.0-alpha.2",
69
- "@turf/line-slice-along": "^7.0.0-alpha.2",
70
- "@turf/meta": "^7.0.0-alpha.2"
74
+ "@turf/helpers": "^7.1.0-alpha.7+0ce6ecca0",
75
+ "@turf/length": "^7.1.0-alpha.7+0ce6ecca0",
76
+ "@turf/line-slice-along": "^7.1.0-alpha.7+0ce6ecca0",
77
+ "@turf/meta": "^7.1.0-alpha.7+0ce6ecca0"
71
78
  },
72
- "gitHead": "dd35b52725945b4fa29a98d9a550733e06cc222e"
79
+ "gitHead": "0ce6ecca05829690270fec6d6bed2003495fe0ea"
73
80
  }
package/dist/es/index.js DELETED
@@ -1,87 +0,0 @@
1
- import length from '@turf/length';
2
- import lineSliceAlong from '@turf/line-slice-along';
3
- import { flattenEach } from '@turf/meta';
4
- import { isObject, featureCollection } from '@turf/helpers';
5
-
6
- /**
7
- * Divides a {@link LineString} into chunks of a specified length.
8
- * If the line is shorter than the segment length then the original line is returned.
9
- *
10
- * @name lineChunk
11
- * @param {FeatureCollection|Geometry|Feature<LineString|MultiLineString>} geojson the lines to split
12
- * @param {number} segmentLength how long to make each segment
13
- * @param {Object} [options={}] Optional parameters
14
- * @param {string} [options.units='kilometers'] units can be degrees, radians, miles, or kilometers
15
- * @param {boolean} [options.reverse=false] reverses coordinates to start the first chunked segment at the end
16
- * @returns {FeatureCollection<LineString>} collection of line segments
17
- * @example
18
- * var line = turf.lineString([[-95, 40], [-93, 45], [-85, 50]]);
19
- *
20
- * var chunk = turf.lineChunk(line, 15, {units: 'miles'});
21
- *
22
- * //addToMap
23
- * var addToMap = [chunk];
24
- */
25
- function lineChunk(geojson, segmentLength, options) {
26
- // Optional parameters
27
- options = options || {};
28
- if (!isObject(options)) throw new Error("options is invalid");
29
- var units = options.units;
30
- var reverse = options.reverse;
31
-
32
- // Validation
33
- if (!geojson) throw new Error("geojson is required");
34
- if (segmentLength <= 0)
35
- throw new Error("segmentLength must be greater than 0");
36
-
37
- // Container
38
- var results = [];
39
-
40
- // Flatten each feature to simple LineString
41
- flattenEach(geojson, function (feature) {
42
- // reverses coordinates to start the first chunked segment at the end
43
- if (reverse)
44
- feature.geometry.coordinates = feature.geometry.coordinates.reverse();
45
-
46
- sliceLineSegments(feature, segmentLength, units, function (segment) {
47
- results.push(segment);
48
- });
49
- });
50
- return featureCollection(results);
51
- }
52
-
53
- /**
54
- * Slice Line Segments
55
- *
56
- * @private
57
- * @param {Feature<LineString>} line GeoJSON LineString
58
- * @param {number} segmentLength how long to make each segment
59
- * @param {string}[units='kilometers'] units can be degrees, radians, miles, or kilometers
60
- * @param {Function} callback iterate over sliced line segments
61
- * @returns {void}
62
- */
63
- function sliceLineSegments(line, segmentLength, units, callback) {
64
- var lineLength = length(line, { units: units });
65
-
66
- // If the line is shorter than the segment length then the orginal line is returned.
67
- if (lineLength <= segmentLength) return callback(line);
68
-
69
- var numberOfSegments = lineLength / segmentLength;
70
-
71
- // If numberOfSegments is integer, no need to plus 1
72
- if (!Number.isInteger(numberOfSegments)) {
73
- numberOfSegments = Math.floor(numberOfSegments) + 1;
74
- }
75
-
76
- for (var i = 0; i < numberOfSegments; i++) {
77
- var outline = lineSliceAlong(
78
- line,
79
- segmentLength * i,
80
- segmentLength * (i + 1),
81
- { units: units }
82
- );
83
- callback(outline, i);
84
- }
85
- }
86
-
87
- export default lineChunk;
@@ -1 +0,0 @@
1
- {"type":"module"}
package/dist/js/index.js DELETED
@@ -1,95 +0,0 @@
1
- 'use strict';
2
-
3
- var length = require('@turf/length');
4
- var lineSliceAlong = require('@turf/line-slice-along');
5
- var meta = require('@turf/meta');
6
- var helpers = require('@turf/helpers');
7
-
8
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
9
-
10
- var length__default = /*#__PURE__*/_interopDefaultLegacy(length);
11
- var lineSliceAlong__default = /*#__PURE__*/_interopDefaultLegacy(lineSliceAlong);
12
-
13
- /**
14
- * Divides a {@link LineString} into chunks of a specified length.
15
- * If the line is shorter than the segment length then the original line is returned.
16
- *
17
- * @name lineChunk
18
- * @param {FeatureCollection|Geometry|Feature<LineString|MultiLineString>} geojson the lines to split
19
- * @param {number} segmentLength how long to make each segment
20
- * @param {Object} [options={}] Optional parameters
21
- * @param {string} [options.units='kilometers'] units can be degrees, radians, miles, or kilometers
22
- * @param {boolean} [options.reverse=false] reverses coordinates to start the first chunked segment at the end
23
- * @returns {FeatureCollection<LineString>} collection of line segments
24
- * @example
25
- * var line = turf.lineString([[-95, 40], [-93, 45], [-85, 50]]);
26
- *
27
- * var chunk = turf.lineChunk(line, 15, {units: 'miles'});
28
- *
29
- * //addToMap
30
- * var addToMap = [chunk];
31
- */
32
- function lineChunk(geojson, segmentLength, options) {
33
- // Optional parameters
34
- options = options || {};
35
- if (!helpers.isObject(options)) throw new Error("options is invalid");
36
- var units = options.units;
37
- var reverse = options.reverse;
38
-
39
- // Validation
40
- if (!geojson) throw new Error("geojson is required");
41
- if (segmentLength <= 0)
42
- throw new Error("segmentLength must be greater than 0");
43
-
44
- // Container
45
- var results = [];
46
-
47
- // Flatten each feature to simple LineString
48
- meta.flattenEach(geojson, function (feature) {
49
- // reverses coordinates to start the first chunked segment at the end
50
- if (reverse)
51
- feature.geometry.coordinates = feature.geometry.coordinates.reverse();
52
-
53
- sliceLineSegments(feature, segmentLength, units, function (segment) {
54
- results.push(segment);
55
- });
56
- });
57
- return helpers.featureCollection(results);
58
- }
59
-
60
- /**
61
- * Slice Line Segments
62
- *
63
- * @private
64
- * @param {Feature<LineString>} line GeoJSON LineString
65
- * @param {number} segmentLength how long to make each segment
66
- * @param {string}[units='kilometers'] units can be degrees, radians, miles, or kilometers
67
- * @param {Function} callback iterate over sliced line segments
68
- * @returns {void}
69
- */
70
- function sliceLineSegments(line, segmentLength, units, callback) {
71
- var lineLength = length__default['default'](line, { units: units });
72
-
73
- // If the line is shorter than the segment length then the orginal line is returned.
74
- if (lineLength <= segmentLength) return callback(line);
75
-
76
- var numberOfSegments = lineLength / segmentLength;
77
-
78
- // If numberOfSegments is integer, no need to plus 1
79
- if (!Number.isInteger(numberOfSegments)) {
80
- numberOfSegments = Math.floor(numberOfSegments) + 1;
81
- }
82
-
83
- for (var i = 0; i < numberOfSegments; i++) {
84
- var outline = lineSliceAlong__default['default'](
85
- line,
86
- segmentLength * i,
87
- segmentLength * (i + 1),
88
- { units: units }
89
- );
90
- callback(outline, i);
91
- }
92
- }
93
-
94
- module.exports = lineChunk;
95
- module.exports.default = lineChunk;