@loaders.gl/mvt 3.1.0-alpha.3 → 3.1.0-beta.2

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 (129) hide show
  1. package/dist/bundle.d.ts +2 -0
  2. package/dist/bundle.d.ts.map +1 -0
  3. package/dist/bundle.js +2276 -4
  4. package/dist/es5/bundle.js +7 -0
  5. package/dist/es5/bundle.js.map +1 -0
  6. package/dist/es5/helpers/binary-util-functions.js +95 -0
  7. package/dist/es5/helpers/binary-util-functions.js.map +1 -0
  8. package/dist/es5/helpers/mapbox-util-functions.js +62 -0
  9. package/dist/es5/helpers/mapbox-util-functions.js.map +1 -0
  10. package/dist/es5/index.js +20 -0
  11. package/dist/es5/index.js.map +1 -0
  12. package/dist/{lib → es5/lib}/binary-vector-tile/LICENSE.txt +0 -0
  13. package/dist/es5/lib/binary-vector-tile/features-to-binary.js +338 -0
  14. package/dist/es5/lib/binary-vector-tile/features-to-binary.js.map +1 -0
  15. package/dist/es5/lib/binary-vector-tile/vector-tile-feature.js +162 -0
  16. package/dist/es5/lib/binary-vector-tile/vector-tile-feature.js.map +1 -0
  17. package/dist/es5/lib/binary-vector-tile/vector-tile-layer.js +69 -0
  18. package/dist/es5/lib/binary-vector-tile/vector-tile-layer.js.map +1 -0
  19. package/dist/es5/lib/binary-vector-tile/vector-tile.js +35 -0
  20. package/dist/es5/lib/binary-vector-tile/vector-tile.js.map +1 -0
  21. package/dist/{lib → es5/lib}/mapbox-vector-tile/LICENSE.txt +0 -0
  22. package/dist/es5/lib/mapbox-vector-tile/vector-tile-feature.js +207 -0
  23. package/dist/es5/lib/mapbox-vector-tile/vector-tile-feature.js.map +1 -0
  24. package/dist/es5/lib/mapbox-vector-tile/vector-tile-layer.js +69 -0
  25. package/dist/es5/lib/mapbox-vector-tile/vector-tile-layer.js.map +1 -0
  26. package/dist/es5/lib/mapbox-vector-tile/vector-tile.js +35 -0
  27. package/dist/es5/lib/mapbox-vector-tile/vector-tile.js.map +1 -0
  28. package/dist/es5/lib/parse-mvt.js +129 -0
  29. package/dist/es5/lib/parse-mvt.js.map +1 -0
  30. package/dist/es5/lib/types.js +2 -0
  31. package/dist/{lib → es5/lib}/types.js.map +0 -0
  32. package/dist/es5/mvt-loader.js +38 -0
  33. package/dist/es5/mvt-loader.js.map +1 -0
  34. package/dist/es5/workers/mvt-worker.js +8 -0
  35. package/dist/es5/workers/mvt-worker.js.map +1 -0
  36. package/dist/esm/bundle.js +5 -0
  37. package/dist/esm/bundle.js.map +1 -0
  38. package/dist/esm/helpers/binary-util-functions.js +81 -0
  39. package/dist/esm/helpers/binary-util-functions.js.map +1 -0
  40. package/dist/esm/helpers/mapbox-util-functions.js +49 -0
  41. package/dist/esm/helpers/mapbox-util-functions.js.map +1 -0
  42. package/dist/esm/index.js +2 -0
  43. package/dist/esm/index.js.map +1 -0
  44. package/dist/esm/lib/binary-vector-tile/LICENSE.txt +31 -0
  45. package/dist/esm/lib/binary-vector-tile/features-to-binary.js +327 -0
  46. package/dist/esm/lib/binary-vector-tile/features-to-binary.js.map +1 -0
  47. package/dist/esm/lib/binary-vector-tile/vector-tile-feature.js +156 -0
  48. package/dist/esm/lib/binary-vector-tile/vector-tile-feature.js.map +1 -0
  49. package/dist/esm/lib/binary-vector-tile/vector-tile-layer.js +64 -0
  50. package/dist/esm/lib/binary-vector-tile/vector-tile-layer.js.map +1 -0
  51. package/dist/esm/lib/binary-vector-tile/vector-tile.js +23 -0
  52. package/dist/esm/lib/binary-vector-tile/vector-tile.js.map +1 -0
  53. package/dist/esm/lib/mapbox-vector-tile/LICENSE.txt +31 -0
  54. package/dist/esm/lib/mapbox-vector-tile/vector-tile-feature.js +202 -0
  55. package/dist/esm/lib/mapbox-vector-tile/vector-tile-feature.js.map +1 -0
  56. package/dist/esm/lib/mapbox-vector-tile/vector-tile-layer.js +64 -0
  57. package/dist/esm/lib/mapbox-vector-tile/vector-tile-layer.js.map +1 -0
  58. package/dist/esm/lib/mapbox-vector-tile/vector-tile.js +23 -0
  59. package/dist/esm/lib/mapbox-vector-tile/vector-tile.js.map +1 -0
  60. package/dist/esm/lib/parse-mvt.js +116 -0
  61. package/dist/esm/lib/parse-mvt.js.map +1 -0
  62. package/dist/esm/lib/types.js +2 -0
  63. package/dist/esm/lib/types.js.map +1 -0
  64. package/dist/esm/mvt-loader.js +26 -0
  65. package/dist/esm/mvt-loader.js.map +1 -0
  66. package/dist/esm/workers/mvt-worker.js +4 -0
  67. package/dist/esm/workers/mvt-worker.js.map +1 -0
  68. package/dist/helpers/binary-util-functions.d.ts +39 -0
  69. package/dist/helpers/binary-util-functions.d.ts.map +1 -0
  70. package/dist/helpers/binary-util-functions.js +109 -74
  71. package/dist/helpers/mapbox-util-functions.d.ts +29 -0
  72. package/dist/helpers/mapbox-util-functions.d.ts.map +1 -0
  73. package/dist/helpers/mapbox-util-functions.js +78 -45
  74. package/dist/index.d.ts +2 -0
  75. package/dist/index.d.ts.map +1 -0
  76. package/dist/index.js +6 -2
  77. package/dist/lib/binary-vector-tile/features-to-binary.d.ts +185 -0
  78. package/dist/lib/binary-vector-tile/features-to-binary.d.ts.map +1 -0
  79. package/dist/lib/binary-vector-tile/features-to-binary.js +323 -297
  80. package/dist/lib/binary-vector-tile/vector-tile-feature.d.ts +36 -0
  81. package/dist/lib/binary-vector-tile/vector-tile-feature.d.ts.map +1 -0
  82. package/dist/lib/binary-vector-tile/vector-tile-feature.js +145 -142
  83. package/dist/lib/binary-vector-tile/vector-tile-layer.d.ts +23 -0
  84. package/dist/lib/binary-vector-tile/vector-tile-layer.d.ts.map +1 -0
  85. package/dist/lib/binary-vector-tile/vector-tile-layer.js +85 -58
  86. package/dist/lib/binary-vector-tile/vector-tile.d.ts +9 -0
  87. package/dist/lib/binary-vector-tile/vector-tile.d.ts.map +1 -0
  88. package/dist/lib/binary-vector-tile/vector-tile.js +25 -19
  89. package/dist/lib/mapbox-vector-tile/vector-tile-feature.d.ts +27 -0
  90. package/dist/lib/mapbox-vector-tile/vector-tile-feature.d.ts.map +1 -0
  91. package/dist/lib/mapbox-vector-tile/vector-tile-feature.js +161 -193
  92. package/dist/lib/mapbox-vector-tile/vector-tile-layer.d.ts +20 -0
  93. package/dist/lib/mapbox-vector-tile/vector-tile-layer.d.ts.map +1 -0
  94. package/dist/lib/mapbox-vector-tile/vector-tile-layer.js +83 -58
  95. package/dist/lib/mapbox-vector-tile/vector-tile.d.ts +9 -0
  96. package/dist/lib/mapbox-vector-tile/vector-tile.d.ts.map +1 -0
  97. package/dist/lib/mapbox-vector-tile/vector-tile.js +25 -19
  98. package/dist/lib/parse-mvt.d.ts +85 -0
  99. package/dist/lib/parse-mvt.d.ts.map +1 -0
  100. package/dist/lib/parse-mvt.js +126 -100
  101. package/dist/lib/types.d.ts +92 -0
  102. package/dist/lib/types.d.ts.map +1 -0
  103. package/dist/lib/types.js +2 -2
  104. package/dist/mvt-loader.d.ts +10 -0
  105. package/dist/mvt-loader.d.ts.map +1 -0
  106. package/dist/mvt-loader.js +42 -23
  107. package/dist/mvt-worker.js +2286 -3
  108. package/dist/workers/mvt-worker.d.ts +2 -0
  109. package/dist/workers/mvt-worker.d.ts.map +1 -0
  110. package/dist/workers/mvt-worker.js +5 -4
  111. package/package.json +9 -9
  112. package/src/lib/parse-mvt.ts +1 -1
  113. package/dist/bundle.js.map +0 -1
  114. package/dist/dist.min.js +0 -4
  115. package/dist/dist.min.js.map +0 -1
  116. package/dist/helpers/binary-util-functions.js.map +0 -1
  117. package/dist/helpers/mapbox-util-functions.js.map +0 -1
  118. package/dist/index.js.map +0 -1
  119. package/dist/lib/binary-vector-tile/features-to-binary.js.map +0 -1
  120. package/dist/lib/binary-vector-tile/vector-tile-feature.js.map +0 -1
  121. package/dist/lib/binary-vector-tile/vector-tile-layer.js.map +0 -1
  122. package/dist/lib/binary-vector-tile/vector-tile.js.map +0 -1
  123. package/dist/lib/mapbox-vector-tile/vector-tile-feature.js.map +0 -1
  124. package/dist/lib/mapbox-vector-tile/vector-tile-layer.js.map +0 -1
  125. package/dist/lib/mapbox-vector-tile/vector-tile.js.map +0 -1
  126. package/dist/lib/parse-mvt.js.map +0 -1
  127. package/dist/mvt-loader.js.map +0 -1
  128. package/dist/mvt-worker.js.map +0 -1
  129. package/dist/workers/mvt-worker.js.map +0 -1
@@ -1,327 +1,353 @@
1
- import { earcut } from '@math.gl/polygon';
2
- export function featuresToBinary(features, firstPassData, options) {
3
- return fillArrays(features, firstPassData, {
4
- numericPropKeys: options ? options.numericPropKeys : extractNumericPropKeys(features),
5
- PositionDataType: options ? options.PositionDataType : Float32Array
6
- });
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TEST_EXPORTS = exports.featuresToBinary = void 0;
4
+ /* eslint-disable indent */
5
+ const polygon_1 = require("@math.gl/polygon");
6
+ /**
7
+ * Convert binary features to flat binary arrays. Similar to
8
+ * `geojsonToBinary` helper function, except that it expects
9
+ * a binary representation of the feature data, which enables
10
+ * 2X-3X speed increase in parse speed, compared to using
11
+ * geoJSON. See `binary-vector-tile/VectorTileFeature` for
12
+ * data format detais
13
+ *
14
+ * @param features
15
+ * @param firstPassData
16
+ * @param options
17
+ * @returns filled arrays
18
+ */
19
+ function featuresToBinary(features, firstPassData, options) {
20
+ return fillArrays(features, firstPassData, {
21
+ numericPropKeys: options ? options.numericPropKeys : extractNumericPropKeys(features),
22
+ PositionDataType: options ? options.PositionDataType : Float32Array
23
+ });
7
24
  }
8
- export const TEST_EXPORTS = {
9
- extractNumericPropKeys,
10
- fillArrays
25
+ exports.featuresToBinary = featuresToBinary;
26
+ exports.TEST_EXPORTS = {
27
+ extractNumericPropKeys,
28
+ fillArrays
11
29
  };
12
-
30
+ /**
31
+ * Extracts properties that are always numeric
32
+ *
33
+ * @param features
34
+ * @returns object with numeric keys
35
+ */
13
36
  function extractNumericPropKeys(features) {
14
- const numericPropKeys = {};
15
-
16
- for (const feature of features) {
17
- if (feature.properties) {
18
- for (const key in feature.properties) {
19
- const numericSoFar = numericPropKeys[key];
20
-
21
- if (numericSoFar || numericSoFar === undefined) {
22
- const val = feature.properties[key];
23
- numericPropKeys[key] = isNumeric(val);
37
+ const numericPropKeys = {};
38
+ for (const feature of features) {
39
+ if (feature.properties) {
40
+ for (const key in feature.properties) {
41
+ // If property has not been seen before, or if property has been numeric
42
+ // in all previous features, check if numeric in this feature
43
+ // If not numeric, false is stored to prevent rechecking in the future
44
+ const numericSoFar = numericPropKeys[key];
45
+ if (numericSoFar || numericSoFar === undefined) {
46
+ const val = feature.properties[key];
47
+ numericPropKeys[key] = isNumeric(val);
48
+ }
49
+ }
24
50
  }
25
- }
26
51
  }
27
- }
28
-
29
- return Object.keys(numericPropKeys).filter(k => numericPropKeys[k]);
52
+ return Object.keys(numericPropKeys).filter((k) => numericPropKeys[k]);
30
53
  }
31
-
54
+ /**
55
+ * Fills coordinates into pre-allocated typed arrays
56
+ *
57
+ * @param features
58
+ * @param firstPassData
59
+ * @param options
60
+ * @returns an accessor object with value and size keys
61
+ */
62
+ // eslint-disable-next-line complexity
32
63
  function fillArrays(features, firstPassData, options) {
33
- const {
34
- pointPositionsCount,
35
- pointFeaturesCount,
36
- linePositionsCount,
37
- linePathsCount,
38
- lineFeaturesCount,
39
- polygonPositionsCount,
40
- polygonObjectsCount,
41
- polygonRingsCount,
42
- polygonFeaturesCount
43
- } = firstPassData;
44
- const {
45
- numericPropKeys,
46
- PositionDataType = Float32Array
47
- } = options;
48
- const hasGlobalId = features[0] && 'id' in features[0];
49
- const coordLength = 2;
50
- const GlobalFeatureIdsDataType = features.length > 65535 ? Uint32Array : Uint16Array;
51
- const points = {
52
- positions: new PositionDataType(pointPositionsCount * coordLength),
53
- globalFeatureIds: new GlobalFeatureIdsDataType(pointPositionsCount),
54
- featureIds: pointFeaturesCount > 65535 ? new Uint32Array(pointPositionsCount) : new Uint16Array(pointPositionsCount),
55
- numericProps: {},
56
- properties: [],
57
- fields: []
58
- };
59
- const lines = {
60
- pathIndices: linePositionsCount > 65535 ? new Uint32Array(linePathsCount + 1) : new Uint16Array(linePathsCount + 1),
61
- positions: new PositionDataType(linePositionsCount * coordLength),
62
- globalFeatureIds: new GlobalFeatureIdsDataType(linePositionsCount),
63
- featureIds: lineFeaturesCount > 65535 ? new Uint32Array(linePositionsCount) : new Uint16Array(linePositionsCount),
64
- numericProps: {},
65
- properties: [],
66
- fields: []
67
- };
68
- const polygons = {
69
- polygonIndices: polygonPositionsCount > 65535 ? new Uint32Array(polygonObjectsCount + 1) : new Uint16Array(polygonObjectsCount + 1),
70
- primitivePolygonIndices: polygonPositionsCount > 65535 ? new Uint32Array(polygonRingsCount + 1) : new Uint16Array(polygonRingsCount + 1),
71
- positions: new PositionDataType(polygonPositionsCount * coordLength),
72
- triangles: [],
73
- globalFeatureIds: new GlobalFeatureIdsDataType(polygonPositionsCount),
74
- featureIds: polygonFeaturesCount > 65535 ? new Uint32Array(polygonPositionsCount) : new Uint16Array(polygonPositionsCount),
75
- numericProps: {},
76
- properties: [],
77
- fields: []
78
- };
79
-
80
- for (const object of [points, lines, polygons]) {
81
- for (const propName of numericPropKeys) {
82
- object.numericProps[propName] = new Float32Array(object.positions.length / coordLength);
83
- }
84
- }
85
-
86
- lines.pathIndices[linePathsCount] = linePositionsCount;
87
- polygons.polygonIndices[polygonObjectsCount] = polygonPositionsCount;
88
- polygons.primitivePolygonIndices[polygonRingsCount] = polygonPositionsCount;
89
- const indexMap = {
90
- pointPosition: 0,
91
- pointFeature: 0,
92
- linePosition: 0,
93
- linePath: 0,
94
- lineFeature: 0,
95
- polygonPosition: 0,
96
- polygonObject: 0,
97
- polygonRing: 0,
98
- polygonFeature: 0,
99
- feature: 0
100
- };
101
-
102
- for (const feature of features) {
103
- const geometry = feature.geometry;
104
- const properties = feature.properties || {};
105
-
106
- switch (geometry.type) {
107
- case 'Point':
108
- case 'MultiPoint':
109
- handlePoint(geometry, points, indexMap, coordLength, properties);
110
- points.properties.push(keepStringProperties(properties, numericPropKeys));
111
-
112
- if (hasGlobalId) {
113
- points.fields.push({
114
- id: feature.id
115
- });
64
+ const { pointPositionsCount, pointFeaturesCount, linePositionsCount, linePathsCount, lineFeaturesCount, polygonPositionsCount, polygonObjectsCount, polygonRingsCount, polygonFeaturesCount } = firstPassData;
65
+ const { numericPropKeys, PositionDataType = Float32Array } = options;
66
+ const hasGlobalId = features[0] && 'id' in features[0];
67
+ const coordLength = 2;
68
+ const GlobalFeatureIdsDataType = features.length > 65535 ? Uint32Array : Uint16Array;
69
+ const points = {
70
+ positions: new PositionDataType(pointPositionsCount * coordLength),
71
+ globalFeatureIds: new GlobalFeatureIdsDataType(pointPositionsCount),
72
+ featureIds: pointFeaturesCount > 65535
73
+ ? new Uint32Array(pointPositionsCount)
74
+ : new Uint16Array(pointPositionsCount),
75
+ numericProps: {},
76
+ properties: [],
77
+ fields: []
78
+ };
79
+ const lines = {
80
+ pathIndices: linePositionsCount > 65535
81
+ ? new Uint32Array(linePathsCount + 1)
82
+ : new Uint16Array(linePathsCount + 1),
83
+ positions: new PositionDataType(linePositionsCount * coordLength),
84
+ globalFeatureIds: new GlobalFeatureIdsDataType(linePositionsCount),
85
+ featureIds: lineFeaturesCount > 65535
86
+ ? new Uint32Array(linePositionsCount)
87
+ : new Uint16Array(linePositionsCount),
88
+ numericProps: {},
89
+ properties: [],
90
+ fields: []
91
+ };
92
+ const polygons = {
93
+ polygonIndices: polygonPositionsCount > 65535
94
+ ? new Uint32Array(polygonObjectsCount + 1)
95
+ : new Uint16Array(polygonObjectsCount + 1),
96
+ primitivePolygonIndices: polygonPositionsCount > 65535
97
+ ? new Uint32Array(polygonRingsCount + 1)
98
+ : new Uint16Array(polygonRingsCount + 1),
99
+ positions: new PositionDataType(polygonPositionsCount * coordLength),
100
+ triangles: [],
101
+ globalFeatureIds: new GlobalFeatureIdsDataType(polygonPositionsCount),
102
+ featureIds: polygonFeaturesCount > 65535
103
+ ? new Uint32Array(polygonPositionsCount)
104
+ : new Uint16Array(polygonPositionsCount),
105
+ numericProps: {},
106
+ properties: [],
107
+ fields: []
108
+ };
109
+ // Instantiate numeric properties arrays; one value per vertex
110
+ for (const object of [points, lines, polygons]) {
111
+ for (const propName of numericPropKeys) {
112
+ // If property has been numeric in all previous features in which the property existed, check
113
+ // if numeric in this feature
114
+ object.numericProps[propName] = new Float32Array(object.positions.length / coordLength);
116
115
  }
117
-
118
- indexMap.pointFeature++;
119
- break;
120
-
121
- case 'LineString':
122
- case 'MultiLineString':
123
- handleLineString(geometry, lines, indexMap, coordLength, properties);
124
- lines.properties.push(keepStringProperties(properties, numericPropKeys));
125
-
126
- if (hasGlobalId) {
127
- lines.fields.push({
128
- id: feature.id
129
- });
130
- }
131
-
132
- indexMap.lineFeature++;
133
- break;
134
-
135
- case 'Polygon':
136
- case 'MultiPolygon':
137
- handlePolygon(geometry, polygons, indexMap, coordLength, properties);
138
- polygons.properties.push(keepStringProperties(properties, numericPropKeys));
139
-
140
- if (hasGlobalId) {
141
- polygons.fields.push({
142
- id: feature.id
143
- });
116
+ }
117
+ // Set last element of path/polygon indices as positions length
118
+ lines.pathIndices[linePathsCount] = linePositionsCount;
119
+ polygons.polygonIndices[polygonObjectsCount] = polygonPositionsCount;
120
+ polygons.primitivePolygonIndices[polygonRingsCount] = polygonPositionsCount;
121
+ const indexMap = {
122
+ pointPosition: 0,
123
+ pointFeature: 0,
124
+ linePosition: 0,
125
+ linePath: 0,
126
+ lineFeature: 0,
127
+ polygonPosition: 0,
128
+ polygonObject: 0,
129
+ polygonRing: 0,
130
+ polygonFeature: 0,
131
+ feature: 0
132
+ };
133
+ for (const feature of features) {
134
+ const geometry = feature.geometry;
135
+ const properties = feature.properties || {};
136
+ switch (geometry.type) {
137
+ case 'Point':
138
+ case 'MultiPoint':
139
+ handlePoint(geometry, points, indexMap, coordLength, properties);
140
+ points.properties.push(keepStringProperties(properties, numericPropKeys));
141
+ if (hasGlobalId) {
142
+ points.fields.push({ id: feature.id });
143
+ }
144
+ indexMap.pointFeature++;
145
+ break;
146
+ case 'LineString':
147
+ case 'MultiLineString':
148
+ handleLineString(geometry, lines, indexMap, coordLength, properties);
149
+ lines.properties.push(keepStringProperties(properties, numericPropKeys));
150
+ if (hasGlobalId) {
151
+ lines.fields.push({ id: feature.id });
152
+ }
153
+ indexMap.lineFeature++;
154
+ break;
155
+ case 'Polygon':
156
+ case 'MultiPolygon':
157
+ handlePolygon(geometry, polygons, indexMap, coordLength, properties);
158
+ polygons.properties.push(keepStringProperties(properties, numericPropKeys));
159
+ if (hasGlobalId) {
160
+ polygons.fields.push({ id: feature.id });
161
+ }
162
+ indexMap.polygonFeature++;
163
+ break;
164
+ default:
165
+ throw new Error('Invalid geometry type');
144
166
  }
145
-
146
- indexMap.polygonFeature++;
147
- break;
148
-
149
- default:
150
- throw new Error('Invalid geometry type');
167
+ indexMap.feature++;
151
168
  }
152
-
153
- indexMap.feature++;
154
- }
155
-
156
- return makeAccessorObjects(points, lines, polygons, coordLength);
169
+ // Wrap each array in an accessor object with value and size keys
170
+ return makeAccessorObjects(points, lines, polygons, coordLength);
157
171
  }
158
-
172
+ /**
173
+ * Fills (Multi)Point coordinates into points object of arrays
174
+ *
175
+ * @param geometry
176
+ * @param points
177
+ * @param indexMap
178
+ * @param coordLength
179
+ * @param properties
180
+ */
159
181
  function handlePoint(geometry, points, indexMap, coordLength, properties) {
160
- points.positions.set(geometry.data, indexMap.pointPosition * coordLength);
161
- const nPositions = geometry.data.length / coordLength;
162
- fillNumericProperties(points, properties, indexMap.pointPosition, nPositions);
163
- points.globalFeatureIds.fill(indexMap.feature, indexMap.pointPosition, indexMap.pointPosition + nPositions);
164
- points.featureIds.fill(indexMap.pointFeature, indexMap.pointPosition, indexMap.pointPosition + nPositions);
165
- indexMap.pointPosition += nPositions;
182
+ points.positions.set(geometry.data, indexMap.pointPosition * coordLength);
183
+ const nPositions = geometry.data.length / coordLength;
184
+ fillNumericProperties(points, properties, indexMap.pointPosition, nPositions);
185
+ points.globalFeatureIds.fill(indexMap.feature, indexMap.pointPosition, indexMap.pointPosition + nPositions);
186
+ points.featureIds.fill(indexMap.pointFeature, indexMap.pointPosition, indexMap.pointPosition + nPositions);
187
+ indexMap.pointPosition += nPositions;
166
188
  }
167
-
189
+ /**
190
+ * Fills (Multi)LineString coordinates into lines object of arrays
191
+ *
192
+ * @param geometry
193
+ * @param lines
194
+ * @param indexMap
195
+ * @param coordLength
196
+ * @param properties
197
+ */
168
198
  function handleLineString(geometry, lines, indexMap, coordLength, properties) {
169
- lines.positions.set(geometry.data, indexMap.linePosition * coordLength);
170
- const nPositions = geometry.data.length / coordLength;
171
- fillNumericProperties(lines, properties, indexMap.linePosition, nPositions);
172
- lines.globalFeatureIds.fill(indexMap.feature, indexMap.linePosition, indexMap.linePosition + nPositions);
173
- lines.featureIds.fill(indexMap.lineFeature, indexMap.linePosition, indexMap.linePosition + nPositions);
174
-
175
- for (let i = 0, il = geometry.lines.length; i < il; ++i) {
176
- const start = geometry.lines[i];
177
- const end = i === il - 1 ? geometry.data.length : geometry.lines[i + 1];
178
- lines.pathIndices[indexMap.linePath++] = indexMap.linePosition;
179
- indexMap.linePosition += (end - start) / coordLength;
180
- }
199
+ lines.positions.set(geometry.data, indexMap.linePosition * coordLength);
200
+ const nPositions = geometry.data.length / coordLength;
201
+ fillNumericProperties(lines, properties, indexMap.linePosition, nPositions);
202
+ lines.globalFeatureIds.fill(indexMap.feature, indexMap.linePosition, indexMap.linePosition + nPositions);
203
+ lines.featureIds.fill(indexMap.lineFeature, indexMap.linePosition, indexMap.linePosition + nPositions);
204
+ for (let i = 0, il = geometry.lines.length; i < il; ++i) {
205
+ // Extract range of data we are working with, defined by start
206
+ // and end indices (these index into the geometry.data array)
207
+ const start = geometry.lines[i];
208
+ const end = i === il - 1
209
+ ? geometry.data.length // last line, so read to end of data
210
+ : geometry.lines[i + 1]; // start index for next line
211
+ lines.pathIndices[indexMap.linePath++] = indexMap.linePosition;
212
+ indexMap.linePosition += (end - start) / coordLength;
213
+ }
181
214
  }
182
-
215
+ /**
216
+ * Fills (Multi)Polygon coordinates into polygons object of arrays
217
+ *
218
+ * @param geometry
219
+ * @param polygons
220
+ * @param indexMap
221
+ * @param coordLength
222
+ * @param properties
223
+ */
183
224
  function handlePolygon(geometry, polygons, indexMap, coordLength, properties) {
184
- polygons.positions.set(geometry.data, indexMap.polygonPosition * coordLength);
185
- const nPositions = geometry.data.length / coordLength;
186
- fillNumericProperties(polygons, properties, indexMap.polygonPosition, nPositions);
187
- polygons.globalFeatureIds.fill(indexMap.feature, indexMap.polygonPosition, indexMap.polygonPosition + nPositions);
188
- polygons.featureIds.fill(indexMap.polygonFeature, indexMap.polygonPosition, indexMap.polygonPosition + nPositions);
189
-
190
- for (let l = 0, ll = geometry.lines.length; l < ll; ++l) {
191
- const startPosition = indexMap.polygonPosition;
192
- polygons.polygonIndices[indexMap.polygonObject++] = startPosition;
193
- const areas = geometry.areas[l];
194
- const lines = geometry.lines[l];
195
- const nextLines = geometry.lines[l + 1];
196
-
197
- for (let i = 0, il = lines.length; i < il; ++i) {
198
- const start = lines[i];
199
- const end = i === il - 1 ? nextLines === undefined ? geometry.data.length : nextLines[0] : lines[i + 1];
200
- polygons.primitivePolygonIndices[indexMap.polygonRing++] = indexMap.polygonPosition;
201
- indexMap.polygonPosition += (end - start) / coordLength;
225
+ polygons.positions.set(geometry.data, indexMap.polygonPosition * coordLength);
226
+ const nPositions = geometry.data.length / coordLength;
227
+ fillNumericProperties(polygons, properties, indexMap.polygonPosition, nPositions);
228
+ polygons.globalFeatureIds.fill(indexMap.feature, indexMap.polygonPosition, indexMap.polygonPosition + nPositions);
229
+ polygons.featureIds.fill(indexMap.polygonFeature, indexMap.polygonPosition, indexMap.polygonPosition + nPositions);
230
+ // Unlike Point & LineString geometry.lines is a 2D array
231
+ for (let l = 0, ll = geometry.lines.length; l < ll; ++l) {
232
+ const startPosition = indexMap.polygonPosition;
233
+ polygons.polygonIndices[indexMap.polygonObject++] = startPosition;
234
+ const areas = geometry.areas[l];
235
+ const lines = geometry.lines[l];
236
+ const nextLines = geometry.lines[l + 1];
237
+ for (let i = 0, il = lines.length; i < il; ++i) {
238
+ const start = lines[i];
239
+ const end = i === il - 1
240
+ ? // last line, so either read to:
241
+ nextLines === undefined
242
+ ? geometry.data.length // end of data (no next lines)
243
+ : nextLines[0] // start of first line in nextLines
244
+ : lines[i + 1]; // start index for next line
245
+ polygons.primitivePolygonIndices[indexMap.polygonRing++] = indexMap.polygonPosition;
246
+ indexMap.polygonPosition += (end - start) / coordLength;
247
+ }
248
+ const endPosition = indexMap.polygonPosition;
249
+ triangulatePolygon(polygons, areas, lines, { startPosition, endPosition, coordLength });
202
250
  }
203
-
204
- const endPosition = indexMap.polygonPosition;
205
- triangulatePolygon(polygons, areas, lines, {
206
- startPosition,
207
- endPosition,
208
- coordLength
209
- });
210
- }
211
251
  }
212
-
213
- function triangulatePolygon(polygons, areas, lines, {
214
- startPosition,
215
- endPosition,
216
- coordLength
217
- }) {
218
- const start = startPosition * coordLength;
219
- const end = endPosition * coordLength;
220
- const polygonPositions = polygons.positions.subarray(start, end);
221
- const offset = lines[0];
222
- const holes = lines.slice(1).map(n => (n - offset) / coordLength);
223
- const indices = earcut(polygonPositions, holes, coordLength, areas);
224
-
225
- for (let t = 0, tl = indices.length; t < tl; ++t) {
226
- polygons.triangles.push(startPosition + indices[t]);
227
- }
252
+ /**
253
+ * Triangulate polygon using earcut
254
+ *
255
+ * @param polygons
256
+ * @param areas
257
+ * @param lines
258
+ * @param param3
259
+ */
260
+ function triangulatePolygon(polygons, areas, lines, { startPosition, endPosition, coordLength }) {
261
+ const start = startPosition * coordLength;
262
+ const end = endPosition * coordLength;
263
+ // Extract positions and holes for just this polygon
264
+ const polygonPositions = polygons.positions.subarray(start, end);
265
+ // Holes are referenced relative to outer polygon
266
+ const offset = lines[0];
267
+ const holes = lines.slice(1).map((n) => (n - offset) / coordLength);
268
+ // Compute triangulation
269
+ const indices = (0, polygon_1.earcut)(polygonPositions, holes, coordLength, areas);
270
+ // Indices returned by triangulation are relative to start
271
+ // of polygon, so we need to offset
272
+ for (let t = 0, tl = indices.length; t < tl; ++t) {
273
+ polygons.triangles.push(startPosition + indices[t]);
274
+ }
228
275
  }
229
-
276
+ /**
277
+ * Wrap each array in an accessor object with value and size keys
278
+ *
279
+ * @param points
280
+ * @param lines
281
+ * @param polygons
282
+ * @param coordLength
283
+ * @returns object
284
+ */
230
285
  function makeAccessorObjects(points, lines, polygons, coordLength) {
231
- const returnObj = {
232
- points: { ...points,
233
- positions: {
234
- value: points.positions,
235
- size: coordLength
236
- },
237
- globalFeatureIds: {
238
- value: points.globalFeatureIds,
239
- size: 1
240
- },
241
- featureIds: {
242
- value: points.featureIds,
243
- size: 1
244
- }
245
- },
246
- lines: { ...lines,
247
- pathIndices: {
248
- value: lines.pathIndices,
249
- size: 1
250
- },
251
- positions: {
252
- value: lines.positions,
253
- size: coordLength
254
- },
255
- globalFeatureIds: {
256
- value: lines.globalFeatureIds,
257
- size: 1
258
- },
259
- featureIds: {
260
- value: lines.featureIds,
261
- size: 1
262
- }
263
- },
264
- polygons: { ...polygons,
265
- polygonIndices: {
266
- value: polygons.polygonIndices,
267
- size: 1
268
- },
269
- primitivePolygonIndices: {
270
- value: polygons.primitivePolygonIndices,
271
- size: 1
272
- },
273
- positions: {
274
- value: polygons.positions,
275
- size: coordLength
276
- },
277
- triangles: {
278
- value: new Uint32Array(polygons.triangles),
279
- size: 1
280
- },
281
- globalFeatureIds: {
282
- value: polygons.globalFeatureIds,
283
- size: 1
284
- },
285
- featureIds: {
286
- value: polygons.featureIds,
287
- size: 1
288
- }
289
- }
290
- };
291
-
292
- for (const geomType in returnObj) {
293
- for (const numericProp in returnObj[geomType].numericProps) {
294
- returnObj[geomType].numericProps[numericProp] = {
295
- value: returnObj[geomType].numericProps[numericProp],
296
- size: 1
297
- };
286
+ const returnObj = {
287
+ points: {
288
+ ...points,
289
+ positions: { value: points.positions, size: coordLength },
290
+ globalFeatureIds: { value: points.globalFeatureIds, size: 1 },
291
+ featureIds: { value: points.featureIds, size: 1 }
292
+ },
293
+ lines: {
294
+ ...lines,
295
+ pathIndices: { value: lines.pathIndices, size: 1 },
296
+ positions: { value: lines.positions, size: coordLength },
297
+ globalFeatureIds: { value: lines.globalFeatureIds, size: 1 },
298
+ featureIds: { value: lines.featureIds, size: 1 }
299
+ },
300
+ polygons: {
301
+ ...polygons,
302
+ polygonIndices: { value: polygons.polygonIndices, size: 1 },
303
+ primitivePolygonIndices: { value: polygons.primitivePolygonIndices, size: 1 },
304
+ positions: { value: polygons.positions, size: coordLength },
305
+ triangles: { value: new Uint32Array(polygons.triangles), size: 1 },
306
+ globalFeatureIds: { value: polygons.globalFeatureIds, size: 1 },
307
+ featureIds: { value: polygons.featureIds, size: 1 }
308
+ }
309
+ };
310
+ for (const geomType in returnObj) {
311
+ for (const numericProp in returnObj[geomType].numericProps) {
312
+ returnObj[geomType].numericProps[numericProp] = {
313
+ value: returnObj[geomType].numericProps[numericProp],
314
+ size: 1
315
+ };
316
+ }
298
317
  }
299
- }
300
-
301
- return returnObj;
318
+ return returnObj;
302
319
  }
303
-
320
+ /**
321
+ * Add numeric properties to object
322
+ *
323
+ * @param object
324
+ * @param properties
325
+ * @param index
326
+ * @param length
327
+ */
304
328
  function fillNumericProperties(object, properties, index, length) {
305
- for (const numericPropName in object.numericProps) {
306
- if (numericPropName in properties) {
307
- object.numericProps[numericPropName].fill(properties[numericPropName], index, index + length);
329
+ for (const numericPropName in object.numericProps) {
330
+ if (numericPropName in properties) {
331
+ object.numericProps[numericPropName].fill(properties[numericPropName], index, index + length);
332
+ }
308
333
  }
309
- }
310
334
  }
311
-
335
+ /**
336
+ * Keep string properties in object
337
+ *
338
+ * @param properties
339
+ * @param numericKeys
340
+ * @returns object
341
+ */
312
342
  function keepStringProperties(properties, numericKeys) {
313
- const props = {};
314
-
315
- for (const key in properties) {
316
- if (!numericKeys.includes(key)) {
317
- props[key] = properties[key];
343
+ const props = {};
344
+ for (const key in properties) {
345
+ if (!numericKeys.includes(key)) {
346
+ props[key] = properties[key];
347
+ }
318
348
  }
319
- }
320
-
321
- return props;
349
+ return props;
322
350
  }
323
-
324
351
  function isNumeric(x) {
325
- return Number.isFinite(x);
352
+ return Number.isFinite(x);
326
353
  }
327
- //# sourceMappingURL=features-to-binary.js.map