@openglobus/og 0.13.5 → 0.13.6

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.
@@ -1,294 +1,292 @@
1
- import { getTileExtent } from "../mercator.js";
2
- import { Layer } from "../layer/Layer.js";
3
- import { GlobusTerrain } from "./GlobusTerrain.js";
4
- import { isPowerOfTwo } from "../math.js";
5
-
6
- const KEY =
7
- "pk.eyJ1IjoiZm94bXVsZGVyODMiLCJhIjoiY2pqYmR3dG5oM2Z1bzNrczJqYm5pODhuNSJ9.Y4DRmEPhb-XSlCR9CAXACQ";
8
-
9
- class MapboxTerrain extends GlobusTerrain {
10
- constructor(name, options) {
11
- super(name, options);
12
-
13
- options = options || {};
14
-
15
- this.equalizeVertices =
16
- options.equalizeVertices != undefined ? options.equalizeVertices : true;
17
-
18
- this.equalizeNormals = options.equalizeNormals || false;
19
-
20
- this.minZoom = options.minZoom != undefined ? options.minZoom : 2;
21
-
22
- this.maxZoom = options.maxZoom != undefined ? options.maxZoom : 17;
23
-
24
- this.gridSizeByZoom = options.gridSizeByZoom || [
25
- 64, 32, 16, 8, 8, 8, 8, 16, 16, 16, 16, 16, 32, 16, 32, 16, 32, 16, 32, 16, 8, 4
26
- ];
27
-
28
- this.url =
29
- options.url != undefined
30
- ? options.url
31
- : `//api.mapbox.com/v4/mapbox.terrain-rgb/{z}/{x}/{y}.pngraw?access_token=${
32
- options.key || KEY
33
- }`;
34
-
35
- this.noDataValues = options.noDataValues || [-65537, -10000];
36
-
37
- this.plainGridSize = options.plainGridSize || 128;
38
-
39
- this._dataType = "imageBitmap";
40
-
41
- this._imageSize = options.imageSize || 256;
42
-
43
- this._ctx = this._createTemporalCanvas(this._imageSize);
44
- }
45
-
46
- isBlur(segment) {
47
- if (segment.tileZoom >= 13) {
48
- return true;
49
- }
50
- return false;
51
- }
52
-
53
- _createTemporalCanvas(size) {
54
- let canvas = document.createElement("canvas");
55
- canvas.width = size;
56
- canvas.height = size;
57
- return canvas.getContext("2d");
58
- }
59
-
60
- _createHeights(data, segment) {
61
- const SIZE = data.width;
62
-
63
- this._ctx.clearRect(0, 0, SIZE, SIZE);
64
- this._ctx.drawImage(data, 0, 0);
65
- let rgbaData = this._ctx.getImageData(0, 0, SIZE, SIZE).data;
66
-
67
- //
68
- //Non power of two images
69
- //
70
- if (!isPowerOfTwo(this._imageSize) && SIZE === this._imageSize) {
71
- let outCurrenElevations = new Float32Array(SIZE * SIZE);
72
- extractElevationTilesMapboxNonPowerOfTwo(rgbaData, outCurrenElevations);
73
- return outCurrenElevations;
74
- }
75
-
76
- // TODO:
77
- //if (this._imageSize === this.plainGridSize) {
78
- // let elevationsSize = (this.plainGridSize + 1) * (this.plainGridSize + 1);
79
- // let d = SIZE / this.plainGridSize;
80
-
81
- // let outCurrenElevations = new Float32Array(elevationsSize);
82
-
83
- // for (let i = 0, len = outCurrenElevations.length; i < len; i++) {
84
- // let i4 = i * 4;
85
- // outCurrenElevations[i] = -10000 + 0.1 * (rgbaData[i4] * 256 * 256 + rgbaData[i4 + 1] * 256 + rgbaData[i4 + 2]);
86
- // }
87
-
88
- // return outCurrenElevations;
89
- //}
90
-
91
- //
92
- // Power of two images
93
- //
94
- let elevationsSize = (this.plainGridSize + 1) * (this.plainGridSize + 1);
95
- let d = SIZE / this.plainGridSize;
96
-
97
- let outCurrenElevations = new Float32Array(elevationsSize);
98
- let outChildrenElevations = new Array(d);
99
-
100
- for (let i = 0; i < d; i++) {
101
- outChildrenElevations[i] = [];
102
- for (let j = 0; j < d; j++) {
103
- outChildrenElevations[i][j] = new Float32Array(elevationsSize);
104
- }
105
- }
106
-
107
- extractElevationTilesMapbox(
108
- rgbaData,
109
- this.noDataValues,
110
- outCurrenElevations,
111
- outChildrenElevations
112
- );
113
-
114
- this._elevationCache[segment.tileIndex] = {
115
- heights: outCurrenElevations,
116
- extent: segment.getExtent()
117
- };
118
-
119
- for (let i = 0; i < d; i++) {
120
- for (let j = 0; j < d; j++) {
121
- let x = segment.tileX * 2 + j,
122
- y = segment.tileY * 2 + i,
123
- z = segment.tileZoom + 1;
124
- let tileIndex = Layer.getTileIndex(x, y, z);
125
- this._elevationCache[tileIndex] = {
126
- heights: outChildrenElevations[i][j],
127
- extent: getTileExtent(x, y, z)
128
- };
129
- }
130
- }
131
-
132
- return outCurrenElevations;
133
- }
134
- }
135
-
136
- function extractElevationTilesMapboxNonPowerOfTwo(rgbaData, outCurrenElevations) {
137
- for (let i = 0, len = outCurrenElevations.length; i < len; i++) {
138
- let i4 = i * 4;
139
- outCurrenElevations[i] =
140
- -10000 + 0.1 * (rgbaData[i4] * 256 * 256 + rgbaData[i4 + 1] * 256 + rgbaData[i4 + 2]);
141
- }
142
- }
143
-
144
- function extractElevationTilesMapbox(
145
- rgbaData,
146
- noDataValues,
147
- outCurrenElevations,
148
- outChildrenElevations
149
- ) {
150
- let destSize = Math.sqrt(outCurrenElevations.length) - 1;
151
- let destSizeOne = destSize + 1;
152
- let sourceSize = Math.sqrt(rgbaData.length / 4);
153
- let dt = sourceSize / destSize;
154
-
155
- let rightHeight = 0,
156
- bottomHeight = 0,
157
- sourceSize4 = 0;
158
-
159
- for (
160
- let k = 0, currIndex = 0, sourceDataLength = rgbaData.length / 4;
161
- k < sourceDataLength;
162
- k++
163
- ) {
164
- let k4 = k * 4;
165
-
166
- let height =
167
- -10000 + 0.1 * (rgbaData[k4] * 256 * 256 + rgbaData[k4 + 1] * 256 + rgbaData[k4 + 2]);
168
-
169
- let isNoDataCurrent = MapboxTerrain.checkNoDataValue(noDataValues, height),
170
- isNoDataRight = false,
171
- isNoDataBottom = false;
172
-
173
- let i = Math.floor(k / sourceSize),
174
- j = k % sourceSize;
175
-
176
- let tileX = Math.floor(j / destSize),
177
- tileY = Math.floor(i / destSize);
178
-
179
- let destArr = outChildrenElevations[tileY][tileX];
180
-
181
- let ii = i % destSize,
182
- jj = j % destSize;
183
-
184
- let destIndex = (ii + tileY) * destSizeOne + jj + tileX;
185
-
186
- destArr[destIndex] = height;
187
-
188
- if ((i + tileY) % dt === 0 && (j + tileX) % dt === 0) {
189
- outCurrenElevations[currIndex++] = height;
190
- }
191
-
192
- if ((j + 1) % destSize === 0 && j !== sourceSize - 1) {
193
- //current tile
194
- rightHeight =
195
- -10000 +
196
- 0.1 * (rgbaData[k4 + 4] * 256 * 256 + rgbaData[k4 + 5] * 256 + rgbaData[k4 + 6]);
197
-
198
- isNoDataRight = MapboxTerrain.checkNoDataValue(noDataValues, rightHeight);
199
-
200
- let middleHeight = height;
201
-
202
- if (!(isNoDataCurrent || isNoDataRight)) {
203
- middleHeight = (height + rightHeight) * 0.5;
204
- }
205
-
206
- destIndex = (ii + tileY) * destSizeOne + jj + 1;
207
- destArr[destIndex] = middleHeight;
208
-
209
- if ((i + tileY) % dt === 0) {
210
- outCurrenElevations[currIndex++] = middleHeight;
211
- }
212
-
213
- //next right tile
214
- let rightindex = (ii + tileY) * destSizeOne + ((jj + 1) % destSize);
215
- outChildrenElevations[tileY][tileX + 1][rightindex] = middleHeight;
216
- }
217
-
218
- if ((i + 1) % destSize === 0 && i !== sourceSize - 1) {
219
- //current tile
220
- sourceSize4 = sourceSize * 4;
221
-
222
- bottomHeight =
223
- -10000 +
224
- 0.1 *
225
- (rgbaData[k4 + sourceSize4] * 256 * 256 +
226
- rgbaData[k4 + sourceSize4 + 1] * 256 +
227
- rgbaData[k4 + sourceSize4 + 2]);
228
-
229
- isNoDataBottom = MapboxTerrain.checkNoDataValue(noDataValues, bottomHeight);
230
-
231
- let middleHeight = (height + bottomHeight) * 0.5;
232
-
233
- if (!(isNoDataCurrent || isNoDataBottom)) {
234
- middleHeight = (height + bottomHeight) * 0.5;
235
- }
236
-
237
- destIndex = (ii + 1) * destSizeOne + jj + tileX;
238
- destArr[destIndex] = middleHeight;
239
-
240
- if ((j + tileX) % dt === 0) {
241
- outCurrenElevations[currIndex++] = middleHeight;
242
- }
243
-
244
- //next bottom tile
245
- let bottomindex = ((ii + 1) % destSize) * destSizeOne + jj + tileX;
246
- outChildrenElevations[tileY + 1][tileX][bottomindex] = middleHeight;
247
- }
248
-
249
- if (
250
- (j + 1) % destSize === 0 &&
251
- j !== sourceSize - 1 &&
252
- (i + 1) % destSize === 0 &&
253
- i !== sourceSize - 1
254
- ) {
255
- //current tile
256
- let rightBottomHeight =
257
- -10000 +
258
- 0.1 *
259
- (rgbaData[k4 + sourceSize4 + 4] * 256 * 256 +
260
- rgbaData[k4 + sourceSize4 + 5] * 256 +
261
- rgbaData[k4 + sourceSize4 + 6]);
262
-
263
- let isNoDataRightBottom = MapboxTerrain.checkNoDataValue(
264
- noDataValues,
265
- rightBottomHeight
266
- );
267
-
268
- let middleHeight = height;
269
-
270
- if (!(isNoDataCurrent || isNoDataRight || isNoDataBottom || isNoDataRightBottom)) {
271
- middleHeight = (height + rightHeight + bottomHeight + rightBottomHeight) * 0.25;
272
- }
273
-
274
- destIndex = (ii + 1) * destSizeOne + (jj + 1);
275
- destArr[destIndex] = middleHeight;
276
-
277
- outCurrenElevations[currIndex++] = middleHeight;
278
-
279
- //next right tile
280
- let rightindex = (ii + 1) * destSizeOne;
281
- outChildrenElevations[tileY][tileX + 1][rightindex] = middleHeight;
282
-
283
- //next bottom tile
284
- let bottomindex = destSize;
285
- outChildrenElevations[tileY + 1][tileX][bottomindex] = middleHeight;
286
-
287
- //next right bottom tile
288
- let rightBottomindex = 0;
289
- outChildrenElevations[tileY + 1][tileX + 1][rightBottomindex] = middleHeight;
290
- }
291
- }
292
- }
293
-
294
- export { MapboxTerrain };
1
+ import { getTileExtent } from "../mercator.js";
2
+ import { Layer } from "../layer/Layer.js";
3
+ import { GlobusTerrain } from "./GlobusTerrain.js";
4
+ import { isPowerOfTwo } from "../math.js";
5
+
6
+ const KEY =
7
+ "pk.eyJ1IjoiZm94bXVsZGVyODMiLCJhIjoiY2pqYmR3dG5oM2Z1bzNrczJqYm5pODhuNSJ9.Y4DRmEPhb-XSlCR9CAXACQ";
8
+
9
+ class MapboxTerrain extends GlobusTerrain {
10
+ constructor(name, options = {}) {
11
+ super(name, options);
12
+
13
+ this.equalizeVertices =
14
+ options.equalizeVertices != undefined ? options.equalizeVertices : true;
15
+
16
+ this.equalizeNormals = options.equalizeNormals || false;
17
+
18
+ this.minZoom = options.minZoom != undefined ? options.minZoom : 2;
19
+
20
+ this.maxZoom = options.maxZoom != undefined ? options.maxZoom : 17;
21
+
22
+ this.gridSizeByZoom = options.gridSizeByZoom || [
23
+ 64, 32, 16, 8, 8, 8, 8, 16, 16, 16, 16, 16, 32, 16, 32, 16, 32, 16, 32, 16, 8, 4
24
+ ];
25
+
26
+ this.url =
27
+ options.url != undefined
28
+ ? options.url
29
+ : `//api.mapbox.com/v4/mapbox.terrain-rgb/{z}/{x}/{y}.pngraw?access_token=${
30
+ options.key || KEY
31
+ }`;
32
+
33
+ this.noDataValues = options.noDataValues || [-65537, -10000];
34
+
35
+ this.plainGridSize = options.plainGridSize || 128;
36
+
37
+ this._dataType = "imageBitmap";
38
+
39
+ this._imageSize = options.imageSize || 256;
40
+
41
+ this._ctx = this._createTemporalCanvas(this._imageSize);
42
+ }
43
+
44
+ isBlur(segment) {
45
+ if (segment.tileZoom >= 13) {
46
+ return true;
47
+ }
48
+ return false;
49
+ }
50
+
51
+ _createTemporalCanvas(size) {
52
+ let canvas = document.createElement("canvas");
53
+ canvas.width = size;
54
+ canvas.height = size;
55
+ return canvas.getContext("2d");
56
+ }
57
+
58
+ _createHeights(data, tileIndex, tileX, tileY, tileZoom, extent) {
59
+ const SIZE = data.width;
60
+
61
+ this._ctx.clearRect(0, 0, SIZE, SIZE);
62
+ this._ctx.drawImage(data, 0, 0);
63
+ let rgbaData = this._ctx.getImageData(0, 0, SIZE, SIZE).data;
64
+
65
+ //
66
+ //Non power of two images
67
+ //
68
+ if (!isPowerOfTwo(this._imageSize) && SIZE === this._imageSize) {
69
+ let outCurrenElevations = new Float32Array(SIZE * SIZE);
70
+ extractElevationTilesMapboxNonPowerOfTwo(rgbaData, outCurrenElevations);
71
+ return outCurrenElevations;
72
+ }
73
+
74
+ // TODO:
75
+ //if (this._imageSize === this.plainGridSize) {
76
+ // let elevationsSize = (this.plainGridSize + 1) * (this.plainGridSize + 1);
77
+ // let d = SIZE / this.plainGridSize;
78
+
79
+ // let outCurrenElevations = new Float32Array(elevationsSize);
80
+
81
+ // for (let i = 0, len = outCurrenElevations.length; i < len; i++) {
82
+ // let i4 = i * 4;
83
+ // outCurrenElevations[i] = -10000 + 0.1 * (rgbaData[i4] * 256 * 256 + rgbaData[i4 + 1] * 256 + rgbaData[i4 + 2]);
84
+ // }
85
+
86
+ // return outCurrenElevations;
87
+ //}
88
+
89
+ //
90
+ // Power of two images
91
+ //
92
+ let elevationsSize = (this.plainGridSize + 1) * (this.plainGridSize + 1);
93
+ let d = SIZE / this.plainGridSize;
94
+
95
+ let outCurrenElevations = new Float32Array(elevationsSize);
96
+ let outChildrenElevations = new Array(d);
97
+
98
+ for (let i = 0; i < d; i++) {
99
+ outChildrenElevations[i] = [];
100
+ for (let j = 0; j < d; j++) {
101
+ outChildrenElevations[i][j] = new Float32Array(elevationsSize);
102
+ }
103
+ }
104
+
105
+ extractElevationTilesMapbox(
106
+ rgbaData,
107
+ this.noDataValues,
108
+ outCurrenElevations,
109
+ outChildrenElevations
110
+ );
111
+
112
+ this._elevationCache[tileIndex] = {
113
+ heights: outCurrenElevations,
114
+ extent: extent//segment.getExtent()
115
+ };
116
+
117
+ for (let i = 0; i < d; i++) {
118
+ for (let j = 0; j < d; j++) {
119
+ let x = tileX * 2 + j,
120
+ y = tileY * 2 + i,
121
+ z = tileZoom + 1;
122
+ let tileIndex = Layer.getTileIndex(x, y, z);
123
+ this._elevationCache[tileIndex] = {
124
+ heights: outChildrenElevations[i][j],
125
+ extent: getTileExtent(x, y, z)
126
+ };
127
+ }
128
+ }
129
+
130
+ return outCurrenElevations;
131
+ }
132
+ }
133
+
134
+ function extractElevationTilesMapboxNonPowerOfTwo(rgbaData, outCurrenElevations) {
135
+ for (let i = 0, len = outCurrenElevations.length; i < len; i++) {
136
+ let i4 = i * 4;
137
+ outCurrenElevations[i] =
138
+ -10000 + 0.1 * (rgbaData[i4] * 256 * 256 + rgbaData[i4 + 1] * 256 + rgbaData[i4 + 2]);
139
+ }
140
+ }
141
+
142
+ function extractElevationTilesMapbox(
143
+ rgbaData,
144
+ noDataValues,
145
+ outCurrenElevations,
146
+ outChildrenElevations
147
+ ) {
148
+ let destSize = Math.sqrt(outCurrenElevations.length) - 1;
149
+ let destSizeOne = destSize + 1;
150
+ let sourceSize = Math.sqrt(rgbaData.length / 4);
151
+ let dt = sourceSize / destSize;
152
+
153
+ let rightHeight = 0,
154
+ bottomHeight = 0,
155
+ sourceSize4 = 0;
156
+
157
+ for (
158
+ let k = 0, currIndex = 0, sourceDataLength = rgbaData.length / 4;
159
+ k < sourceDataLength;
160
+ k++
161
+ ) {
162
+ let k4 = k * 4;
163
+
164
+ let height =
165
+ -10000 + 0.1 * (rgbaData[k4] * 256 * 256 + rgbaData[k4 + 1] * 256 + rgbaData[k4 + 2]);
166
+
167
+ let isNoDataCurrent = MapboxTerrain.checkNoDataValue(noDataValues, height),
168
+ isNoDataRight = false,
169
+ isNoDataBottom = false;
170
+
171
+ let i = Math.floor(k / sourceSize),
172
+ j = k % sourceSize;
173
+
174
+ let tileX = Math.floor(j / destSize),
175
+ tileY = Math.floor(i / destSize);
176
+
177
+ let destArr = outChildrenElevations[tileY][tileX];
178
+
179
+ let ii = i % destSize,
180
+ jj = j % destSize;
181
+
182
+ let destIndex = (ii + tileY) * destSizeOne + jj + tileX;
183
+
184
+ destArr[destIndex] = height;
185
+
186
+ if ((i + tileY) % dt === 0 && (j + tileX) % dt === 0) {
187
+ outCurrenElevations[currIndex++] = height;
188
+ }
189
+
190
+ if ((j + 1) % destSize === 0 && j !== sourceSize - 1) {
191
+ //current tile
192
+ rightHeight =
193
+ -10000 +
194
+ 0.1 * (rgbaData[k4 + 4] * 256 * 256 + rgbaData[k4 + 5] * 256 + rgbaData[k4 + 6]);
195
+
196
+ isNoDataRight = MapboxTerrain.checkNoDataValue(noDataValues, rightHeight);
197
+
198
+ let middleHeight = height;
199
+
200
+ if (!(isNoDataCurrent || isNoDataRight)) {
201
+ middleHeight = (height + rightHeight) * 0.5;
202
+ }
203
+
204
+ destIndex = (ii + tileY) * destSizeOne + jj + 1;
205
+ destArr[destIndex] = middleHeight;
206
+
207
+ if ((i + tileY) % dt === 0) {
208
+ outCurrenElevations[currIndex++] = middleHeight;
209
+ }
210
+
211
+ //next right tile
212
+ let rightindex = (ii + tileY) * destSizeOne + ((jj + 1) % destSize);
213
+ outChildrenElevations[tileY][tileX + 1][rightindex] = middleHeight;
214
+ }
215
+
216
+ if ((i + 1) % destSize === 0 && i !== sourceSize - 1) {
217
+ //current tile
218
+ sourceSize4 = sourceSize * 4;
219
+
220
+ bottomHeight =
221
+ -10000 +
222
+ 0.1 *
223
+ (rgbaData[k4 + sourceSize4] * 256 * 256 +
224
+ rgbaData[k4 + sourceSize4 + 1] * 256 +
225
+ rgbaData[k4 + sourceSize4 + 2]);
226
+
227
+ isNoDataBottom = MapboxTerrain.checkNoDataValue(noDataValues, bottomHeight);
228
+
229
+ let middleHeight = (height + bottomHeight) * 0.5;
230
+
231
+ if (!(isNoDataCurrent || isNoDataBottom)) {
232
+ middleHeight = (height + bottomHeight) * 0.5;
233
+ }
234
+
235
+ destIndex = (ii + 1) * destSizeOne + jj + tileX;
236
+ destArr[destIndex] = middleHeight;
237
+
238
+ if ((j + tileX) % dt === 0) {
239
+ outCurrenElevations[currIndex++] = middleHeight;
240
+ }
241
+
242
+ //next bottom tile
243
+ let bottomindex = ((ii + 1) % destSize) * destSizeOne + jj + tileX;
244
+ outChildrenElevations[tileY + 1][tileX][bottomindex] = middleHeight;
245
+ }
246
+
247
+ if (
248
+ (j + 1) % destSize === 0 &&
249
+ j !== sourceSize - 1 &&
250
+ (i + 1) % destSize === 0 &&
251
+ i !== sourceSize - 1
252
+ ) {
253
+ //current tile
254
+ let rightBottomHeight =
255
+ -10000 +
256
+ 0.1 *
257
+ (rgbaData[k4 + sourceSize4 + 4] * 256 * 256 +
258
+ rgbaData[k4 + sourceSize4 + 5] * 256 +
259
+ rgbaData[k4 + sourceSize4 + 6]);
260
+
261
+ let isNoDataRightBottom = MapboxTerrain.checkNoDataValue(
262
+ noDataValues,
263
+ rightBottomHeight
264
+ );
265
+
266
+ let middleHeight = height;
267
+
268
+ if (!(isNoDataCurrent || isNoDataRight || isNoDataBottom || isNoDataRightBottom)) {
269
+ middleHeight = (height + rightHeight + bottomHeight + rightBottomHeight) * 0.25;
270
+ }
271
+
272
+ destIndex = (ii + 1) * destSizeOne + (jj + 1);
273
+ destArr[destIndex] = middleHeight;
274
+
275
+ outCurrenElevations[currIndex++] = middleHeight;
276
+
277
+ //next right tile
278
+ let rightindex = (ii + 1) * destSizeOne;
279
+ outChildrenElevations[tileY][tileX + 1][rightindex] = middleHeight;
280
+
281
+ //next bottom tile
282
+ let bottomindex = destSize;
283
+ outChildrenElevations[tileY + 1][tileX][bottomindex] = middleHeight;
284
+
285
+ //next right bottom tile
286
+ let rightBottomindex = 0;
287
+ outChildrenElevations[tileY + 1][tileX + 1][rightBottomindex] = middleHeight;
288
+ }
289
+ }
290
+ }
291
+
292
+ export { MapboxTerrain };