@loaders.gl/mvt 4.0.0-alpha.8 → 4.0.0-beta.1

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.
Files changed (65) hide show
  1. package/dist/dist.min.js +228 -24
  2. package/dist/es5/index.js +7 -0
  3. package/dist/es5/index.js.map +1 -1
  4. package/dist/es5/lib/parse-mvt.js +8 -7
  5. package/dist/es5/lib/parse-mvt.js.map +1 -1
  6. package/dist/es5/lib/parse-tilejson.js +186 -0
  7. package/dist/es5/lib/parse-tilejson.js.map +1 -0
  8. package/dist/es5/lib/types.js.map +1 -1
  9. package/dist/es5/mvt-loader.js +10 -11
  10. package/dist/es5/mvt-loader.js.map +1 -1
  11. package/dist/es5/tilejson-loader.js +50 -0
  12. package/dist/es5/tilejson-loader.js.map +1 -0
  13. package/dist/esm/index.js +1 -0
  14. package/dist/esm/index.js.map +1 -1
  15. package/dist/esm/lib/parse-mvt.js +8 -7
  16. package/dist/esm/lib/parse-mvt.js.map +1 -1
  17. package/dist/esm/lib/parse-tilejson.js +157 -0
  18. package/dist/esm/lib/parse-tilejson.js.map +1 -0
  19. package/dist/esm/lib/types.js.map +1 -1
  20. package/dist/esm/mvt-loader.js +10 -11
  21. package/dist/esm/mvt-loader.js.map +1 -1
  22. package/dist/esm/tilejson-loader.js +25 -0
  23. package/dist/esm/tilejson-loader.js.map +1 -0
  24. package/dist/index.d.ts +4 -0
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/lib/parse-mvt.d.ts +3 -3
  27. package/dist/lib/parse-mvt.d.ts.map +1 -1
  28. package/dist/lib/parse-tilejson.d.ts +46 -0
  29. package/dist/lib/parse-tilejson.d.ts.map +1 -0
  30. package/dist/lib/types.d.ts +4 -4
  31. package/dist/lib/types.d.ts.map +1 -1
  32. package/dist/mvt-loader.d.ts +5 -2
  33. package/dist/mvt-loader.d.ts.map +1 -1
  34. package/dist/mvt-worker.js +40 -26
  35. package/dist/tilejson-loader.d.ts +10 -0
  36. package/dist/tilejson-loader.d.ts.map +1 -0
  37. package/package.json +5 -5
  38. package/src/index.ts +5 -0
  39. package/src/lib/parse-mvt.ts +15 -11
  40. package/src/lib/parse-tilejson.ts +314 -0
  41. package/src/lib/types.ts +4 -4
  42. package/src/mvt-loader.ts +27 -13
  43. package/src/tilejson-loader.ts +39 -0
  44. package/dist/bundle.js +0 -5
  45. package/dist/helpers/binary-util-functions.js +0 -118
  46. package/dist/helpers/mapbox-util-functions.js +0 -82
  47. package/dist/index.js +0 -9
  48. package/dist/lib/binary-vector-tile/vector-tile-feature.js +0 -156
  49. package/dist/lib/binary-vector-tile/vector-tile-layer.js +0 -91
  50. package/dist/lib/binary-vector-tile/vector-tile.js +0 -29
  51. package/dist/lib/geojson-tiler/clip.js +0 -209
  52. package/dist/lib/geojson-tiler/convert.js +0 -134
  53. package/dist/lib/geojson-tiler/feature.js +0 -46
  54. package/dist/lib/geojson-tiler/geojson-tiler.js +0 -210
  55. package/dist/lib/geojson-tiler/simplify.js +0 -68
  56. package/dist/lib/geojson-tiler/tile.js +0 -125
  57. package/dist/lib/geojson-tiler/transform.js +0 -43
  58. package/dist/lib/geojson-tiler/wrap.js +0 -86
  59. package/dist/lib/mapbox-vector-tile/vector-tile-feature.js +0 -170
  60. package/dist/lib/mapbox-vector-tile/vector-tile-layer.js +0 -89
  61. package/dist/lib/mapbox-vector-tile/vector-tile.js +0 -29
  62. package/dist/lib/parse-mvt.js +0 -167
  63. package/dist/lib/types.js +0 -2
  64. package/dist/mvt-loader.js +0 -47
  65. package/dist/workers/mvt-worker.js +0 -5
@@ -1,118 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.readTag = exports.readFeature = exports.project = exports.classifyRings = void 0;
4
- const polygon_1 = require("@math.gl/polygon");
5
- /**
6
- * Classifies an array of rings into polygons with outer rings and holes
7
- * The function also detects holes which have zero area and
8
- * removes them. In doing so it modifies the input
9
- * `geom.data` array to remove the unneeded data
10
- *
11
- * @param geometry
12
- * @returns object
13
- */
14
- // eslint-disable-next-line max-statements
15
- function classifyRings(geom) {
16
- const len = geom.indices.length;
17
- const type = 'Polygon';
18
- if (len <= 1) {
19
- return {
20
- type,
21
- data: geom.data,
22
- areas: [[(0, polygon_1.getPolygonSignedArea)(geom.data)]],
23
- indices: [geom.indices]
24
- };
25
- }
26
- const areas = [];
27
- const polygons = [];
28
- let ringAreas = [];
29
- let polygon = [];
30
- let ccw;
31
- let offset = 0;
32
- for (let endIndex, i = 0, startIndex; i < len; i++) {
33
- startIndex = geom.indices[i] - offset;
34
- endIndex = geom.indices[i + 1] - offset || geom.data.length;
35
- const shape = geom.data.slice(startIndex, endIndex);
36
- const area = (0, polygon_1.getPolygonSignedArea)(shape);
37
- if (area === 0) {
38
- // This polygon has no area, so remove it from the shape
39
- // Remove the section from the data array
40
- const before = geom.data.slice(0, startIndex);
41
- const after = geom.data.slice(endIndex);
42
- geom.data = before.concat(after);
43
- // Need to offset any remaining indices as we have
44
- // modified the data buffer
45
- offset += endIndex - startIndex;
46
- // Do not add this index to the output and process next shape
47
- continue; // eslint-disable-line no-continue
48
- }
49
- if (ccw === undefined)
50
- ccw = area < 0;
51
- if (ccw === area < 0) {
52
- if (polygon.length) {
53
- areas.push(ringAreas);
54
- polygons.push(polygon);
55
- }
56
- polygon = [startIndex];
57
- ringAreas = [area];
58
- }
59
- else {
60
- ringAreas.push(area);
61
- polygon.push(startIndex);
62
- }
63
- }
64
- if (ringAreas)
65
- areas.push(ringAreas);
66
- if (polygon.length)
67
- polygons.push(polygon);
68
- return { type, areas, indices: polygons, data: geom.data };
69
- }
70
- exports.classifyRings = classifyRings;
71
- /**
72
- *
73
- * @param data
74
- * @param x0
75
- * @param y0
76
- * @param size
77
- */
78
- function project(data, x0, y0, size) {
79
- for (let j = 0, jl = data.length; j < jl; j += 2) {
80
- data[j] = ((data[j] + x0) * 360) / size - 180;
81
- const y2 = 180 - ((data[j + 1] + y0) * 360) / size;
82
- data[j + 1] = (360 / Math.PI) * Math.atan(Math.exp((y2 * Math.PI) / 180)) - 90;
83
- }
84
- }
85
- exports.project = project;
86
- /**
87
- * All code below is unchanged from the original Mapbox implemenation
88
- *
89
- * @param tag
90
- * @param feature
91
- * @param pbf
92
- */
93
- function readFeature(tag, feature, pbf) {
94
- if (feature && pbf) {
95
- if (tag === 1)
96
- feature.id = pbf.readVarint();
97
- else if (tag === 2)
98
- readTag(pbf, feature);
99
- else if (tag === 3)
100
- feature.type = pbf.readVarint();
101
- else if (tag === 4)
102
- feature._geometry = pbf.pos;
103
- }
104
- }
105
- exports.readFeature = readFeature;
106
- /**
107
- * @param pbf
108
- * @param feature
109
- */
110
- function readTag(pbf, feature) {
111
- const end = pbf.readVarint() + pbf.pos;
112
- while (pbf.pos < end) {
113
- const key = feature._keys[pbf.readVarint()];
114
- const value = feature._values[pbf.readVarint()];
115
- feature.properties[key] = value;
116
- }
117
- }
118
- exports.readTag = readTag;
@@ -1,82 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.readTag = exports.readFeature = exports.signedArea = exports.classifyRings = void 0;
4
- /**
5
- * Classifies an array of rings into polygons with outer rings and holes
6
- * @param rings
7
- * @returns polygons
8
- */
9
- function classifyRings(rings) {
10
- const len = rings.length;
11
- if (len <= 1)
12
- return [rings];
13
- const polygons = [];
14
- let polygon;
15
- let ccw;
16
- for (let i = 0; i < len; i++) {
17
- const area = signedArea(rings[i]);
18
- if (area === 0)
19
- continue; // eslint-disable-line no-continue
20
- if (ccw === undefined)
21
- ccw = area < 0;
22
- if (ccw === area < 0) {
23
- if (polygon)
24
- polygons.push(polygon);
25
- polygon = [rings[i]];
26
- }
27
- else if (polygon)
28
- polygon.push(rings[i]);
29
- }
30
- if (polygon)
31
- polygons.push(polygon);
32
- return polygons;
33
- }
34
- exports.classifyRings = classifyRings;
35
- /**
36
- *
37
- * @param ring
38
- * @returns sum
39
- */
40
- function signedArea(ring) {
41
- let sum = 0;
42
- for (let i = 0, j = ring.length - 1, p1, p2; i < ring.length; j = i++) {
43
- p1 = ring[i];
44
- p2 = ring[j];
45
- sum += (p2[0] - p1[0]) * (p1[1] + p2[1]);
46
- }
47
- return sum;
48
- }
49
- exports.signedArea = signedArea;
50
- /**
51
- *
52
- * @param tag
53
- * @param feature
54
- * @param pbf
55
- */
56
- function readFeature(tag, feature, pbf) {
57
- if (feature && pbf) {
58
- if (tag === 1)
59
- feature.id = pbf.readVarint();
60
- else if (tag === 2)
61
- readTag(pbf, feature);
62
- else if (tag === 3)
63
- feature.type = pbf.readVarint();
64
- else if (tag === 4)
65
- feature._geometry = pbf.pos;
66
- }
67
- }
68
- exports.readFeature = readFeature;
69
- /**
70
- *
71
- * @param pbf
72
- * @param feature
73
- */
74
- function readTag(pbf, feature) {
75
- const end = pbf.readVarint() + pbf.pos;
76
- while (pbf.pos < end) {
77
- const key = feature._keys[pbf.readVarint()];
78
- const value = feature._values[pbf.readVarint()];
79
- feature.properties[key] = value;
80
- }
81
- }
82
- exports.readTag = readTag;
package/dist/index.js DELETED
@@ -1,9 +0,0 @@
1
- "use strict";
2
- // loaders.gl, MIT license
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.GeoJSONTiler = exports.MVTWorkerLoader = exports.MVTLoader = void 0;
5
- var mvt_loader_1 = require("./mvt-loader");
6
- Object.defineProperty(exports, "MVTLoader", { enumerable: true, get: function () { return mvt_loader_1.MVTLoader; } });
7
- Object.defineProperty(exports, "MVTWorkerLoader", { enumerable: true, get: function () { return mvt_loader_1.MVTWorkerLoader; } });
8
- var geojson_tiler_1 = require("./lib/geojson-tiler/geojson-tiler");
9
- Object.defineProperty(exports, "GeoJSONTiler", { enumerable: true, get: function () { return geojson_tiler_1.GeoJSONTiler; } });
@@ -1,156 +0,0 @@
1
- "use strict";
2
- // This code is forked from https://github.com/mapbox/vector-tile-js under BSD 3-clause license.
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.TEST_EXPORTS = void 0;
5
- const binary_util_functions_1 = require("../../helpers/binary-util-functions");
6
- // Reduce GC by reusing variables
7
- let endPos;
8
- let cmd;
9
- let cmdLen;
10
- let length;
11
- let x;
12
- let y;
13
- let i;
14
- exports.TEST_EXPORTS = {
15
- classifyRings: binary_util_functions_1.classifyRings
16
- };
17
- class VectorTileFeature {
18
- // eslint-disable-next-line max-params
19
- constructor(pbf, end, extent, keys, values, geometryInfo) {
20
- // Public
21
- this.properties = {};
22
- this.extent = extent;
23
- this.type = 0;
24
- this.id = null;
25
- // Private
26
- this._pbf = pbf;
27
- this._geometry = -1;
28
- this._keys = keys;
29
- this._values = values;
30
- this._geometryInfo = geometryInfo;
31
- pbf.readFields(binary_util_functions_1.readFeature, this, end);
32
- }
33
- // eslint-disable-next-line complexity, max-statements
34
- loadGeometry() {
35
- const pbf = this._pbf;
36
- pbf.pos = this._geometry;
37
- endPos = pbf.readVarint() + pbf.pos;
38
- cmd = 1;
39
- length = 0;
40
- x = 0;
41
- y = 0;
42
- i = 0;
43
- // Note: I attempted to replace the `data` array with a
44
- // Float32Array, but performance was worse, both using
45
- // `set()` and direct index access. Also, we cannot
46
- // know how large the buffer should be, so it would
47
- // increase memory usage
48
- const indices = []; // Indices where geometries start
49
- const data = []; // Flat array of coordinate data
50
- while (pbf.pos < endPos) {
51
- if (length <= 0) {
52
- cmdLen = pbf.readVarint();
53
- cmd = cmdLen & 0x7;
54
- length = cmdLen >> 3;
55
- }
56
- length--;
57
- if (cmd === 1 || cmd === 2) {
58
- x += pbf.readSVarint();
59
- y += pbf.readSVarint();
60
- if (cmd === 1) {
61
- // New line
62
- indices.push(i);
63
- }
64
- data.push(x, y);
65
- i += 2;
66
- }
67
- else if (cmd === 7) {
68
- // Workaround for https://github.com/mapbox/mapnik-vector-tile/issues/90
69
- if (i > 0) {
70
- const start = indices[indices.length - 1]; // start index of polygon
71
- data.push(data[start], data[start + 1]); // closePolygon
72
- i += 2;
73
- }
74
- }
75
- else {
76
- throw new Error(`unknown command ${cmd}`);
77
- }
78
- }
79
- return { data, indices };
80
- }
81
- /**
82
- *
83
- * @param transform
84
- * @returns result
85
- */
86
- _toBinaryCoordinates(transform) {
87
- // Expands the protobuf data to an intermediate Flat GeoJSON
88
- // data format, which maps closely to the binary data buffers.
89
- // It is similar to GeoJSON, but rather than storing the coordinates
90
- // in multidimensional arrays, we have a 1D `data` with all the
91
- // coordinates, and then index into this using the `indices`
92
- // parameter, e.g.
93
- //
94
- // geometry: {
95
- // type: 'Point', data: [1,2], indices: [0]
96
- // }
97
- // geometry: {
98
- // type: 'LineString', data: [1,2,3,4,...], indices: [0]
99
- // }
100
- // geometry: {
101
- // type: 'Polygon', data: [1,2,3,4,...], indices: [[0, 2]]
102
- // }
103
- // Thus the indices member lets us look up the relevant range
104
- // from the data array.
105
- // The Multi* versions of the above types share the same data
106
- // structure, just with multiple elements in the indices array
107
- const geom = this.loadGeometry();
108
- let geometry;
109
- // Apply the supplied transformation to data
110
- transform(geom.data, this);
111
- const coordLength = 2;
112
- // eslint-disable-next-line default-case
113
- switch (this.type) {
114
- case 1: // Point
115
- this._geometryInfo.pointFeaturesCount++;
116
- this._geometryInfo.pointPositionsCount += geom.indices.length;
117
- geometry = { type: 'Point', ...geom };
118
- break;
119
- case 2: // LineString
120
- this._geometryInfo.lineFeaturesCount++;
121
- this._geometryInfo.linePathsCount += geom.indices.length;
122
- this._geometryInfo.linePositionsCount += geom.data.length / coordLength;
123
- geometry = { type: 'LineString', ...geom };
124
- break;
125
- case 3: // Polygon
126
- geometry = (0, binary_util_functions_1.classifyRings)(geom);
127
- // Unlike Point & LineString geom.indices is a 2D array, thanks
128
- // to the classifyRings method
129
- this._geometryInfo.polygonFeaturesCount++;
130
- this._geometryInfo.polygonObjectsCount += geometry.indices.length;
131
- for (const indices of geometry.indices) {
132
- this._geometryInfo.polygonRingsCount += indices.length;
133
- }
134
- this._geometryInfo.polygonPositionsCount += geometry.data.length / coordLength;
135
- break;
136
- default:
137
- throw new Error(`Invalid geometry type: ${this.type}`);
138
- }
139
- const result = { type: 'Feature', geometry, properties: this.properties };
140
- if (this.id !== null) {
141
- result.id = this.id;
142
- }
143
- return result;
144
- }
145
- toBinaryCoordinates(options) {
146
- if (typeof options === 'function') {
147
- return this._toBinaryCoordinates(options);
148
- }
149
- const { x, y, z } = options;
150
- const size = this.extent * Math.pow(2, z);
151
- const x0 = this.extent * x;
152
- const y0 = this.extent * y;
153
- return this._toBinaryCoordinates((data) => (0, binary_util_functions_1.project)(data, x0, y0, size));
154
- }
155
- }
156
- exports.default = VectorTileFeature;
@@ -1,91 +0,0 @@
1
- "use strict";
2
- /* eslint-disable indent */
3
- // This code is forked from https://github.com/mapbox/vector-tile-js under BSD 3-clause license.
4
- var __importDefault = (this && this.__importDefault) || function (mod) {
5
- return (mod && mod.__esModule) ? mod : { "default": mod };
6
- };
7
- Object.defineProperty(exports, "__esModule", { value: true });
8
- const vector_tile_feature_1 = __importDefault(require("./vector-tile-feature"));
9
- class VectorTileLayer {
10
- constructor(pbf, end) {
11
- // Public
12
- this.version = 1;
13
- this.name = '';
14
- this.extent = 4096;
15
- this.length = 0;
16
- // Private
17
- this._pbf = pbf;
18
- this._keys = [];
19
- this._values = [];
20
- this._features = [];
21
- pbf.readFields(readLayer, this, end);
22
- this.length = this._features.length;
23
- }
24
- /**
25
- * return feature `i` from this layer as a `VectorTileFeature`
26
- *
27
- * @param index
28
- * @param geometryInfo
29
- * @returns {VectorTileFeature}
30
- */
31
- feature(i, geometryInfo) {
32
- if (i < 0 || i >= this._features.length) {
33
- throw new Error('feature index out of bounds');
34
- }
35
- this._pbf.pos = this._features[i];
36
- const end = this._pbf.readVarint() + this._pbf.pos;
37
- return new vector_tile_feature_1.default(this._pbf, end, this.extent, this._keys, this._values, geometryInfo);
38
- }
39
- }
40
- exports.default = VectorTileLayer;
41
- /**
42
- *
43
- * @param tag
44
- * @param layer
45
- * @param pbf
46
- */
47
- function readLayer(tag, layer, pbf) {
48
- if (layer && pbf) {
49
- if (tag === 15)
50
- layer.version = pbf.readVarint();
51
- else if (tag === 1)
52
- layer.name = pbf.readString();
53
- else if (tag === 5)
54
- layer.extent = pbf.readVarint();
55
- else if (tag === 2)
56
- layer._features.push(pbf.pos);
57
- else if (tag === 3)
58
- layer._keys.push(pbf.readString());
59
- else if (tag === 4)
60
- layer._values.push(readValueMessage(pbf));
61
- }
62
- }
63
- /**
64
- *
65
- * @param pbf
66
- * @returns value
67
- */
68
- function readValueMessage(pbf) {
69
- let value = null;
70
- const end = pbf.readVarint() + pbf.pos;
71
- while (pbf.pos < end) {
72
- const tag = pbf.readVarint() >> 3;
73
- value =
74
- tag === 1
75
- ? pbf.readString()
76
- : tag === 2
77
- ? pbf.readFloat()
78
- : tag === 3
79
- ? pbf.readDouble()
80
- : tag === 4
81
- ? pbf.readVarint64()
82
- : tag === 5
83
- ? pbf.readVarint()
84
- : tag === 6
85
- ? pbf.readSVarint()
86
- : tag === 7
87
- ? pbf.readBoolean()
88
- : null;
89
- }
90
- return value;
91
- }
@@ -1,29 +0,0 @@
1
- "use strict";
2
- // This code is forked from https://github.com/mapbox/vector-tile-js under BSD 3-clause license.
3
- var __importDefault = (this && this.__importDefault) || function (mod) {
4
- return (mod && mod.__esModule) ? mod : { "default": mod };
5
- };
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- const vector_tile_layer_1 = __importDefault(require("./vector-tile-layer"));
8
- class VectorTile {
9
- constructor(pbf, end) {
10
- this.layers = pbf.readFields(readTile, {}, end);
11
- }
12
- }
13
- exports.default = VectorTile;
14
- /**
15
- *
16
- * @param tag
17
- * @param layers
18
- * @param pbf
19
- */
20
- function readTile(tag, layers, pbf) {
21
- if (tag === 3) {
22
- if (pbf) {
23
- const layer = new vector_tile_layer_1.default(pbf, pbf.readVarint() + pbf.pos);
24
- if (layer.length && layers) {
25
- layers[layer.name] = layer;
26
- }
27
- }
28
- }
29
- }
@@ -1,209 +0,0 @@
1
- "use strict";
2
- // loaders.gl, MIT license
3
- // Forked from https://github.com/mapbox/geojson-vt under compatible ISC license
4
- Object.defineProperty(exports, "__esModule", { value: true });
5
- exports.clip = void 0;
6
- const feature_1 = require("./feature");
7
- /* eslint-disable no-continue */
8
- /**
9
- * Clip features between two vertical or horizontal axis-parallel lines:
10
- * | |
11
- * ___|___ | /
12
- * / | \____|____/
13
- * | |
14
- *
15
- * @param k1 and k2 are the line coordinates
16
- * @param axis: 0 for x, 1 for y
17
- * @param minAll and maxAll: minimum and maximum coordinate value for all features
18
- */
19
- // eslint-disable-next-line max-params, complexity, max-statements
20
- function clip(features, scale, k1, k2, axis, minAll, maxAll, options) {
21
- k1 /= scale;
22
- k2 /= scale;
23
- if (minAll >= k1 && maxAll < k2) {
24
- return features;
25
- }
26
- // trivial accept
27
- else if (maxAll < k1 || minAll >= k2) {
28
- return null; // trivial reject
29
- }
30
- const clipped = [];
31
- for (const feature of features) {
32
- const geometry = feature.geometry;
33
- let type = feature.type;
34
- const min = axis === 0 ? feature.minX : feature.minY;
35
- const max = axis === 0 ? feature.maxX : feature.maxY;
36
- if (min >= k1 && max < k2) {
37
- // trivial accept
38
- clipped.push(feature);
39
- continue;
40
- }
41
- else if (max < k1 || min >= k2) {
42
- // trivial reject
43
- continue;
44
- }
45
- let newGeometry = [];
46
- if (type === 'Point' || type === 'MultiPoint') {
47
- clipPoints(geometry, newGeometry, k1, k2, axis);
48
- }
49
- else if (type === 'LineString') {
50
- clipLine(geometry, newGeometry, k1, k2, axis, false, options.lineMetrics);
51
- }
52
- else if (type === 'MultiLineString') {
53
- clipLines(geometry, newGeometry, k1, k2, axis, false);
54
- }
55
- else if (type === 'Polygon') {
56
- clipLines(geometry, newGeometry, k1, k2, axis, true);
57
- }
58
- else if (type === 'MultiPolygon') {
59
- for (const polygon of geometry) {
60
- const newPolygon = [];
61
- clipLines(polygon, newPolygon, k1, k2, axis, true);
62
- if (newPolygon.length) {
63
- newGeometry.push(newPolygon);
64
- }
65
- }
66
- }
67
- if (newGeometry.length) {
68
- if (options.lineMetrics && type === 'LineString') {
69
- for (const line of newGeometry) {
70
- clipped.push((0, feature_1.createFeature)(feature.id, type, line, feature.tags));
71
- }
72
- continue;
73
- }
74
- if (type === 'LineString' || type === 'MultiLineString') {
75
- if (newGeometry.length === 1) {
76
- type = 'LineString';
77
- // @ts-expect-error TODO - use proper GeoJSON geometry types
78
- newGeometry = newGeometry[0];
79
- }
80
- else {
81
- type = 'MultiLineString';
82
- }
83
- }
84
- if (type === 'Point' || type === 'MultiPoint') {
85
- type = newGeometry.length === 3 ? 'Point' : 'MultiPoint';
86
- }
87
- clipped.push((0, feature_1.createFeature)(feature.id, type, newGeometry, feature.tags));
88
- }
89
- }
90
- return clipped.length ? clipped : null;
91
- }
92
- exports.clip = clip;
93
- function clipPoints(geom, newGeom, k1, k2, axis) {
94
- for (let i = 0; i < geom.length; i += 3) {
95
- const a = geom[i + axis];
96
- if (a >= k1 && a <= k2) {
97
- addPoint(newGeom, geom[i], geom[i + 1], geom[i + 2]);
98
- }
99
- }
100
- }
101
- // eslint-disable-next-line max-params, complexity, max-statements
102
- function clipLine(geom, newGeom, k1, k2, axis, isPolygon, trackMetrics) {
103
- let slice = newSlice(geom);
104
- const intersect = axis === 0 ? intersectX : intersectY;
105
- let len = geom.start;
106
- let segLen;
107
- let t;
108
- for (let i = 0; i < geom.length - 3; i += 3) {
109
- const ax = geom[i];
110
- const ay = geom[i + 1];
111
- const az = geom[i + 2];
112
- const bx = geom[i + 3];
113
- const by = geom[i + 4];
114
- const a = axis === 0 ? ax : ay;
115
- const b = axis === 0 ? bx : by;
116
- let exited = false;
117
- if (trackMetrics) {
118
- segLen = Math.sqrt(Math.pow(ax - bx, 2) + Math.pow(ay - by, 2));
119
- }
120
- if (a < k1) {
121
- // ---|--> | (line enters the clip region from the left)
122
- if (b > k1) {
123
- t = intersect(slice, ax, ay, bx, by, k1);
124
- if (trackMetrics) {
125
- slice.start = len + segLen * t;
126
- }
127
- }
128
- }
129
- else if (a > k2) {
130
- // | <--|--- (line enters the clip region from the right)
131
- if (b < k2) {
132
- t = intersect(slice, ax, ay, bx, by, k2);
133
- if (trackMetrics) {
134
- slice.start = len + segLen * t;
135
- }
136
- }
137
- }
138
- else {
139
- addPoint(slice, ax, ay, az);
140
- }
141
- if (b < k1 && a >= k1) {
142
- // <--|--- | or <--|-----|--- (line exits the clip region on the left)
143
- t = intersect(slice, ax, ay, bx, by, k1);
144
- exited = true;
145
- }
146
- if (b > k2 && a <= k2) {
147
- // | ---|--> or ---|-----|--> (line exits the clip region on the right)
148
- t = intersect(slice, ax, ay, bx, by, k2);
149
- exited = true;
150
- }
151
- if (!isPolygon && exited) {
152
- if (trackMetrics) {
153
- slice.end = len + segLen * t;
154
- }
155
- newGeom.push(slice);
156
- slice = newSlice(geom);
157
- }
158
- if (trackMetrics) {
159
- len += segLen;
160
- }
161
- }
162
- // add the last point
163
- let last = geom.length - 3;
164
- const ax = geom[last];
165
- const ay = geom[last + 1];
166
- const az = geom[last + 2];
167
- const a = axis === 0 ? ax : ay;
168
- if (a >= k1 && a <= k2)
169
- addPoint(slice, ax, ay, az);
170
- // close the polygon if its endpoints are not the same after clipping
171
- last = slice.length - 3;
172
- if (isPolygon && last >= 3 && (slice[last] !== slice[0] || slice[last + 1] !== slice[1])) {
173
- addPoint(slice, slice[0], slice[1], slice[2]);
174
- }
175
- // add the final slice
176
- if (slice.length) {
177
- newGeom.push(slice);
178
- }
179
- }
180
- class Slice extends Array {
181
- }
182
- function newSlice(line) {
183
- const slice = [];
184
- slice.size = line.size;
185
- slice.start = line.start;
186
- slice.end = line.end;
187
- return slice;
188
- }
189
- // eslint-disable-next-line max-params
190
- function clipLines(geom, newGeom, k1, k2, axis, isPolygon) {
191
- for (const line of geom) {
192
- clipLine(line, newGeom, k1, k2, axis, isPolygon, false);
193
- }
194
- }
195
- function addPoint(out, x, y, z) {
196
- out.push(x, y, z);
197
- }
198
- // eslint-disable-next-line max-params
199
- function intersectX(out, ax, ay, bx, by, x) {
200
- const t = (x - ax) / (bx - ax);
201
- addPoint(out, x, ay + (by - ay) * t, 1);
202
- return t;
203
- }
204
- // eslint-disable-next-line max-params
205
- function intersectY(out, ax, ay, bx, by, y) {
206
- const t = (y - ay) / (by - ay);
207
- addPoint(out, ax + (bx - ax) * t, y, 1);
208
- return t;
209
- }