@map-colonies/mc-utils 2.0.2 → 3.1.0

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 (122) hide show
  1. package/README.md +67 -67
  2. package/dist/arrays/index.d.ts +1 -1
  3. package/dist/arrays/index.d.ts.map +0 -0
  4. package/dist/arrays/index.js +17 -17
  5. package/dist/arrays/index.js.map +0 -0
  6. package/dist/arrays/subGroups.d.ts +8 -8
  7. package/dist/arrays/subGroups.d.ts.map +0 -0
  8. package/dist/arrays/subGroups.js +33 -138
  9. package/dist/arrays/subGroups.js.map +1 -1
  10. package/dist/communication/http/httpClient.d.ts +29 -29
  11. package/dist/communication/http/httpClient.d.ts.map +0 -0
  12. package/dist/communication/http/httpClient.js +317 -434
  13. package/dist/communication/http/httpClient.js.map +1 -1
  14. package/dist/communication/http/index.d.ts +1 -1
  15. package/dist/communication/http/index.d.ts.map +0 -0
  16. package/dist/communication/http/index.js +17 -17
  17. package/dist/communication/http/index.js.map +0 -0
  18. package/dist/communication/index.d.ts +1 -1
  19. package/dist/communication/index.d.ts.map +0 -0
  20. package/dist/communication/index.js +17 -17
  21. package/dist/communication/index.js.map +0 -0
  22. package/dist/dateTime/getUTCDate.d.ts +1 -1
  23. package/dist/dateTime/getUTCDate.d.ts.map +0 -0
  24. package/dist/dateTime/getUTCDate.js +10 -10
  25. package/dist/dateTime/getUTCDate.js.map +1 -1
  26. package/dist/dateTime/index.d.ts +1 -1
  27. package/dist/dateTime/index.d.ts.map +0 -0
  28. package/dist/dateTime/index.js +17 -17
  29. package/dist/dateTime/index.js.map +0 -0
  30. package/dist/geo/bboxUtils.d.ts +23 -23
  31. package/dist/geo/bboxUtils.d.ts.map +0 -0
  32. package/dist/geo/bboxUtils.js +89 -89
  33. package/dist/geo/bboxUtils.js.map +1 -1
  34. package/dist/geo/geoConvertor.d.ts +18 -18
  35. package/dist/geo/geoConvertor.d.ts.map +0 -0
  36. package/dist/geo/geoConvertor.js +49 -51
  37. package/dist/geo/geoConvertor.js.map +1 -1
  38. package/dist/geo/geoHash.d.ts +24 -24
  39. package/dist/geo/geoHash.d.ts.map +1 -1
  40. package/dist/geo/geoHash.js +134 -246
  41. package/dist/geo/geoHash.js.map +1 -1
  42. package/dist/geo/geoIntersection.d.ts +19 -19
  43. package/dist/geo/geoIntersection.d.ts.map +0 -0
  44. package/dist/geo/geoIntersection.js +70 -70
  45. package/dist/geo/geoIntersection.js.map +1 -1
  46. package/dist/geo/index.d.ts +8 -8
  47. package/dist/geo/index.d.ts.map +0 -0
  48. package/dist/geo/index.js +24 -24
  49. package/dist/geo/index.js.map +0 -0
  50. package/dist/geo/tileBatcher.d.ts +8 -8
  51. package/dist/geo/tileBatcher.d.ts.map +1 -1
  52. package/dist/geo/tileBatcher.js +97 -167
  53. package/dist/geo/tileBatcher.js.map +1 -1
  54. package/dist/geo/tileRanger.d.ts +46 -46
  55. package/dist/geo/tileRanger.d.ts.map +1 -1
  56. package/dist/geo/tileRanger.js +232 -327
  57. package/dist/geo/tileRanger.js.map +1 -1
  58. package/dist/geo/tiles.d.ts +82 -82
  59. package/dist/geo/tiles.d.ts.map +0 -0
  60. package/dist/geo/tiles.js +219 -246
  61. package/dist/geo/tiles.js.map +1 -1
  62. package/dist/geo/tilesGenerator.d.ts +2 -2
  63. package/dist/geo/tilesGenerator.d.ts.map +1 -1
  64. package/dist/geo/tilesGenerator.js +17 -94
  65. package/dist/geo/tilesGenerator.js.map +1 -1
  66. package/dist/index.d.ts +6 -5
  67. package/dist/index.d.ts.map +1 -1
  68. package/dist/index.js +22 -21
  69. package/dist/index.js.map +1 -1
  70. package/dist/models/enums/gdal/dataType.d.ts +16 -16
  71. package/dist/models/enums/gdal/dataType.d.ts.map +0 -0
  72. package/dist/models/enums/gdal/dataType.js +20 -20
  73. package/dist/models/enums/gdal/dataType.js.map +0 -0
  74. package/dist/models/enums/gdal/index.d.ts +2 -2
  75. package/dist/models/enums/gdal/index.d.ts.map +0 -0
  76. package/dist/models/enums/gdal/index.js +18 -18
  77. package/dist/models/enums/gdal/index.js.map +0 -0
  78. package/dist/models/enums/gdal/resamplingMethod.d.ts +16 -16
  79. package/dist/models/enums/gdal/resamplingMethod.d.ts.map +0 -0
  80. package/dist/models/enums/gdal/resamplingMethod.js +20 -20
  81. package/dist/models/enums/gdal/resamplingMethod.js.map +0 -0
  82. package/dist/models/enums/geo/index.d.ts +1 -1
  83. package/dist/models/enums/geo/index.d.ts.map +0 -0
  84. package/dist/models/enums/geo/index.js +17 -17
  85. package/dist/models/enums/geo/index.js.map +0 -0
  86. package/dist/models/enums/geo/tileOrigin.d.ts +7 -7
  87. package/dist/models/enums/geo/tileOrigin.d.ts.map +0 -0
  88. package/dist/models/enums/geo/tileOrigin.js +11 -11
  89. package/dist/models/enums/geo/tileOrigin.js.map +0 -0
  90. package/dist/models/enums/index.d.ts +2 -2
  91. package/dist/models/enums/index.d.ts.map +0 -0
  92. package/dist/models/enums/index.js +18 -18
  93. package/dist/models/enums/index.js.map +0 -0
  94. package/dist/models/index.d.ts +2 -2
  95. package/dist/models/index.d.ts.map +0 -0
  96. package/dist/models/index.js +18 -18
  97. package/dist/models/index.js.map +0 -0
  98. package/dist/models/interfaces/geo/iPoint.d.ts +7 -7
  99. package/dist/models/interfaces/geo/iPoint.d.ts.map +0 -0
  100. package/dist/models/interfaces/geo/iPoint.js +2 -2
  101. package/dist/models/interfaces/geo/iPoint.js.map +0 -0
  102. package/dist/models/interfaces/geo/iTile.d.ts +15 -15
  103. package/dist/models/interfaces/geo/iTile.d.ts.map +0 -0
  104. package/dist/models/interfaces/geo/iTile.js +2 -2
  105. package/dist/models/interfaces/geo/iTile.js.map +0 -0
  106. package/dist/models/interfaces/geo/index.d.ts +2 -2
  107. package/dist/models/interfaces/geo/index.d.ts.map +0 -0
  108. package/dist/models/interfaces/geo/index.js +18 -18
  109. package/dist/models/interfaces/geo/index.js.map +0 -0
  110. package/dist/models/interfaces/index.d.ts +1 -1
  111. package/dist/models/interfaces/index.d.ts.map +0 -0
  112. package/dist/models/interfaces/index.js +17 -17
  113. package/dist/models/interfaces/index.js.map +0 -0
  114. package/dist/utils/index.d.ts +2 -0
  115. package/dist/utils/index.d.ts.map +1 -0
  116. package/dist/utils/index.js +18 -0
  117. package/dist/utils/index.js.map +1 -0
  118. package/dist/utils/timeout.d.ts +5 -0
  119. package/dist/utils/timeout.d.ts.map +1 -0
  120. package/dist/utils/timeout.js +23 -0
  121. package/dist/utils/timeout.js.map +1 -0
  122. package/package.json +79 -79
@@ -1 +1 @@
1
- {"version":3,"file":"tileRanger.d.ts","sourceRoot":"","sources":["../../src/geo/tileRanger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,mCAAmC,CAAC;AAC3D,OAAO,EAA0D,OAAO,EAAa,YAAY,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAC/H,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,gCAAgC,CAAC;AAkBnE;;GAEG;AACH,qBAAa,UAAU;IACrB;;;;;OAKG;IACI,WAAW,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,GAAG,UAAU;IA4BzD;;;;;OAKG;IACK,eAAe,CAAC,SAAS,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,GAAG,YAAY,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,UAAQ,GAAG,SAAS,CAAC,UAAU,CAAC;IAiCnI;;;;OAIG;IACI,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,SAAS,CAAC,KAAK,CAAC;IAClE;;;;OAIG;IACI,aAAa,CAAC,SAAS,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,GAAG,YAAY,CAAC,EAAE,IAAI,EAAE,MAAM,GAAG,SAAS,CAAC,KAAK,CAAC;IAW1G,OAAO,CAAE,cAAc;IA6EvB;;;;;;OAMG;IACH,OAAO,CAAE,YAAY;IAwCrB,OAAO,CAAC,gBAAgB;IASxB,OAAO,CAAC,QAAQ,CAAC,yBAAyB,CAiBxC;IAEF,OAAO,CAAC,MAAM;CAKf"}
1
+ {"version":3,"file":"tileRanger.d.ts","sourceRoot":"","sources":["../../src/geo/tileRanger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,mCAAmC,CAAC;AAC3D,OAAO,EAA0D,OAAO,EAAa,YAAY,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAC/H,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,gCAAgC,CAAC;AAkBnE;;GAEG;AACH,qBAAa,UAAU;IACrB;;;;;OAKG;IACI,WAAW,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,GAAG,UAAU;IA4BzD;;;;;OAKG;IACW,eAAe,CAAC,SAAS,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,GAAG,YAAY,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,UAAQ,GAAG,cAAc,CAAC,UAAU,CAAC;IAiC9I;;;;OAIG;IACI,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,cAAc,CAAC,KAAK,CAAC;IACvE;;;;OAIG;IACI,aAAa,CAAC,SAAS,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,GAAG,YAAY,CAAC,EAAE,IAAI,EAAE,MAAM,GAAG,cAAc,CAAC,KAAK,CAAC;YAchG,cAAc;IA6E7B;;;;;;OAMG;IACH,OAAO,CAAE,YAAY;IAwCrB,OAAO,CAAC,gBAAgB;IASxB,OAAO,CAAC,QAAQ,CAAC,yBAAyB,CAiBxC;IAEF,OAAO,CAAC,MAAM;CAKf"}
@@ -1,328 +1,233 @@
1
- "use strict";
2
- var __generator = (this && this.__generator) || function (thisArg, body) {
3
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
4
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
5
- function verb(n) { return function (v) { return step([n, v]); }; }
6
- function step(op) {
7
- if (f) throw new TypeError("Generator is already executing.");
8
- while (_) try {
9
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
10
- if (y = 0, t) op = [op[0] & 2, t.value];
11
- switch (op[0]) {
12
- case 0: case 1: t = op; break;
13
- case 4: _.label++; return { value: op[1], done: false };
14
- case 5: _.label++; y = op[1]; op = [0]; continue;
15
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
16
- default:
17
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
18
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
19
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
20
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
21
- if (t[2]) _.ops.pop();
22
- _.trys.pop(); continue;
23
- }
24
- op = body.call(thisArg, _);
25
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
26
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
27
- }
28
- };
29
- var __values = (this && this.__values) || function(o) {
30
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
31
- if (m) return m.call(o);
32
- if (o && typeof o.length === "number") return {
33
- next: function () {
34
- if (o && i >= o.length) o = void 0;
35
- return { value: o && o[i++], done: !o };
36
- }
37
- };
38
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
39
- };
40
- Object.defineProperty(exports, "__esModule", { value: true });
41
- exports.TileRanger = void 0;
42
- var turf_1 = require("@turf/turf");
43
- var bboxUtils_1 = require("./bboxUtils");
44
- var tiles_1 = require("./tiles");
45
- var tilesGenerator_1 = require("./tilesGenerator");
46
- var TileIntersectionState;
47
- (function (TileIntersectionState) {
48
- TileIntersectionState["FULL"] = "full";
49
- TileIntersectionState["PARTIAL"] = "partial";
50
- TileIntersectionState["NONE"] = "none";
51
- })(TileIntersectionState || (TileIntersectionState = {}));
52
- /**
53
- * class for generating and decoding tile hashes
54
- */
55
- var TileRanger = /** @class */ (function () {
56
- function TileRanger() {
57
- this.tileFootprintIntersection = function (tile, intersectionParams) {
58
- var tileBbox = (0, tiles_1.tileToBbox)(tile);
59
- var tilePoly = (0, turf_1.bboxPolygon)(tileBbox);
60
- var intersection = (0, turf_1.intersect)(intersectionParams.footprint, tilePoly);
61
- if (intersection === null) {
62
- return TileIntersectionState.NONE;
63
- }
64
- // stop condition
65
- if (tile.zoom === intersectionParams.maxZoom) {
66
- return TileIntersectionState.FULL;
67
- }
68
- var intArea = (0, turf_1.area)(intersection);
69
- var hashArea = (0, turf_1.area)(tilePoly);
70
- if (intArea == hashArea) {
71
- return TileIntersectionState.FULL;
72
- }
73
- return TileIntersectionState.PARTIAL;
74
- };
75
- }
76
- /**
77
- * converts tile to tile range of specified zoom level
78
- * @param tile
79
- * @param zoom target tile range zoom
80
- * @returns
81
- */
82
- TileRanger.prototype.tileToRange = function (tile, zoom) {
83
- var minX, minY, maxX, maxY;
84
- minX = tile.x;
85
- maxX = tile.x + 1;
86
- minY = tile.y;
87
- maxY = tile.y + 1;
88
- if (tile.zoom < zoom) {
89
- var dz = zoom - tile.zoom;
90
- minX = minX << dz;
91
- maxX = maxX << dz;
92
- minY = minY << dz;
93
- maxY = maxY << dz;
94
- }
95
- else if (tile.zoom > zoom) {
96
- var dz = tile.zoom - zoom;
97
- minX = minX >> dz;
98
- minY = minY >> dz;
99
- maxX = minX + 1;
100
- maxY = minY + 1;
101
- }
102
- return {
103
- minX: minX,
104
- minY: minY,
105
- maxX: maxX,
106
- maxY: maxY,
107
- zoom: zoom,
108
- };
109
- };
110
- /**
111
- * generate tile hashes
112
- * @param footprint footprint to cover with generated tile hashes
113
- * @param zoom max hash zoom
114
- * @returns
115
- */
116
- TileRanger.prototype.encodeFootprint = function (footprint, zoom, verbose) {
117
- var bbox, tileRange, intersectionParams;
118
- if (verbose === void 0) { verbose = false; }
119
- return __generator(this, function (_a) {
120
- switch (_a.label) {
121
- case 0:
122
- ////////////////////////////////
123
- /// Step 1: check if the footprint is identical to its bbox
124
- ////////////////////////////////
125
- if (verbose) {
126
- console.log('encode footprint');
127
- }
128
- bbox = (0, turf_1.bbox)(footprint);
129
- if (!this.isBbox(footprint)) return [3 /*break*/, 2];
130
- tileRange = (0, bboxUtils_1.bboxToTileRange)(bbox, zoom);
131
- if (verbose) {
132
- console.log("footprint is identical to its bbox - return BBOX tile range zoom: ".concat(tileRange.zoom, " : X ").concat(tileRange.minX, " - ").concat(tileRange.maxX, " : Y ").concat(tileRange.minY, " - ").concat(tileRange.maxY));
133
- }
134
- return [4 /*yield*/, tileRange];
135
- case 1:
136
- _a.sent();
137
- return [3 /*break*/, 4];
138
- case 2:
139
- intersectionParams = {
140
- footprint: footprint,
141
- maxZoom: zoom,
142
- };
143
- if (verbose) {
144
- console.log('footprint is different from its bbox - generateRanges');
145
- }
146
- return [5 /*yield**/, __values(this.generateRanges(bbox, zoom, intersectionParams, this.tileFootprintIntersection, verbose))];
147
- case 3:
148
- _a.sent();
149
- _a.label = 4;
150
- case 4: return [2 /*return*/];
151
- }
152
- });
153
- };
154
- TileRanger.prototype.generateTiles = function (area, zoom) {
155
- var gen;
156
- if (Array.isArray(area)) {
157
- gen = [(0, bboxUtils_1.bboxToTileRange)(area, zoom)];
158
- }
159
- else {
160
- gen = this.encodeFootprint(area, zoom);
161
- }
162
- return (0, tilesGenerator_1.tilesGenerator)(gen);
163
- };
164
- TileRanger.prototype.generateRanges = function (bbox, zoom, intersectionTarget, intersectionFunction, verbose) {
165
- var boundingRange, bboxString, dx, dy, minXZoom, minYZoom, minZoom, minimalRange, x, y, tile, intersection, tileRange;
166
- if (verbose === void 0) { verbose = false; }
167
- return __generator(this, function (_a) {
168
- switch (_a.label) {
169
- case 0:
170
- /////////////////////////////////////////////////////////////////////////////////////////////////
171
- /// Step 3: Convert the bbox to tile range of the requested zoom
172
- /////////////////////////////////////////////////////////////////////////////////////////////////
173
- if (verbose) {
174
- console.log('Convert the bbox to tile range of the requested zoom');
175
- }
176
- boundingRange = (0, bboxUtils_1.bboxToTileRange)(bbox, zoom);
177
- if (verbose) {
178
- bboxString = "BBOX[0]: ".concat(bbox[0], ", BBOX[1]: ").concat(bbox[1], ", BBOX[2]: ").concat(bbox[2], ", BBOX[3]: ").concat(bbox[3]);
179
- console.log("".concat(bboxString, ", Zoom: ").concat(zoom, ", bounding range: minX: ").concat(boundingRange.minX, ", maxX: ").concat(boundingRange.maxX, ", minY: ").concat(boundingRange.minY, ", maxY: ").concat(boundingRange.maxY));
180
- }
181
- /////////////////////////////////////////////////////////////////////////////////////////////////
182
- /// Step 4: Use range size to calculate zoom level where the target area is smaller then 1 tile
183
- /// (use zoom zero in-case there is no such zoom, for example in global bbox).
184
- /////////////////////////////////////////////////////////////////////////////////////////////////
185
- // find minimal zoom where the the area can be converted by area the size of single tile to skip levels that can't have full hashes
186
- if (verbose) {
187
- console.log("find minimal zoom where the the area can be converted by area the size of single tile to skip levels that can't have full hashes");
188
- }
189
- dx = boundingRange.maxX - boundingRange.minX;
190
- dy = boundingRange.maxY - boundingRange.minY;
191
- minXZoom = Math.max(Math.floor(Math.log2(1 << (zoom + 1)) / dx) - 1, 0);
192
- minYZoom = Math.max(Math.floor(Math.log2(1 << zoom) / dy), 0);
193
- minZoom = Math.min(minXZoom, minYZoom);
194
- if (verbose) {
195
- console.log("MinZoom: ".concat(minZoom));
196
- }
197
- minimalRange = (0, bboxUtils_1.bboxToTileRange)(bbox, minZoom);
198
- x = minimalRange.minX;
199
- _a.label = 1;
200
- case 1:
201
- if (!(x < minimalRange.maxX)) return [3 /*break*/, 8];
202
- y = minimalRange.minY;
203
- _a.label = 2;
204
- case 2:
205
- if (!(y < minimalRange.maxY)) return [3 /*break*/, 7];
206
- tile = { x: x, y: y, zoom: minimalRange.zoom };
207
- intersection = intersectionFunction(tile, intersectionTarget);
208
- if (verbose) {
209
- console.log("Tile X: ".concat(tile.x, ", Y: ").concat(tile.y, " zoom ").concat(tile.zoom, ", intersection: ").concat(intersection));
210
- }
211
- if (!(intersection === TileIntersectionState.FULL)) return [3 /*break*/, 4];
212
- tileRange = this.tileToRange(tile, zoom);
213
- if (verbose) {
214
- console.log("return BBOX tile range zoom: ".concat(tileRange.zoom, " : X ").concat(tileRange.minX, " - ").concat(tileRange.maxX, " : Y ").concat(tileRange.minY, " - ").concat(tileRange.maxY));
215
- }
216
- return [4 /*yield*/, tileRange];
217
- case 3:
218
- _a.sent();
219
- return [3 /*break*/, 6];
220
- case 4:
221
- if (!(intersection === TileIntersectionState.PARTIAL)) return [3 /*break*/, 6];
222
- /// if it partly covered:
223
- // calculate the sub tiles contained in the current tile (in the next zoom level)
224
- // for every sub tile recursively run step 6
225
- //optimize partial base hashes
226
- return [5 /*yield**/, __values(this.optimizeHash(tile, zoom, intersectionTarget, intersectionFunction, verbose))];
227
- case 5:
228
- /// if it partly covered:
229
- // calculate the sub tiles contained in the current tile (in the next zoom level)
230
- // for every sub tile recursively run step 6
231
- //optimize partial base hashes
232
- _a.sent();
233
- _a.label = 6;
234
- case 6:
235
- y++;
236
- return [3 /*break*/, 2];
237
- case 7:
238
- x++;
239
- return [3 /*break*/, 1];
240
- case 8: return [2 /*return*/];
241
- }
242
- });
243
- };
244
- /**
245
- * generate tile
246
- * @param tile tile to get all intersecting tiles from
247
- * @param targetZoom target tiles zoom level
248
- * @param intersectionTarget original zoom level and footprint to intersect
249
- * @param intersectionFunction the intersection function to be called
250
- */
251
- TileRanger.prototype.optimizeHash = function (tile, targetZoom, intersectionTarget, intersectionFunction, verbose) {
252
- var tiles, tiles_2, tiles_2_1, subTile, intersection, tileRange, e_1_1;
253
- var e_1, _a;
254
- if (verbose === void 0) { verbose = false; }
255
- return __generator(this, function (_b) {
256
- switch (_b.label) {
257
- case 0:
258
- /// generate from a tile the next zoom level tiles that compose the tile
259
- if (verbose) {
260
- console.log("optimizeHash: Tile X: ".concat(tile.x, ", Y: ").concat(tile.y, " zoom ").concat(tile.zoom, ", intersectionTarget ").concat(intersectionTarget.maxZoom, ", - generate from a tile the next zoom level tiles that compose the tile"));
261
- }
262
- tiles = this.generateSubTiles(tile);
263
- _b.label = 1;
264
- case 1:
265
- _b.trys.push([1, 8, 9, 10]);
266
- tiles_2 = __values(tiles), tiles_2_1 = tiles_2.next();
267
- _b.label = 2;
268
- case 2:
269
- if (!!tiles_2_1.done) return [3 /*break*/, 7];
270
- subTile = tiles_2_1.value;
271
- intersection = intersectionFunction(subTile, intersectionTarget);
272
- if (verbose) {
273
- console.log("Tile X: ".concat(subTile.x, ", Y: ").concat(subTile.y, " zoom ").concat(subTile.zoom, ", intersection: ").concat(intersection));
274
- }
275
- if (!(intersection === TileIntersectionState.FULL)) return [3 /*break*/, 4];
276
- tileRange = this.tileToRange(subTile, targetZoom);
277
- if (verbose) {
278
- console.log("return BBOX tile range zoom: ".concat(tileRange.zoom, " : X ").concat(tileRange.minX, " - ").concat(tileRange.maxX, " : Y ").concat(tileRange.minY, " - ").concat(tileRange.maxY));
279
- }
280
- return [4 /*yield*/, tileRange];
281
- case 3:
282
- _b.sent();
283
- return [3 /*break*/, 6];
284
- case 4:
285
- if (!(intersection === TileIntersectionState.PARTIAL)) return [3 /*break*/, 6];
286
- /// if it partly covered:
287
- // calculate the sub tiles contained in the current tile (in the next zoom level) - recursive
288
- return [5 /*yield**/, __values(this.optimizeHash(subTile, targetZoom, intersectionTarget, intersectionFunction, verbose))];
289
- case 5:
290
- /// if it partly covered:
291
- // calculate the sub tiles contained in the current tile (in the next zoom level) - recursive
292
- _b.sent();
293
- _b.label = 6;
294
- case 6:
295
- tiles_2_1 = tiles_2.next();
296
- return [3 /*break*/, 2];
297
- case 7: return [3 /*break*/, 10];
298
- case 8:
299
- e_1_1 = _b.sent();
300
- e_1 = { error: e_1_1 };
301
- return [3 /*break*/, 10];
302
- case 9:
303
- try {
304
- if (tiles_2_1 && !tiles_2_1.done && (_a = tiles_2.return)) _a.call(tiles_2);
305
- }
306
- finally { if (e_1) throw e_1.error; }
307
- return [7 /*endfinally*/];
308
- case 10: return [2 /*return*/];
309
- }
310
- });
311
- };
312
- TileRanger.prototype.generateSubTiles = function (tile) {
313
- var tile0 = { x: tile.x << 1, y: tile.y << 1, zoom: tile.zoom + 1 };
314
- var tile1 = { x: tile0.x + 1, y: tile0.y, zoom: tile0.zoom };
315
- var tile2 = { x: tile0.x, y: tile0.y + 1, zoom: tile0.zoom };
316
- var tile3 = { x: tile0.x + 1, y: tile0.y + 1, zoom: tile0.zoom };
317
- var tiles = [tile0, tile1, tile2, tile3];
318
- return tiles;
319
- };
320
- TileRanger.prototype.isBbox = function (footprint) {
321
- var bbox = (0, turf_1.bbox)(footprint);
322
- var bboxPoly = (0, turf_1.bboxPolygon)(bbox);
323
- return (0, turf_1.booleanEqual)(footprint, bboxPoly);
324
- };
325
- return TileRanger;
326
- }());
327
- exports.TileRanger = TileRanger;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TileRanger = void 0;
4
+ const turf_1 = require("@turf/turf");
5
+ const bboxUtils_1 = require("./bboxUtils");
6
+ const tiles_1 = require("./tiles");
7
+ const tilesGenerator_1 = require("./tilesGenerator");
8
+ var TileIntersectionState;
9
+ (function (TileIntersectionState) {
10
+ TileIntersectionState["FULL"] = "full";
11
+ TileIntersectionState["PARTIAL"] = "partial";
12
+ TileIntersectionState["NONE"] = "none";
13
+ })(TileIntersectionState || (TileIntersectionState = {}));
14
+ /**
15
+ * class for generating and decoding tile hashes
16
+ */
17
+ class TileRanger {
18
+ /**
19
+ * converts tile to tile range of specified zoom level
20
+ * @param tile
21
+ * @param zoom target tile range zoom
22
+ * @returns
23
+ */
24
+ tileToRange(tile, zoom) {
25
+ let minX, minY, maxX, maxY;
26
+ minX = tile.x;
27
+ maxX = tile.x + 1;
28
+ minY = tile.y;
29
+ maxY = tile.y + 1;
30
+ if (tile.zoom < zoom) {
31
+ const dz = zoom - tile.zoom;
32
+ minX = minX << dz;
33
+ maxX = maxX << dz;
34
+ minY = minY << dz;
35
+ maxY = maxY << dz;
36
+ }
37
+ else if (tile.zoom > zoom) {
38
+ const dz = tile.zoom - zoom;
39
+ minX = minX >> dz;
40
+ minY = minY >> dz;
41
+ maxX = minX + 1;
42
+ maxY = minY + 1;
43
+ }
44
+ return {
45
+ minX,
46
+ minY,
47
+ maxX,
48
+ maxY,
49
+ zoom,
50
+ };
51
+ }
52
+ /**
53
+ * generate tile hashes
54
+ * @param footprint footprint to cover with generated tile hashes
55
+ * @param zoom max hash zoom
56
+ * @returns
57
+ */
58
+ async *encodeFootprint(footprint, zoom, verbose = false) {
59
+ ////////////////////////////////
60
+ /// Step 1: check if the footprint is identical to its bbox
61
+ ////////////////////////////////
62
+ if (verbose) {
63
+ console.log('encode footprint');
64
+ }
65
+ ////////////////////////////////
66
+ /// Step 2: convert footprint to BBOX
67
+ ////////////////////////////////
68
+ const bbox = (0, turf_1.bbox)(footprint);
69
+ if (this.isBbox(footprint)) {
70
+ // if it is convert its bbox directly to tile range and return it (bbox to tiles conversion is fast and direct mathematical conversion)
71
+ const tileRange = (0, bboxUtils_1.bboxToTileRange)(bbox, zoom);
72
+ if (verbose) {
73
+ console.log(`footprint is identical to its bbox - return BBOX tile range zoom: ${tileRange.zoom} : X ${tileRange.minX} - ${tileRange.maxX} : Y ${tileRange.minY} - ${tileRange.maxY}`);
74
+ }
75
+ yield await Promise.resolve(tileRange);
76
+ }
77
+ else {
78
+ const intersectionParams = {
79
+ footprint,
80
+ maxZoom: zoom,
81
+ };
82
+ if (verbose) {
83
+ console.log('footprint is different from its bbox - generateRanges');
84
+ }
85
+ yield* await Promise.resolve(this.generateRanges(bbox, zoom, intersectionParams, this.tileFootprintIntersection, verbose));
86
+ }
87
+ }
88
+ generateTiles(area, zoom) {
89
+ let gen;
90
+ if (Array.isArray(area)) {
91
+ const tileRangeGen = async function* tileRangeGenerator() {
92
+ yield await Promise.resolve((0, bboxUtils_1.bboxToTileRange)(area, zoom));
93
+ };
94
+ gen = tileRangeGen();
95
+ }
96
+ else {
97
+ gen = this.encodeFootprint(area, zoom);
98
+ }
99
+ return (0, tilesGenerator_1.tilesGenerator)(gen);
100
+ }
101
+ async *generateRanges(bbox, zoom, intersectionTarget, intersectionFunction, verbose = false) {
102
+ /////////////////////////////////////////////////////////////////////////////////////////////////
103
+ /// Step 3: Convert the bbox to tile range of the requested zoom
104
+ /////////////////////////////////////////////////////////////////////////////////////////////////
105
+ if (verbose) {
106
+ console.log('Convert the bbox to tile range of the requested zoom');
107
+ }
108
+ const boundingRange = (0, bboxUtils_1.bboxToTileRange)(bbox, zoom);
109
+ if (verbose) {
110
+ const bboxString = `BBOX[0]: ${bbox[0]}, BBOX[1]: ${bbox[1]}, BBOX[2]: ${bbox[2]}, BBOX[3]: ${bbox[3]}`;
111
+ console.log(`${bboxString}, Zoom: ${zoom}, bounding range: minX: ${boundingRange.minX}, maxX: ${boundingRange.maxX}, minY: ${boundingRange.minY}, maxY: ${boundingRange.maxY}`);
112
+ }
113
+ /////////////////////////////////////////////////////////////////////////////////////////////////
114
+ /// Step 4: Use range size to calculate zoom level where the target area is smaller then 1 tile
115
+ /// (use zoom zero in-case there is no such zoom, for example in global bbox).
116
+ /////////////////////////////////////////////////////////////////////////////////////////////////
117
+ // find minimal zoom where the the area can be converted by area the size of single tile to skip levels that can't have full hashes
118
+ if (verbose) {
119
+ console.log("find minimal zoom where the the area can be converted by area the size of single tile to skip levels that can't have full hashes");
120
+ }
121
+ const dx = boundingRange.maxX - boundingRange.minX;
122
+ const dy = boundingRange.maxY - boundingRange.minY;
123
+ const minXZoom = Math.max(Math.floor(Math.log2(1 << (zoom + 1)) / dx) - 1, 0);
124
+ const minYZoom = Math.max(Math.floor(Math.log2(1 << zoom) / dy), 0);
125
+ const minZoom = Math.min(minXZoom, minYZoom);
126
+ if (verbose) {
127
+ console.log(`MinZoom: ${minZoom}`);
128
+ }
129
+ /////////////////////////////////////////////////////////////////////////////////////////////////
130
+ /// Step 5: convert the requested bbox to to tile range of the zoom level calculated in step 3 (this reduce the iteration required for the calculation)
131
+ /////////////////////////////////////////////////////////////////////////////////////////////////
132
+ //find base hashes
133
+ const minimalRange = (0, bboxUtils_1.bboxToTileRange)(bbox, minZoom);
134
+ for (let x = minimalRange.minX; x < minimalRange.maxX; x++) {
135
+ for (let y = minimalRange.minY; y < minimalRange.maxY; y++) {
136
+ /////////////////////////////////////////////////////////////////////////////////////////////////
137
+ /// Step 6: for every tile in the current range:
138
+ /// Step 7: check the tile intersection with the footprint
139
+ /////////////////////////////////////////////////////////////////////////////////////////////////
140
+ const tile = { x, y, zoom: minimalRange.zoom };
141
+ const intersection = intersectionFunction(tile, intersectionTarget);
142
+ if (verbose) {
143
+ console.log(`Tile X: ${tile.x}, Y: ${tile.y} zoom ${tile.zoom}, intersection: ${intersection}`);
144
+ }
145
+ /// if it is completely covered or the tile is in the requested zoom:
146
+ if (intersection === TileIntersectionState.FULL) {
147
+ /// convert it to tile range of the requested resolution
148
+ /// add the range to the result set (yield is used for lazy calculation to improve memory usage)
149
+ const tileRange = this.tileToRange(tile, zoom);
150
+ if (verbose) {
151
+ console.log(`return BBOX tile range zoom: ${tileRange.zoom} : X ${tileRange.minX} - ${tileRange.maxX} : Y ${tileRange.minY} - ${tileRange.maxY}`);
152
+ }
153
+ yield await Promise.resolve(tileRange);
154
+ }
155
+ else if (intersection === TileIntersectionState.PARTIAL) {
156
+ /// if it partly covered:
157
+ // calculate the sub tiles contained in the current tile (in the next zoom level)
158
+ // for every sub tile recursively run step 6
159
+ //optimize partial base hashes
160
+ yield* await Promise.resolve(this.optimizeHash(tile, zoom, intersectionTarget, intersectionFunction, verbose));
161
+ }
162
+ /// else do nothing as this tiles aren't intersected with the original footprint
163
+ }
164
+ }
165
+ }
166
+ /**
167
+ * generate tile
168
+ * @param tile tile to get all intersecting tiles from
169
+ * @param targetZoom target tiles zoom level
170
+ * @param intersectionTarget original zoom level and footprint to intersect
171
+ * @param intersectionFunction the intersection function to be called
172
+ */
173
+ *optimizeHash(tile, targetZoom, intersectionTarget, intersectionFunction, verbose = false) {
174
+ /// generate from a tile the next zoom level tiles that compose the tile
175
+ if (verbose) {
176
+ console.log(`optimizeHash: Tile X: ${tile.x}, Y: ${tile.y} zoom ${tile.zoom}, intersectionTarget ${intersectionTarget.maxZoom}, - generate from a tile the next zoom level tiles that compose the tile`);
177
+ }
178
+ const tiles = this.generateSubTiles(tile);
179
+ for (const subTile of tiles) {
180
+ const intersection = intersectionFunction(subTile, intersectionTarget);
181
+ if (verbose) {
182
+ console.log(`Tile X: ${subTile.x}, Y: ${subTile.y} zoom ${subTile.zoom}, intersection: ${intersection}`);
183
+ }
184
+ if (intersection === TileIntersectionState.FULL) {
185
+ /// convert it to tile range of the requested resolution
186
+ /// add the range to the result set (yield is used for lazy calculation to improve memory usage)
187
+ const tileRange = this.tileToRange(subTile, targetZoom);
188
+ if (verbose) {
189
+ console.log(`return BBOX tile range zoom: ${tileRange.zoom} : X ${tileRange.minX} - ${tileRange.maxX} : Y ${tileRange.minY} - ${tileRange.maxY}`);
190
+ }
191
+ yield tileRange;
192
+ }
193
+ else if (intersection === TileIntersectionState.PARTIAL) {
194
+ /// if it partly covered:
195
+ // calculate the sub tiles contained in the current tile (in the next zoom level) - recursive
196
+ yield* this.optimizeHash(subTile, targetZoom, intersectionTarget, intersectionFunction, verbose);
197
+ }
198
+ }
199
+ }
200
+ generateSubTiles(tile) {
201
+ const tile0 = { x: tile.x << 1, y: tile.y << 1, zoom: tile.zoom + 1 };
202
+ const tile1 = { x: tile0.x + 1, y: tile0.y, zoom: tile0.zoom };
203
+ const tile2 = { x: tile0.x, y: tile0.y + 1, zoom: tile0.zoom };
204
+ const tile3 = { x: tile0.x + 1, y: tile0.y + 1, zoom: tile0.zoom };
205
+ const tiles = [tile0, tile1, tile2, tile3];
206
+ return tiles;
207
+ }
208
+ tileFootprintIntersection = (tile, intersectionParams) => {
209
+ const tileBbox = (0, tiles_1.tileToBbox)(tile);
210
+ const tilePoly = (0, turf_1.bboxPolygon)(tileBbox);
211
+ const intersection = (0, turf_1.intersect)(intersectionParams.footprint, tilePoly);
212
+ if (intersection === null) {
213
+ return TileIntersectionState.NONE;
214
+ }
215
+ // stop condition
216
+ if (tile.zoom === intersectionParams.maxZoom) {
217
+ return TileIntersectionState.FULL;
218
+ }
219
+ const intArea = (0, turf_1.area)(intersection);
220
+ const hashArea = (0, turf_1.area)(tilePoly);
221
+ if (intArea == hashArea) {
222
+ return TileIntersectionState.FULL;
223
+ }
224
+ return TileIntersectionState.PARTIAL;
225
+ };
226
+ isBbox(footprint) {
227
+ const bbox = (0, turf_1.bbox)(footprint);
228
+ const bboxPoly = (0, turf_1.bboxPolygon)(bbox);
229
+ return (0, turf_1.booleanEqual)(footprint, bboxPoly);
230
+ }
231
+ }
232
+ exports.TileRanger = TileRanger;
328
233
  //# sourceMappingURL=tileRanger.js.map