leaflet-city-mapper 0.0.5 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (261) hide show
  1. package/README.md +10 -5
  2. package/dist/leaflet-city-mapper-src.js +6874 -6304
  3. package/dist/leaflet-city-mapper-src.js.map +4 -4
  4. package/dist/leaflet-city-mapper-worker-src.js +2766 -0
  5. package/dist/leaflet-city-mapper-worker-src.js.map +4 -4
  6. package/dist/leaflet-city-mapper-worker.js +1 -1
  7. package/dist/leaflet-city-mapper-worker.js.map +4 -4
  8. package/dist/leaflet-city-mapper.js +5 -3
  9. package/dist/leaflet-city-mapper.js.map +4 -4
  10. package/libs/leaflet-lens.js +15 -3
  11. package/libs/leaflet.js +1 -1
  12. package/package.json +1 -1
  13. package/src/camera/setBearing.js +5 -0
  14. package/src/camera/setPitch.js +5 -0
  15. package/src/cog/initCog.js +2 -2
  16. package/src/collision/collectIntersections.js +16 -0
  17. package/src/collision/collider.js +3 -2
  18. package/src/collision/countCrossings.js +15 -0
  19. package/src/collision/insideMesh.js +4 -24
  20. package/src/collision/insideWalker.js +2 -2
  21. package/src/collision/readTriangle.js +7 -4
  22. package/src/collision/setTrafficNormal.js +9 -0
  23. package/src/collision/trafficCollision.js +4 -9
  24. package/src/collision/vertexCount.js +3 -0
  25. package/src/controls/cameraControls.js +6 -0
  26. package/src/core/gridlayerGetTiledPixelBounds.js +37 -4
  27. package/src/core/markerSetPos.js +1 -1
  28. package/src/core/popupGetEvents.js +1 -1
  29. package/src/core/popupUpdatePosition.js +8 -8
  30. package/src/core/tooltipGetEvents.js +1 -1
  31. package/src/core/tooltipSetPosition.js +2 -2
  32. package/src/geometry/appendNativeTriangle.js +2 -2
  33. package/src/geometry/createModelGeometry.js +2 -9
  34. package/src/geometry/dashedSegment.js +10 -5
  35. package/src/geometry/disk.js +1 -1
  36. package/src/geometry/fillNativePolygon.js +20 -0
  37. package/src/geometry/lineJoin.js +2 -2
  38. package/src/geometry/markerQuadCorners.js +6 -0
  39. package/src/geometry/nativeGeometry.js +1 -1
  40. package/src/geometry/polygon.js +4 -10
  41. package/src/geometry/segment.js +4 -3
  42. package/src/globals/colorTexture.js +6 -0
  43. package/src/globals/depthTexture.js +1 -0
  44. package/src/globals/drawMeshEntry.js +1 -1
  45. package/src/globals/meshPipeline.js +1 -0
  46. package/src/globals/mvtTextDecoder.js +1 -0
  47. package/src/globals/nativeScalableStyleKeys.js +1 -0
  48. package/src/globals/scenePass.js +1 -0
  49. package/src/globals/vectorStyleNamedColors.js +8 -0
  50. package/src/globals/vectorTileEvents.js +1 -0
  51. package/src/globals/workerCogSource.js +1 -0
  52. package/src/gpu/mountRenderer.js +4 -11
  53. package/src/gpu/releaseRenderer.js +1 -0
  54. package/src/index.js +6 -12
  55. package/src/materials/createProgramUniforms.js +1 -0
  56. package/src/models/loadModel.js +6 -10
  57. package/src/models/loadModelResources.js +2 -8
  58. package/src/models/modelNode.js +3 -7
  59. package/src/models/prepareModel.js +19 -0
  60. package/src/models/prepareModelGeometry.js +25 -0
  61. package/src/models/prepareModelPrimitive.js +17 -0
  62. package/src/mvt/addMvtCrossings.js +17 -0
  63. package/src/mvt/appendMvtBandTriangles.js +31 -0
  64. package/src/mvt/appendMvtCoordinates.js +11 -0
  65. package/src/mvt/appendMvtPolygonTriangles.js +19 -0
  66. package/src/mvt/beginMvtPath.js +9 -0
  67. package/src/mvt/closeMvtPath.js +7 -0
  68. package/src/mvt/collectMvtEdges.js +19 -0
  69. package/src/mvt/createMvtReader.js +7 -0
  70. package/src/mvt/decodeVectorTile.js +32 -0
  71. package/src/mvt/expectMvtWire.js +3 -0
  72. package/src/mvt/finishMvtPath.js +5 -0
  73. package/src/mvt/finiteMvtCoordinate.js +7 -0
  74. package/src/mvt/groupMvtPolygons.js +25 -0
  75. package/src/mvt/isMvtFallbackError.js +6 -0
  76. package/src/mvt/mergeMvtActiveEdges.js +9 -0
  77. package/src/mvt/mvtEdgeX.js +4 -0
  78. package/src/mvt/mvtPathsVertices.js +8 -0
  79. package/src/mvt/mvtPolygonPaths.js +12 -0
  80. package/src/mvt/numericMvtValue.js +4 -0
  81. package/src/mvt/readMvtFeature.js +34 -0
  82. package/src/mvt/readMvtField.js +8 -0
  83. package/src/mvt/readMvtGeometry.js +29 -0
  84. package/src/mvt/readMvtInteger.js +12 -0
  85. package/src/mvt/readMvtKnownValue.js +21 -0
  86. package/src/mvt/readMvtLayer.js +31 -0
  87. package/src/mvt/readMvtMessage.js +10 -0
  88. package/src/mvt/readMvtNumber.js +9 -0
  89. package/src/mvt/readMvtNumbers.js +11 -0
  90. package/src/mvt/readMvtString.js +6 -0
  91. package/src/mvt/readMvtValue.js +19 -0
  92. package/src/mvt/skipMvtField.js +14 -0
  93. package/src/mvt/sortMvtActiveEdges.js +15 -0
  94. package/src/mvt/sortedMvtLevels.js +22 -0
  95. package/src/mvt/takeMvtBytes.js +8 -0
  96. package/src/mvt/tessellateVectorRings.js +19 -0
  97. package/src/mvt/triangulateVectorRings.js +19 -0
  98. package/src/native/beforeNativeRender.js +6 -0
  99. package/src/native/canScaleNativeStyle.js +17 -0
  100. package/src/native/createNativeMesh.js +31 -0
  101. package/src/native/disposeNativeLayer.js +1 -1
  102. package/src/native/getGridTerrainTile.js +34 -0
  103. package/src/native/gridTerrainElevation.js +30 -0
  104. package/src/native/loadGridTerrain.js +26 -0
  105. package/src/native/nativeLayer.js +2 -2
  106. package/src/native/nativeLayerMethod.js +5 -14
  107. package/src/native/nativeProgram.js +20 -0
  108. package/src/native/nativeVectorGeometry.js +23 -0
  109. package/src/native/orderNativeGridLayer.js +14 -0
  110. package/src/native/putNativeMesh.js +15 -21
  111. package/src/native/refreshNativeEvent.js +13 -2
  112. package/src/native/refreshNativeLayer.js +1 -0
  113. package/src/native/syncGridLayer.js +48 -3
  114. package/src/native/syncNativeStrokeAnchors.js +12 -0
  115. package/src/native/syncNativeStrokeZoom.js +29 -0
  116. package/src/native/syncVectorLayer.js +15 -13
  117. package/src/native/terrainGridMesh.js +34 -0
  118. package/src/native/updateNativeStrokeRadius.js +9 -0
  119. package/src/projections/displayContainerPoint.js +23 -3
  120. package/src/projections/groundBounds.js +2 -2
  121. package/src/projections/localPixel.js +7 -2
  122. package/src/projections/localPixelForGeometry.js +4 -2
  123. package/src/projections/projectCameraPoint.js +6 -5
  124. package/src/raster/setRasterOpacity.js +1 -1
  125. package/src/raster/setRasterZIndex.js +1 -1
  126. package/src/rendering/collectVisibleMesh.js +5 -2
  127. package/src/rendering/drawSceneMeshes.js +3 -1
  128. package/src/rendering/nativeStrokeExpansion.js +4 -0
  129. package/src/rendering/prepareMeshUniforms.js +2 -0
  130. package/src/rendering/resizeRenderer.js +9 -0
  131. package/src/scene/initializerFor.js +3 -5
  132. package/src/scene/layerInitialize.js +14 -12
  133. package/src/scene/resolveNextInitialization.js +1 -1
  134. package/src/shaders/materialShaders.js +1 -0
  135. package/src/shaders/materialVertexShader.js +7 -2
  136. package/src/shaders/nativeStrokeShader.js +6 -0
  137. package/src/sources/attachAreaMesh.js +5 -6
  138. package/src/sources/buildArea.js +7 -17
  139. package/src/sources/buildAreaFeature.js +5 -24
  140. package/src/sources/buildAreaFeatures.js +14 -0
  141. package/src/sources/initVectorTiles.js +15 -0
  142. package/src/sources/isVectorTileSource.js +4 -0
  143. package/src/sources/matchesAreaTile.js +7 -0
  144. package/src/sources/prepareAreaGeometryJob.js +2 -11
  145. package/src/sources/projectAreaPolygons.js +15 -0
  146. package/src/sources/readyVectorTiles.js +10 -0
  147. package/src/sources/sourceAreaContext.js +1 -6
  148. package/src/sunlight/createSun.js +0 -1
  149. package/src/terrain/initTerrain.js +7 -1
  150. package/src/terrain/loadTerrain.js +4 -1
  151. package/src/terrain/mapElevationOf.js +5 -1
  152. package/src/terrain/onTerrainAdd.js +11 -1
  153. package/src/terrain/onTerrainRemove.js +10 -1
  154. package/src/utils/yieldTask.js +5 -0
  155. package/src/vector-style/appendQuotedStyleCharacter.js +22 -0
  156. package/src/vector-style/arrayContainsRange.js +8 -0
  157. package/src/vector-style/evaluateBoolean.js +18 -0
  158. package/src/vector-style/evaluateCoalesceOrCase.js +21 -0
  159. package/src/vector-style/evaluateComparison.js +16 -0
  160. package/src/vector-style/evaluateIExpression.js +8 -0
  161. package/src/vector-style/evaluateInput.js +6 -0
  162. package/src/vector-style/evaluateInterpolate.js +27 -0
  163. package/src/vector-style/evaluateMatch.js +12 -0
  164. package/src/vector-style/evaluateMath.js +14 -0
  165. package/src/vector-style/evaluateMembership.js +18 -0
  166. package/src/vector-style/evaluateNegation.js +13 -0
  167. package/src/vector-style/evaluatePrimitive.js +9 -0
  168. package/src/vector-style/evaluateStep.js +11 -0
  169. package/src/vector-style/evaluateStyle.js +33 -0
  170. package/src/vector-style/evaluateToExpression.js +14 -0
  171. package/src/vector-style/fillVectorPaint.js +17 -0
  172. package/src/vector-style/lineVectorPaint.js +21 -0
  173. package/src/vector-style/parseFunctionalColor.js +14 -0
  174. package/src/vector-style/parseHexColor.js +13 -0
  175. package/src/vector-style/parseVectorStyle.js +13 -0
  176. package/src/vector-style/readPaintValue.js +6 -0
  177. package/src/vector-style/removeTrailingCommas.js +12 -0
  178. package/src/vector-style/scaleDashArray.js +13 -0
  179. package/src/vector-style/skipStyleComment.js +9 -0
  180. package/src/vector-style/stripStyleComments.js +21 -0
  181. package/src/vector-style/styleColor.js +11 -0
  182. package/src/vector-style/validateVectorLayers.js +10 -0
  183. package/src/vector-style/vectorPaint.js +8 -0
  184. package/src/vector-style/visibleStyle.js +7 -0
  185. package/src/vector-tiles/appendVectorBatch.js +22 -0
  186. package/src/vector-tiles/attachVectorTileBatch.js +17 -0
  187. package/src/vector-tiles/attachVectorTileBatches.js +15 -0
  188. package/src/vector-tiles/compareVectorLabelPriority.js +3 -0
  189. package/src/vector-tiles/compareVectorTileDistance.js +3 -0
  190. package/src/vector-tiles/configureVectorTileLayer.js +28 -0
  191. package/src/vector-tiles/forwardVectorTileError.js +5 -0
  192. package/src/vector-tiles/loadVectorSprite.js +5 -0
  193. package/src/vector-tiles/loadVectorTile.js +36 -0
  194. package/src/vector-tiles/onVectorTileAdd.js +39 -0
  195. package/src/vector-tiles/onVectorTileMessage.js +10 -0
  196. package/src/vector-tiles/onVectorTileRemove.js +20 -0
  197. package/src/vector-tiles/onVectorTileWorkerError.js +11 -0
  198. package/src/vector-tiles/paintVectorTileBatch.js +12 -0
  199. package/src/vector-tiles/prepareVectorLabel.js +43 -0
  200. package/src/vector-tiles/prepareVectorStyleLayer.js +30 -0
  201. package/src/vector-tiles/prepareVectorTile.js +16 -0
  202. package/src/vector-tiles/projectVectorPaths.js +15 -0
  203. package/src/vector-tiles/projectVectorPoint.js +6 -0
  204. package/src/vector-tiles/projectVectorTriangles.js +18 -0
  205. package/src/vector-tiles/pruneVectorTiles.js +11 -0
  206. package/src/vector-tiles/pumpVectorTiles.js +18 -0
  207. package/src/vector-tiles/readTileJSON.js +17 -0
  208. package/src/vector-tiles/readVectorStyle.js +17 -0
  209. package/src/vector-tiles/refreshVectorTiles.js +5 -0
  210. package/src/vector-tiles/reserveVectorLabelCells.js +6 -0
  211. package/src/vector-tiles/retireVectorTileGroup.js +13 -0
  212. package/src/vector-tiles/revokeVectorWorkerUrl.js +9 -0
  213. package/src/vector-tiles/rotateVectorLabel.js +12 -0
  214. package/src/vector-tiles/rotateVectorLabels.js +6 -0
  215. package/src/vector-tiles/simplifyVectorPath.js +21 -0
  216. package/src/vector-tiles/updateVectorLabelMarker.js +27 -0
  217. package/src/vector-tiles/updateVectorTileLabels.js +28 -0
  218. package/src/vector-tiles/updateVectorTilePaint.js +20 -0
  219. package/src/vector-tiles/updateVectorTiles.js +29 -0
  220. package/src/vector-tiles/validateVectorTileZooms.js +6 -0
  221. package/src/vector-tiles/vectorFeaturePaint.js +15 -0
  222. package/src/vector-tiles/vectorFeaturePaths.js +16 -0
  223. package/src/vector-tiles/vectorFeatureZoom.js +8 -0
  224. package/src/vector-tiles/vectorLabelAnchors.js +24 -0
  225. package/src/vector-tiles/vectorLabelCandidates.js +11 -0
  226. package/src/vector-tiles/vectorLabelCells.js +26 -0
  227. package/src/vector-tiles/vectorLabelIcon.js +30 -0
  228. package/src/vector-tiles/vectorLabelProperty.js +3 -0
  229. package/src/vector-tiles/vectorSpriteIcon.js +11 -0
  230. package/src/vector-tiles/vectorStraightRun.js +14 -0
  231. package/src/vector-tiles/vectorStyleDependencies.js +15 -0
  232. package/src/vector-tiles/vectorStyleValue.js +5 -0
  233. package/src/vector-tiles/vectorStyleZoom.js +6 -0
  234. package/src/vector-tiles/vectorTileAwaited.js +9 -0
  235. package/src/vector-tiles/vectorTileOverlaps.js +4 -0
  236. package/src/vector-tiles/vectorTilePaint.js +10 -0
  237. package/src/vector-tiles/vectorTileSettings.js +17 -0
  238. package/src/vector-tiles/vectorTilesIdle.js +3 -0
  239. package/src/vector-tiles/vectorTolerance.js +4 -0
  240. package/src/vector-tiles/wrapVectorLabel.js +11 -0
  241. package/src/walk/animateWalker.js +2 -13
  242. package/src/walk/updateWalkerFrame.js +19 -0
  243. package/src/weakmaps/nativeProgramsByRenderer.js +1 -0
  244. package/src/weakmaps/streamTerrainByLayer.js +1 -0
  245. package/src/weakmaps/streamTerrainByMap.js +1 -0
  246. package/src/weakmaps/vectorTileOptionsByLayer.js +1 -0
  247. package/src/weakmaps/vectorWorkerByLayer.js +1 -0
  248. package/src/weakmaps/workerMapByLayer.js +1 -0
  249. package/src/workers/acquireGeometryWorker.js +2 -5
  250. package/src/workers/buildAreaInWorker.js +3 -23
  251. package/src/workers/cogWorker.js +21 -0
  252. package/src/workers/createGeometryWorker.js +20 -0
  253. package/src/workers/geometryWorker.js +15 -0
  254. package/src/workers/getCogInWorker.js +7 -0
  255. package/src/workers/modelWorker.js +10 -0
  256. package/src/workers/openCogInWorker.js +14 -0
  257. package/src/workers/runSourceWorker.js +32 -0
  258. package/src/workers/settleGeometryWorker.js +1 -1
  259. package/src/workers/vectorTileWorker.js +10 -0
  260. package/src/geometry/buildExtrudedPolygon.js +0 -10
  261. package/src/sources/buildAreaPoint.js +0 -7
@@ -930,8 +930,2774 @@
930
930
  return result;
931
931
  }
932
932
 
933
+ // src/mvt/createMvtReader.js
934
+ function createMvtReader(bytes) {
935
+ return (
936
+ /* alloc */
937
+ {
938
+ bytes,
939
+ view: (
940
+ /* alloc */
941
+ new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength)
942
+ ),
943
+ pos: 0
944
+ }
945
+ );
946
+ }
947
+
948
+ // src/mvt/takeMvtBytes.js
949
+ function takeMvtBytes(reader, size) {
950
+ if (!Number.isSafeInteger(size) || size < 0 || size > reader.bytes.length - reader.pos) {
951
+ throw (
952
+ /* alloc */
953
+ new Error("Truncated MVT field")
954
+ );
955
+ }
956
+ const start = reader.pos;
957
+ reader.pos += size;
958
+ return start;
959
+ }
960
+
961
+ // src/mvt/readMvtInteger.js
962
+ function readMvtInteger(reader) {
963
+ let value = 0n, i = 0, byte;
964
+ for (; i < 10; i++) {
965
+ byte = reader.bytes[takeMvtBytes(reader, 1)];
966
+ if (i === 9 && byte > 1) throw (
967
+ /* alloc */
968
+ new Error("Invalid MVT varint")
969
+ );
970
+ value |= BigInt(byte & 127) << BigInt(i * 7);
971
+ if (byte < 128) return value;
972
+ }
973
+ throw (
974
+ /* alloc */
975
+ new Error("Invalid MVT varint")
976
+ );
977
+ }
978
+
979
+ // src/mvt/readMvtNumber.js
980
+ function readMvtNumber(reader) {
981
+ const value = readMvtInteger(reader);
982
+ if (value > BigInt(Number.MAX_SAFE_INTEGER)) {
983
+ throw (
984
+ /* alloc */
985
+ new Error("MVT integer exceeds safe range")
986
+ );
987
+ }
988
+ return Number(value);
989
+ }
990
+
991
+ // src/mvt/readMvtField.js
992
+ function readMvtField(reader) {
993
+ const tag = readMvtNumber(reader);
994
+ const field = Math.floor(tag / 8), wire = tag % 8;
995
+ if (!field || field > 536870911) throw (
996
+ /* alloc */
997
+ new Error("Invalid MVT field number")
998
+ );
999
+ return (
1000
+ /* alloc */
1001
+ [field, wire]
1002
+ );
1003
+ }
1004
+
1005
+ // src/mvt/expectMvtWire.js
1006
+ function expectMvtWire(wire, expected) {
1007
+ if (wire !== expected) throw (
1008
+ /* alloc */
1009
+ new Error("Invalid MVT wire type")
1010
+ );
1011
+ }
1012
+
1013
+ // src/mvt/numericMvtValue.js
1014
+ function numericMvtValue(value) {
1015
+ return value >= BigInt(Number.MIN_SAFE_INTEGER) && value <= BigInt(Number.MAX_SAFE_INTEGER) ? Number(value) : value.toString();
1016
+ }
1017
+
1018
+ // src/mvt/finishMvtPath.js
1019
+ function finishMvtPath(path, type, closed) {
1020
+ if (path && (type === 2 && path.length < 2 || type === 3 && !closed)) {
1021
+ throw (
1022
+ /* alloc */
1023
+ new Error("Incomplete MVT geometry")
1024
+ );
1025
+ }
1026
+ }
1027
+
1028
+ // src/mvt/appendMvtCoordinates.js
1029
+ function appendMvtCoordinates(commands, state, count) {
1030
+ let i = 0, dx, dy;
1031
+ for (; i < count; i++) {
1032
+ dx = commands[state.index++];
1033
+ dy = commands[state.index++];
1034
+ if (dx > 4294967295 || dy > 4294967295) throw (
1035
+ /* alloc */
1036
+ new Error("Invalid MVT coordinate")
1037
+ );
1038
+ state.x += dx % 2 ? -(dx + 1) / 2 : dx / 2;
1039
+ state.y += dy % 2 ? -(dy + 1) / 2 : dy / 2;
1040
+ state.path.push(
1041
+ /* alloc */
1042
+ [state.x, state.y]
1043
+ );
1044
+ }
1045
+ }
1046
+
1047
+ // src/mvt/beginMvtPath.js
1048
+ function beginMvtPath(state, paths, type, count) {
1049
+ finishMvtPath(state.path, type, state.closed);
1050
+ if (type !== 1 && count !== 1) throw (
1051
+ /* alloc */
1052
+ new Error("Invalid MVT MoveTo")
1053
+ );
1054
+ state.path = /* alloc */
1055
+ [];
1056
+ paths.push(state.path);
1057
+ state.closed = false;
1058
+ }
1059
+
1060
+ // src/mvt/closeMvtPath.js
1061
+ function closeMvtPath(state, type, count) {
1062
+ if (type !== 3 || count !== 1 || !state.path || state.closed || state.path.length < 3) {
1063
+ throw (
1064
+ /* alloc */
1065
+ new Error("Invalid MVT ClosePath")
1066
+ );
1067
+ }
1068
+ state.path.push(
1069
+ /* alloc */
1070
+ [...state.path[0]]
1071
+ );
1072
+ state.closed = true;
1073
+ }
1074
+
1075
+ // src/mvt/readMvtGeometry.js
1076
+ function readMvtGeometry(commands, type) {
1077
+ const paths = (
1078
+ /* alloc */
1079
+ []
1080
+ );
1081
+ const state = (
1082
+ /* alloc */
1083
+ { x: 0, y: 0, path: null, closed: false, index: 0 }
1084
+ );
1085
+ let command, id, count;
1086
+ while (state.index < commands.length) {
1087
+ command = commands[state.index++];
1088
+ id = command % 8;
1089
+ count = Math.floor(command / 8);
1090
+ if (!count || command > 4294967295) throw (
1091
+ /* alloc */
1092
+ new Error("Invalid MVT geometry command")
1093
+ );
1094
+ if (id === 7) {
1095
+ closeMvtPath(state, type, count);
1096
+ continue;
1097
+ }
1098
+ if (id !== 1 && id !== 2 || count > Math.floor((commands.length - state.index) / 2)) {
1099
+ throw (
1100
+ /* alloc */
1101
+ new Error("Invalid MVT geometry parameters")
1102
+ );
1103
+ }
1104
+ if (id === 1) beginMvtPath(state, paths, type, count);
1105
+ else if (type === 1 || !state.path || state.closed) throw (
1106
+ /* alloc */
1107
+ new Error("Invalid MVT LineTo")
1108
+ );
1109
+ appendMvtCoordinates(commands, state, count);
1110
+ }
1111
+ finishMvtPath(state.path, type, state.closed);
1112
+ if (!paths.length) throw (
1113
+ /* alloc */
1114
+ new Error("Empty MVT geometry")
1115
+ );
1116
+ return paths;
1117
+ }
1118
+
1119
+ // src/mvt/readMvtMessage.js
1120
+ function readMvtMessage(reader, wire) {
1121
+ expectMvtWire(wire, 2);
1122
+ const size = readMvtNumber(reader), start = takeMvtBytes(reader, size);
1123
+ return createMvtReader(reader.bytes.subarray(start, start + size));
1124
+ }
1125
+
1126
+ // src/mvt/readMvtNumbers.js
1127
+ function readMvtNumbers(reader, wire, target) {
1128
+ let packed;
1129
+ if (wire === 0) target.push(readMvtNumber(reader));
1130
+ else {
1131
+ packed = readMvtMessage(reader, wire);
1132
+ while (packed.pos < packed.bytes.length) target.push(readMvtNumber(packed));
1133
+ }
1134
+ }
1135
+
1136
+ // src/mvt/skipMvtField.js
1137
+ function skipMvtField(reader, wire) {
1138
+ switch (wire) {
1139
+ case 0:
1140
+ readMvtInteger(reader);
1141
+ break;
1142
+ case 1:
1143
+ takeMvtBytes(reader, 8);
1144
+ break;
1145
+ case 2:
1146
+ takeMvtBytes(reader, readMvtNumber(reader));
1147
+ break;
1148
+ case 5:
1149
+ takeMvtBytes(reader, 4);
1150
+ break;
1151
+ default:
1152
+ throw (
1153
+ /* alloc */
1154
+ new Error("Unsupported MVT wire type")
1155
+ );
1156
+ }
1157
+ }
1158
+
1159
+ // src/mvt/readMvtFeature.js
1160
+ function readMvtFeature(reader, keys, values) {
1161
+ let id, type = 0, field, wire, i = 0;
1162
+ const tags = (
1163
+ /* alloc */
1164
+ []
1165
+ ), commands = (
1166
+ /* alloc */
1167
+ []
1168
+ );
1169
+ while (reader.pos < reader.bytes.length) {
1170
+ [field, wire] = readMvtField(reader);
1171
+ if (field === 1) {
1172
+ expectMvtWire(wire, 0);
1173
+ id = numericMvtValue(readMvtInteger(reader));
1174
+ } else if (field === 2) readMvtNumbers(reader, wire, tags);
1175
+ else if (field === 3) {
1176
+ expectMvtWire(wire, 0);
1177
+ type = readMvtNumber(reader);
1178
+ } else if (field === 4) readMvtNumbers(reader, wire, commands);
1179
+ else skipMvtField(reader, wire);
1180
+ }
1181
+ if (tags.length % 2 || type > 3) throw (
1182
+ /* alloc */
1183
+ new Error("Invalid MVT feature")
1184
+ );
1185
+ const properties = /* @__PURE__ */ Object.create(null);
1186
+ for (; i < tags.length; i += 2) {
1187
+ if (tags[i] >= keys.length || tags[i + 1] >= values.length) {
1188
+ throw (
1189
+ /* alloc */
1190
+ new Error("Invalid MVT property index")
1191
+ );
1192
+ }
1193
+ properties[keys[tags[i]]] = values[tags[i + 1]];
1194
+ }
1195
+ return (
1196
+ /* alloc */
1197
+ { id, type, properties, geometry: type ? readMvtGeometry(commands, type) : (
1198
+ /* alloc */
1199
+ []
1200
+ ) }
1201
+ );
1202
+ }
1203
+
1204
+ // src/globals/mvtTextDecoder.js
1205
+ var mvtTextDecoder = new TextDecoder("utf-8", { fatal: true });
1206
+
1207
+ // src/mvt/readMvtString.js
1208
+ function readMvtString(reader, wire) {
1209
+ return mvtTextDecoder.decode(readMvtMessage(reader, wire).bytes);
1210
+ }
1211
+
1212
+ // src/mvt/readMvtKnownValue.js
1213
+ function readMvtKnownValue(reader, field, wire) {
1214
+ let offset, integer;
1215
+ if (field === 1) return readMvtString(reader, wire);
1216
+ if (field === 2 || field === 3) {
1217
+ expectMvtWire(wire, field === 2 ? 5 : 1);
1218
+ offset = takeMvtBytes(reader, field === 2 ? 4 : 8);
1219
+ return field === 2 ? reader.view.getFloat32(offset, true) : reader.view.getFloat64(offset, true);
1220
+ }
1221
+ expectMvtWire(wire, 0);
1222
+ integer = readMvtInteger(reader);
1223
+ if (field === 4) return numericMvtValue(BigInt.asIntN(64, integer));
1224
+ if (field === 5) return numericMvtValue(integer);
1225
+ if (field === 6) return numericMvtValue(integer >> 1n ^ -(integer & 1n));
1226
+ return integer !== 0n;
1227
+ }
1228
+
1229
+ // src/mvt/readMvtValue.js
1230
+ function readMvtValue(reader) {
1231
+ let value, found = false, field, wire;
1232
+ while (reader.pos < reader.bytes.length) {
1233
+ [field, wire] = readMvtField(reader);
1234
+ if (field < 1 || field > 7) {
1235
+ skipMvtField(reader, wire);
1236
+ continue;
1237
+ }
1238
+ if (found) throw (
1239
+ /* alloc */
1240
+ new Error("Multiple MVT value types")
1241
+ );
1242
+ found = true;
1243
+ value = readMvtKnownValue(reader, field, wire);
1244
+ }
1245
+ if (!found) throw (
1246
+ /* alloc */
1247
+ new Error("Empty MVT value")
1248
+ );
1249
+ return value;
1250
+ }
1251
+
1252
+ // src/mvt/readMvtLayer.js
1253
+ function readMvtLayer(reader) {
1254
+ let name, version, extent = 4096, field, wire, value, i = 0;
1255
+ const keys = (
1256
+ /* alloc */
1257
+ []
1258
+ ), values = (
1259
+ /* alloc */
1260
+ []
1261
+ ), features = (
1262
+ /* alloc */
1263
+ []
1264
+ );
1265
+ while (reader.pos < reader.bytes.length) {
1266
+ [field, wire] = readMvtField(reader);
1267
+ if (field === 1) name = readMvtString(reader, wire);
1268
+ else if (field === 2) features.push(readMvtMessage(reader, wire));
1269
+ else if (field === 3) keys.push(readMvtString(reader, wire));
1270
+ else if (field === 4) values.push(readMvtValue(readMvtMessage(reader, wire)));
1271
+ else if (field === 5 || field === 15) {
1272
+ expectMvtWire(wire, 0);
1273
+ value = readMvtNumber(reader);
1274
+ if (field === 5) extent = value;
1275
+ else version = value;
1276
+ } else skipMvtField(reader, wire);
1277
+ }
1278
+ if (!name || !/* alloc */
1279
+ [1, 2].includes(version) || !extent || extent > 4294967295) {
1280
+ throw (
1281
+ /* alloc */
1282
+ new Error("Invalid MVT layer metadata")
1283
+ );
1284
+ }
1285
+ for (; i < features.length; i++) features[i] = readMvtFeature(features[i], keys, values);
1286
+ return (
1287
+ /* alloc */
1288
+ { name, extent, features }
1289
+ );
1290
+ }
1291
+
1292
+ // src/mvt/decodeVectorTile.js
1293
+ function decodeVectorTile(buffer) {
1294
+ if (!(buffer instanceof ArrayBuffer) && !(buffer instanceof Uint8Array)) {
1295
+ throw (
1296
+ /* alloc */
1297
+ new TypeError("MVT input must be an ArrayBuffer or Uint8Array")
1298
+ );
1299
+ }
1300
+ const bytes = buffer instanceof Uint8Array ? buffer : (
1301
+ /* alloc */
1302
+ new Uint8Array(buffer)
1303
+ );
1304
+ const reader = createMvtReader(bytes);
1305
+ const layers = (
1306
+ /* alloc */
1307
+ []
1308
+ ), names = (
1309
+ /* alloc */
1310
+ /* @__PURE__ */ new Set()
1311
+ );
1312
+ let field, wire, layer;
1313
+ while (reader.pos < reader.bytes.length) {
1314
+ [field, wire] = readMvtField(reader);
1315
+ if (field !== 3) {
1316
+ skipMvtField(reader, wire);
1317
+ continue;
1318
+ }
1319
+ layer = readMvtLayer(readMvtMessage(reader, wire));
1320
+ if (names.has(layer.name)) throw (
1321
+ /* alloc */
1322
+ new Error("Duplicate MVT layer name")
1323
+ );
1324
+ names.add(layer.name);
1325
+ layers.push(layer);
1326
+ }
1327
+ return layers;
1328
+ }
1329
+
1330
+ // src/vector-tiles/vectorStyleDependencies.js
1331
+ function vectorStyleDependencies(entry) {
1332
+ const dependencies = (
1333
+ /* alloc */
1334
+ /* @__PURE__ */ new Set()
1335
+ );
1336
+ const expressions = (
1337
+ /* alloc */
1338
+ [entry.paint, entry.filter]
1339
+ );
1340
+ let value, child;
1341
+ while (expressions.length) {
1342
+ value = expressions.pop();
1343
+ if (!value || typeof value !== "object") continue;
1344
+ if (Array.isArray(value) && typeof value[1] === "string" && /* alloc */
1345
+ ["get", "has", "==", "!=", "<", ">", "<=", ">=", "in", "!in"].includes(value[0])) {
1346
+ dependencies.add(value[1]);
1347
+ }
1348
+ for (child of Object.values(value)) expressions.push(child);
1349
+ }
1350
+ return dependencies;
1351
+ }
1352
+
1353
+ // src/vector-style/evaluateBoolean.js
1354
+ function evaluateBoolean(op, args, properties, zoom, legacy) {
1355
+ let i = 0;
1356
+ if (op === "all") {
1357
+ for (i = 0; i < args.length; i++) {
1358
+ if (!evaluateStyle(args[i], properties, zoom, legacy)) return false;
1359
+ }
1360
+ return true;
1361
+ }
1362
+ if (op === "any") {
1363
+ for (i = 0; i < args.length; i++) {
1364
+ if (evaluateStyle(args[i], properties, zoom, legacy)) return true;
1365
+ }
1366
+ return false;
1367
+ }
1368
+ throw (
1369
+ /* alloc */
1370
+ new Error(`Unsupported vector style expression: ${op}`)
1371
+ );
1372
+ }
1373
+
1374
+ // src/vector-style/evaluateCoalesceOrCase.js
1375
+ function evaluateCoalesceOrCase(op, args, properties, zoom, legacy) {
1376
+ let i = 0, result = null;
1377
+ if (op === "coalesce") {
1378
+ for (i = 0; i < args.length; i++) {
1379
+ result = evaluateStyle(args[i], properties, zoom);
1380
+ if (result !== null && result !== void 0) return result;
1381
+ }
1382
+ return null;
1383
+ }
1384
+ if (op === "case") {
1385
+ for (i = 0; i < args.length - 1; i += 2) {
1386
+ if (evaluateStyle(args[i], properties, zoom)) {
1387
+ return evaluateStyle(args[i + 1], properties, zoom);
1388
+ }
1389
+ }
1390
+ return evaluateStyle(args[args.length - 1], properties, zoom);
1391
+ }
1392
+ throw (
1393
+ /* alloc */
1394
+ new Error(`Unsupported vector style expression: ${op}`)
1395
+ );
1396
+ }
1397
+
1398
+ // src/vector-style/evaluateInput.js
1399
+ function evaluateInput(value, properties, zoom, legacy) {
1400
+ if (legacy && typeof value === "string") return properties[value];
1401
+ return evaluateStyle(value, properties, zoom);
1402
+ }
1403
+
1404
+ // src/vector-style/evaluateComparison.js
1405
+ function evaluateComparison(op, args, properties, zoom, legacy) {
1406
+ const left = evaluateInput(args[0], properties, zoom, legacy);
1407
+ const right = evaluateStyle(args[1], properties, zoom);
1408
+ switch (op) {
1409
+ case "==":
1410
+ return left === right;
1411
+ case "!=":
1412
+ return left !== right;
1413
+ case "<=":
1414
+ return left <= right;
1415
+ case ">=":
1416
+ return left >= right;
1417
+ case "<":
1418
+ return left < right;
1419
+ case ">":
1420
+ return left > right;
1421
+ default:
1422
+ throw (
1423
+ /* alloc */
1424
+ new Error(`Unsupported vector style expression: ${op}`)
1425
+ );
1426
+ }
1427
+ }
1428
+
1429
+ // src/globals/vectorStyleNamedColors.js
1430
+ var vectorStyleNamedColors = {
1431
+ black: [0, 0, 0, 1],
1432
+ white: [255, 255, 255, 1],
1433
+ gray: [128, 128, 128, 1],
1434
+ blue: [0, 0, 255, 1],
1435
+ red: [255, 0, 0, 1],
1436
+ green: [0, 128, 0, 1]
1437
+ };
1438
+
1439
+ // src/vector-style/parseFunctionalColor.js
1440
+ function parseFunctionalColor(value) {
1441
+ const match = /^rgba?\(([^)]+)\)$/.exec(value);
1442
+ if (match) {
1443
+ const values = match[1].split(",");
1444
+ let i = 0, finite = true;
1445
+ for (i = 0; i < values.length; i++) values[i] = Number(values[i]);
1446
+ if (values.length === 3) values.push(1);
1447
+ for (i = 0; i < values.length; i++) {
1448
+ if (!Number.isFinite(values[i])) finite = false;
1449
+ }
1450
+ if (values.length === 4 && finite) return values;
1451
+ }
1452
+ throw (
1453
+ /* alloc */
1454
+ new Error(`Unsupported interpolated style color: ${value}`)
1455
+ );
1456
+ }
1457
+
1458
+ // src/vector-style/parseHexColor.js
1459
+ function parseHexColor(value) {
1460
+ let i = 0, offset = 0;
1461
+ if (value.length === 4) {
1462
+ value = "#" + value[1] + value[1] + value[2] + value[2] + value[3] + value[3];
1463
+ }
1464
+ const color = (
1465
+ /* alloc */
1466
+ []
1467
+ );
1468
+ for (i = 1; i < 6; i += 2) {
1469
+ offset = parseInt(value.slice(i, i + 2), 16);
1470
+ color.push(offset);
1471
+ }
1472
+ color.push(1);
1473
+ return color;
1474
+ }
1475
+
1476
+ // src/vector-style/styleColor.js
1477
+ function styleColor(value) {
1478
+ if (Object.hasOwn(vectorStyleNamedColors, value)) return vectorStyleNamedColors[value];
1479
+ if (/^#[0-9a-f]{3}$/i.test(value) || /^#[0-9a-f]{6}$/i.test(value)) {
1480
+ return parseHexColor(value);
1481
+ }
1482
+ return parseFunctionalColor(value);
1483
+ }
1484
+
1485
+ // src/vector-style/evaluateInterpolate.js
1486
+ function evaluateInterpolate(args, properties, zoom) {
1487
+ const result = evaluateStyle(args[1], properties, zoom);
1488
+ const base = args[0][0] === "exponential" ? args[0][1] : 1;
1489
+ let i = 0, lower = 0, upper = 0, ratio = 0, left = null, right = null, j = 0, color = "";
1490
+ if (result <= args[2]) return evaluateStyle(args[3], properties, zoom);
1491
+ for (i = 4; i < args.length; i += 2) {
1492
+ if (result > args[i]) continue;
1493
+ lower = args[i - 2];
1494
+ upper = args[i];
1495
+ ratio = base === 1 ? (result - lower) / (upper - lower) : (base ** (result - lower) - 1) / (base ** (upper - lower) - 1);
1496
+ left = evaluateStyle(args[i - 1], properties, zoom);
1497
+ right = evaluateStyle(args[i + 1], properties, zoom);
1498
+ if (typeof left === "number" && typeof right === "number") return left + (right - left) * ratio;
1499
+ left = styleColor(left);
1500
+ right = styleColor(right);
1501
+ for (j = 0; j < 3; j++) {
1502
+ if (j) color += ",";
1503
+ color += Math.round(left[j] + (right[j] - left[j]) * ratio);
1504
+ }
1505
+ return `rgba(${color},${left[3] + (right[3] - left[3]) * ratio})`;
1506
+ }
1507
+ return evaluateStyle(args.at(-1), properties, zoom);
1508
+ }
1509
+
1510
+ // src/vector-style/arrayContainsRange.js
1511
+ function arrayContainsRange(values, start, end, target) {
1512
+ let i = 0, value = null;
1513
+ for (i = start; i < end; i++) {
1514
+ value = values[i];
1515
+ if (value === target || value !== value && target !== target) return true;
1516
+ }
1517
+ return false;
1518
+ }
1519
+
1520
+ // src/vector-style/evaluateMembership.js
1521
+ function evaluateMembership(op, args, properties, zoom, legacy) {
1522
+ let target = null;
1523
+ if (op === "!in") {
1524
+ target = evaluateInput(args[0], properties, zoom, legacy);
1525
+ return !arrayContainsRange(args, 1, args.length, target);
1526
+ }
1527
+ if (legacy) {
1528
+ target = evaluateInput(args[0], properties, zoom, true);
1529
+ return arrayContainsRange(args, 1, args.length, target);
1530
+ }
1531
+ const values = evaluateStyle(args[1], properties, zoom);
1532
+ target = evaluateInput(args[0], properties, zoom, false);
1533
+ return values.includes(target);
1534
+ }
1535
+
1536
+ // src/vector-style/evaluateIExpression.js
1537
+ function evaluateIExpression(op, args, properties, zoom, legacy) {
1538
+ if (op === "in") return evaluateMembership(op, args, properties, zoom, legacy);
1539
+ if (op === "interpolate") return evaluateInterpolate(args, properties, zoom);
1540
+ throw (
1541
+ /* alloc */
1542
+ new Error(`Unsupported vector style expression: ${op}`)
1543
+ );
1544
+ }
1545
+
1546
+ // src/vector-style/evaluateMath.js
1547
+ function evaluateMath(op, args, properties, zoom) {
1548
+ let i = 0, sum = 0;
1549
+ if (op === "+") {
1550
+ for (i = 0; i < args.length; i++) sum += evaluateStyle(args[i], properties, zoom);
1551
+ return sum;
1552
+ }
1553
+ if (op === "-") {
1554
+ if (args.length === 1) return -evaluateStyle(args[0], properties, zoom);
1555
+ return evaluateStyle(args[0], properties, zoom) - evaluateStyle(args[1], properties, zoom);
1556
+ }
1557
+ throw (
1558
+ /* alloc */
1559
+ new Error(`Unsupported vector style expression: ${op}`)
1560
+ );
1561
+ }
1562
+
1563
+ // src/vector-style/evaluateMatch.js
1564
+ function evaluateMatch(args, properties, zoom) {
1565
+ const result = evaluateStyle(args[0], properties, zoom);
1566
+ let i = 0, matched = false;
1567
+ for (i = 1; i < args.length - 1; i += 2) {
1568
+ if (Array.isArray(args[i])) matched = args[i].includes(result);
1569
+ else matched = args[i] === result;
1570
+ if (matched) return evaluateStyle(args[i + 1], properties, zoom);
1571
+ }
1572
+ return evaluateStyle(args[args.length - 1], properties, zoom);
1573
+ }
1574
+
1575
+ // src/vector-style/evaluateNegation.js
1576
+ function evaluateNegation(op, args, properties, zoom, legacy) {
1577
+ if (op === "!") return !evaluateStyle(args[0], properties, zoom, legacy);
1578
+ if (op === "!=") {
1579
+ return evaluateInput(args[0], properties, zoom, legacy) !== evaluateStyle(args[1], properties, zoom);
1580
+ }
1581
+ if (op === "!in") return evaluateMembership(op, args, properties, zoom, legacy);
1582
+ throw (
1583
+ /* alloc */
1584
+ new Error(`Unsupported vector style expression: ${op}`)
1585
+ );
1586
+ }
1587
+
1588
+ // src/vector-style/evaluatePrimitive.js
1589
+ function evaluatePrimitive(op, args, properties, zoom) {
1590
+ switch (op) {
1591
+ case "literal":
1592
+ return args[0];
1593
+ case "get":
1594
+ return properties[args[0]];
1595
+ case "has":
1596
+ return Object.hasOwn(properties, args[0]);
1597
+ case "zoom":
1598
+ return zoom;
1599
+ default:
1600
+ throw (
1601
+ /* alloc */
1602
+ new Error(`Unsupported vector style expression: ${op}`)
1603
+ );
1604
+ }
1605
+ }
1606
+
1607
+ // src/vector-style/evaluateStep.js
1608
+ function evaluateStep(args, properties, zoom) {
1609
+ const result = evaluateStyle(args[0], properties, zoom);
1610
+ let output = args[1], i = 0;
1611
+ for (i = 2; i < args.length; i += 2) {
1612
+ if (result < args[i]) break;
1613
+ output = args[i + 1];
1614
+ }
1615
+ return evaluateStyle(output, properties, zoom);
1616
+ }
1617
+
1618
+ // src/vector-style/evaluateToExpression.js
1619
+ function evaluateToExpression(op, args, properties, zoom) {
1620
+ if (op === "to-boolean") return Boolean(evaluateStyle(args[0], properties, zoom));
1621
+ if (op === "to-number") {
1622
+ let i = 0, number = 0;
1623
+ for (i = 0; i < args.length; i++) {
1624
+ number = Number(evaluateStyle(args[i], properties, zoom));
1625
+ if (Number.isFinite(number)) return number;
1626
+ }
1627
+ throw (
1628
+ /* alloc */
1629
+ new Error("Invalid numeric style expression")
1630
+ );
1631
+ }
1632
+ throw (
1633
+ /* alloc */
1634
+ new Error(`Unsupported vector style expression: ${op}`)
1635
+ );
1636
+ }
1637
+
1638
+ // src/vector-style/evaluateStyle.js
1639
+ function evaluateStyle(value, properties, zoom, legacy = false) {
1640
+ if (!Array.isArray(value)) return value;
1641
+ const op = value[0];
1642
+ const args = value.slice(1);
1643
+ const operation = String(op);
1644
+ if (/^(?:literal|get|has|zoom)$/.test(operation)) {
1645
+ return evaluatePrimitive(op, args, properties, zoom);
1646
+ }
1647
+ switch (operation[0]) {
1648
+ case "a":
1649
+ return evaluateBoolean(op, args, properties, zoom, legacy);
1650
+ case "!":
1651
+ return evaluateNegation(op, args, properties, zoom, legacy);
1652
+ case "=":
1653
+ case "<":
1654
+ case ">":
1655
+ return evaluateComparison(op, args, properties, zoom, legacy);
1656
+ case "i":
1657
+ return evaluateIExpression(op, args, properties, zoom, legacy);
1658
+ case "t":
1659
+ return evaluateToExpression(op, args, properties, zoom, legacy);
1660
+ case "c":
1661
+ return evaluateCoalesceOrCase(op, args, properties, zoom, legacy);
1662
+ case "m":
1663
+ return evaluateMatch(args, properties, zoom);
1664
+ case "s":
1665
+ return evaluateStep(args, properties, zoom);
1666
+ default:
1667
+ return evaluateMath(op, args, properties, zoom);
1668
+ }
1669
+ }
1670
+
1671
+ // src/vector-style/visibleStyle.js
1672
+ function visibleStyle(layer, properties, zoom) {
1673
+ return zoom >= (layer.minzoom ?? 0) && zoom < (layer.maxzoom ?? Infinity) && layer.layout?.visibility !== "none" && (!layer.filter || evaluateStyle(layer.filter, properties, zoom, true));
1674
+ }
1675
+
1676
+ // src/vector-tiles/vectorFeatureZoom.js
1677
+ function vectorFeatureZoom(entry, properties, zoom, maxZoom) {
1678
+ if (visibleStyle(entry, properties, zoom)) return zoom;
1679
+ zoom = maxZoom;
1680
+ while (zoom >= 0 && !visibleStyle(entry, properties, zoom)) zoom--;
1681
+ return zoom;
1682
+ }
1683
+
1684
+ // src/vector-tiles/projectVectorPoint.js
1685
+ function projectVectorPoint(point, coords, extent) {
1686
+ const size = 2 ** coords.z;
1687
+ const x = (coords.x + point[0] / extent) / size;
1688
+ const y = (coords.y + point[1] / extent) / size;
1689
+ return (
1690
+ /* alloc */
1691
+ [Math.atan(Math.sinh(Math.PI * (1 - 2 * y))) * 180 / Math.PI, x * 360 - 180]
1692
+ );
1693
+ }
1694
+
1695
+ // src/vector-tiles/vectorLabelProperty.js
1696
+ function vectorLabelProperty(properties, match, key) {
1697
+ return properties[key] ?? "";
1698
+ }
1699
+
1700
+ // src/vector-tiles/vectorStyleValue.js
1701
+ function vectorStyleValue(object, key, fallback, properties, zoom) {
1702
+ return object?.[key] === void 0 ? fallback : evaluateStyle(object[key], properties, zoom);
1703
+ }
1704
+
1705
+ // src/vector-tiles/vectorStraightRun.js
1706
+ function vectorStraightRun(path, i, point, step) {
1707
+ const base = Math.atan2(path[i][1] - path[i - 1][1], path[i][0] - path[i - 1][0]);
1708
+ let j = step > 0 ? i : i - 1, previous = point, run = 0, next;
1709
+ while (j >= 0 && j < path.length) {
1710
+ run += Math.hypot(path[j][0] - previous[0], path[j][1] - previous[1]);
1711
+ previous = path[j];
1712
+ next = j + step;
1713
+ if (next < 0 || next >= path.length) break;
1714
+ if (Math.cos(Math.atan2(path[next][1] - path[j][1], path[next][0] - path[j][0]) - base + (step < 0 ? Math.PI : 0)) < 0.7) break;
1715
+ j = next;
1716
+ }
1717
+ return run;
1718
+ }
1719
+
1720
+ // src/vector-tiles/vectorLabelAnchors.js
1721
+ function vectorLabelAnchors(path, spacing) {
1722
+ const anchors = (
1723
+ /* alloc */
1724
+ []
1725
+ );
1726
+ let length = 0, i, walked, target, count, k, segment, t, point;
1727
+ for (i = 1; i < path.length; i++) length += Math.hypot(path[i][0] - path[i - 1][0], path[i][1] - path[i - 1][1]);
1728
+ count = Math.max(1, Math.floor(length / spacing));
1729
+ i = 1;
1730
+ walked = 0;
1731
+ for (k = 0; k < count; k++) {
1732
+ target = (k + 0.5) * length / count;
1733
+ while (i < path.length - 1 && walked + Math.hypot(path[i][0] - path[i - 1][0], path[i][1] - path[i - 1][1]) < target) {
1734
+ walked += Math.hypot(path[i][0] - path[i - 1][0], path[i][1] - path[i - 1][1]);
1735
+ i++;
1736
+ }
1737
+ segment = Math.hypot(path[i][0] - path[i - 1][0], path[i][1] - path[i - 1][1]);
1738
+ t = segment ? Math.min(1, Math.max(0, (target - walked) / segment)) : 0;
1739
+ point = /* alloc */
1740
+ [
1741
+ path[i - 1][0] + (path[i][0] - path[i - 1][0]) * t,
1742
+ path[i - 1][1] + (path[i][1] - path[i - 1][1]) * t
1743
+ ];
1744
+ anchors.push(
1745
+ /* alloc */
1746
+ {
1747
+ point,
1748
+ a: path[i - 1],
1749
+ b: path[i],
1750
+ length: vectorStraightRun(path, i, point, 1) + vectorStraightRun(path, i, point, -1)
1751
+ }
1752
+ );
1753
+ }
1754
+ return anchors;
1755
+ }
1756
+
1757
+ // src/vector-tiles/prepareVectorLabel.js
1758
+ function prepareVectorLabel(job, layer, entry, feature, order) {
1759
+ let text = entry.layout?.["text-field"];
1760
+ const { properties } = feature, { zoom } = job;
1761
+ const icon = entry.layout?.["symbol-placement"] === "line" ? null : evaluateStyle(entry.layout?.["icon-image"], properties, zoom);
1762
+ if (Array.isArray(text)) text = evaluateStyle(text, properties, zoom);
1763
+ if (typeof text !== "string") text = "";
1764
+ text = text.replace(/\{([^}]+)\}/g, vectorLabelProperty.bind(null, properties));
1765
+ if (!text.trim() && typeof icon !== "string") return;
1766
+ if (entry.layout?.["text-transform"] === "uppercase") text = text.toUpperCase();
1767
+ const size = vectorStyleValue(entry.layout, "text-size", 12, properties, zoom), line = feature.type === 2 && text.trim();
1768
+ const spacing = layer.extent / 16, anchors = (
1769
+ /* alloc */
1770
+ []
1771
+ );
1772
+ let path, anchor;
1773
+ for (path of line ? feature.geometry : feature.geometry.slice(0, 1)) {
1774
+ if (!path?.length) continue;
1775
+ if (!line) anchors.push(
1776
+ /* alloc */
1777
+ { point: path[Math.floor(path.length / 2)], a: null }
1778
+ );
1779
+ else for (anchor of vectorLabelAnchors(path, spacing)) anchors.push(anchor);
1780
+ }
1781
+ for (anchor of anchors) {
1782
+ if (anchor.point[0] < 0 || anchor.point[0] >= layer.extent || anchor.point[1] < 0 || anchor.point[1] >= layer.extent) continue;
1783
+ job.labels.push(
1784
+ /* alloc */
1785
+ {
1786
+ text,
1787
+ latlng: projectVectorPoint(anchor.point, job.coords, layer.extent),
1788
+ order,
1789
+ entry,
1790
+ properties,
1791
+ size,
1792
+ pixelBase: anchor.length * 256 / layer.extent / 2 ** job.coords.z,
1793
+ along: anchor.a ? (
1794
+ /* alloc */
1795
+ [
1796
+ projectVectorPoint(anchor.a, job.coords, layer.extent),
1797
+ projectVectorPoint(anchor.b, job.coords, layer.extent)
1798
+ ]
1799
+ ) : null,
1800
+ icon: text.trim() ? null : icon,
1801
+ iconOffset: evaluateStyle(entry.layout?.["icon-offset"], properties, zoom) || /* alloc */
1802
+ [0, 0],
1803
+ allowOverlap: entry.layout?.["icon-allow-overlap"] === true,
1804
+ color: vectorStyleValue(entry.paint, "text-color", "#000", properties, zoom),
1805
+ halo: vectorStyleValue(entry.paint, "text-halo-color", "#fff", properties, zoom),
1806
+ haloWidth: vectorStyleValue(entry.paint, "text-halo-width", 0, properties, zoom),
1807
+ priority: (
1808
+ /* alloc */
1809
+ ["place_labels", "place"].includes(entry["source-layer"]) ? 0 : (
1810
+ /* alloc */
1811
+ ["street_labels", "transportation_name"].includes(entry["source-layer"]) ? 1 : 2
1812
+ )
1813
+ )
1814
+ }
1815
+ );
1816
+ }
1817
+ }
1818
+
1819
+ // src/vector-style/readPaintValue.js
1820
+ function readPaintValue(paint, key, fallback, properties, zoom) {
1821
+ if (paint[key] === void 0) return fallback;
1822
+ return evaluateStyle(paint[key], properties, zoom);
1823
+ }
1824
+
1825
+ // src/vector-style/fillVectorPaint.js
1826
+ function fillVectorPaint(paint, properties, zoom) {
1827
+ const fill = styleColor(readPaintValue(paint, "fill-color", "#000", properties, zoom));
1828
+ const outline = readPaintValue(paint, "fill-outline-color", null, properties, zoom);
1829
+ const rgb = outline === null ? null : styleColor(outline);
1830
+ const fillOpacity = readPaintValue(paint, "fill-opacity", 1, properties, zoom) * fill[3];
1831
+ const fillColor = (
1832
+ /* alloc */
1833
+ { r: fill[0] / 255, g: fill[1] / 255, b: fill[2] / 255 }
1834
+ );
1835
+ const color = rgb ? (
1836
+ /* alloc */
1837
+ { r: rgb[0] / 255, g: rgb[1] / 255, b: rgb[2] / 255 }
1838
+ ) : null;
1839
+ return (
1840
+ /* alloc */
1841
+ {
1842
+ fillColor,
1843
+ fillOpacity,
1844
+ color,
1845
+ weight: 0.5
1846
+ }
1847
+ );
1848
+ }
1849
+
1850
+ // src/vector-style/scaleDashArray.js
1851
+ function scaleDashArray(dash, weight, properties, zoom) {
1852
+ if (!dash) return void 0;
1853
+ let values = dash, i = 0, result = "";
1854
+ if (typeof dash[0] !== "number") values = evaluateStyle(dash, properties, zoom);
1855
+ if (!Array.isArray(values)) throw (
1856
+ /* alloc */
1857
+ new TypeError("Style dash array must be an array")
1858
+ );
1859
+ for (i = 0; i < values.length; i++) {
1860
+ if (i) result += " ";
1861
+ result += values[i] * weight;
1862
+ }
1863
+ return result;
1864
+ }
1865
+
1866
+ // src/vector-style/lineVectorPaint.js
1867
+ function lineVectorPaint(layer, paint, properties, zoom) {
1868
+ const weight = readPaintValue(paint, "line-width", 1, properties, zoom);
1869
+ const rgb = styleColor(readPaintValue(paint, "line-color", "#000", properties, zoom));
1870
+ const opacity = readPaintValue(paint, "line-opacity", 1, properties, zoom) * rgb[3];
1871
+ const dashArray = scaleDashArray(paint["line-dasharray"], weight, properties, zoom);
1872
+ const lineCap = layer.layout?.["line-cap"] || "butt";
1873
+ const lineJoin = layer.layout?.["line-join"] || "miter";
1874
+ const color = (
1875
+ /* alloc */
1876
+ { r: rgb[0] / 255, g: rgb[1] / 255, b: rgb[2] / 255 }
1877
+ );
1878
+ return (
1879
+ /* alloc */
1880
+ {
1881
+ color,
1882
+ opacity,
1883
+ weight,
1884
+ dashArray,
1885
+ lineCap,
1886
+ lineJoin
1887
+ }
1888
+ );
1889
+ }
1890
+
1891
+ // src/vector-style/vectorPaint.js
1892
+ function vectorPaint(layer, properties, zoom) {
1893
+ const paint = layer.paint || /* alloc */
1894
+ {};
1895
+ if (layer.type === "fill") return fillVectorPaint(paint, properties, zoom);
1896
+ return lineVectorPaint(layer, paint, properties, zoom);
1897
+ }
1898
+
1899
+ // src/vector-tiles/vectorFeaturePaint.js
1900
+ function vectorFeaturePaint(entry, feature, sampleZoom, maxZoom) {
1901
+ if (entry.type === "fill") return feature.type === 3 ? vectorPaint(entry, feature.properties, sampleZoom) : null;
1902
+ if (entry.type !== "line" || feature.type !== 2) return null;
1903
+ let paint = vectorPaint(entry, feature.properties, sampleZoom), candidate;
1904
+ if (paint.weight > 0 && paint.opacity > 0) return paint;
1905
+ for (candidate = maxZoom; candidate >= 0; candidate--) {
1906
+ if (!visibleStyle(entry, feature.properties, candidate)) continue;
1907
+ paint = vectorPaint(entry, feature.properties, candidate);
1908
+ if (paint.weight > 0 && paint.opacity > 0) return paint;
1909
+ }
1910
+ return null;
1911
+ }
1912
+
1913
+ // src/mvt/mvtEdgeX.js
1914
+ function mvtEdgeX(edge, y) {
1915
+ return edge[0][0] + (y - edge[0][1]) * (edge[1][0] - edge[0][0]) / (edge[1][1] - edge[0][1]);
1916
+ }
1917
+
1918
+ // src/mvt/addMvtCrossings.js
1919
+ function addMvtCrossings(edges, levels) {
1920
+ let i = 0, j, a, b, top, bottom, start, end;
1921
+ for (; i < edges.length; i++) {
1922
+ a = edges[i];
1923
+ for (j = i + 1; j < edges.length; j++) {
1924
+ b = edges[j];
1925
+ top = Math.max(a[0][1], b[0][1]);
1926
+ bottom = Math.min(a[1][1], b[1][1]);
1927
+ if (top >= bottom) continue;
1928
+ start = mvtEdgeX(a, top) - mvtEdgeX(b, top);
1929
+ end = mvtEdgeX(a, bottom) - mvtEdgeX(b, bottom);
1930
+ if (start * end < 0) levels.add(top + (bottom - top) * start / (start - end));
1931
+ }
1932
+ }
1933
+ }
1934
+
1935
+ // src/mvt/mergeMvtActiveEdges.js
1936
+ function mergeMvtActiveEdges(active, sorted, left, mid, right) {
1937
+ let i = left, j = mid, k = left;
1938
+ while (i < mid && j < right) {
1939
+ if (active[j].x < active[i].x) sorted[k++] = active[j++];
1940
+ else sorted[k++] = active[i++];
1941
+ }
1942
+ while (i < mid) sorted[k++] = active[i++];
1943
+ while (j < right) sorted[k++] = active[j++];
1944
+ }
1945
+
1946
+ // src/mvt/sortMvtActiveEdges.js
1947
+ function sortMvtActiveEdges(active) {
1948
+ const sorted = (
1949
+ /* alloc */
1950
+ new Array(active.length)
1951
+ );
1952
+ let width = 1, left, mid, right, i;
1953
+ while (width < active.length) {
1954
+ for (left = 0; left < active.length; left += width * 2) {
1955
+ mid = Math.min(left + width, active.length);
1956
+ right = Math.min(left + width * 2, active.length);
1957
+ mergeMvtActiveEdges(active, sorted, left, mid, right);
1958
+ }
1959
+ for (i = 0; i < active.length; i++) active[i] = sorted[i];
1960
+ width *= 2;
1961
+ }
1962
+ }
1963
+
1964
+ // src/mvt/appendMvtBandTriangles.js
1965
+ function appendMvtBandTriangles(edges, top, bottom, triangles) {
1966
+ const mid = (top + bottom) / 2, active = (
1967
+ /* alloc */
1968
+ []
1969
+ );
1970
+ let edge, i = 0, leftTop, leftBottom, rightTop, rightBottom;
1971
+ for (edge of edges) {
1972
+ if (edge[0][1] < mid && edge[1][1] > mid) {
1973
+ active.push(
1974
+ /* alloc */
1975
+ { edge, x: mvtEdgeX(edge, mid) }
1976
+ );
1977
+ }
1978
+ }
1979
+ sortMvtActiveEdges(active);
1980
+ if (active.length % 2) throw (
1981
+ /* alloc */
1982
+ new Error("Unbalanced MVT polygon edges")
1983
+ );
1984
+ for (; i < active.length; i += 2) {
1985
+ leftTop = mvtEdgeX(active[i].edge, top);
1986
+ leftBottom = mvtEdgeX(active[i].edge, bottom);
1987
+ rightTop = mvtEdgeX(active[i + 1].edge, top);
1988
+ rightBottom = mvtEdgeX(active[i + 1].edge, bottom);
1989
+ if (rightTop < leftTop - 1e-8 || rightBottom < leftBottom - 1e-8) {
1990
+ throw (
1991
+ /* alloc */
1992
+ new Error("Crossing MVT polygon edges")
1993
+ );
1994
+ }
1995
+ if ((rightTop - leftTop) * (bottom - top) > 1e-6) {
1996
+ triangles.push(
1997
+ /* alloc */
1998
+ [
1999
+ /* alloc */
2000
+ [leftTop, top],
2001
+ /* alloc */
2002
+ [rightTop, top],
2003
+ /* alloc */
2004
+ [leftBottom, bottom]
2005
+ ]
2006
+ );
2007
+ }
2008
+ if ((rightBottom - leftBottom) * (bottom - top) > 1e-6) {
2009
+ triangles.push(
2010
+ /* alloc */
2011
+ [
2012
+ /* alloc */
2013
+ [rightTop, top],
2014
+ /* alloc */
2015
+ [rightBottom, bottom],
2016
+ /* alloc */
2017
+ [leftBottom, bottom]
2018
+ ]
2019
+ );
2020
+ }
2021
+ }
2022
+ }
2023
+
2024
+ // src/mvt/finiteMvtCoordinate.js
2025
+ function finiteMvtCoordinate(point) {
2026
+ let i = 0;
2027
+ for (; i < point.length; i++) {
2028
+ if (!Number.isFinite(point[i])) return false;
2029
+ }
2030
+ return true;
2031
+ }
2032
+
2033
+ // src/mvt/collectMvtEdges.js
2034
+ function collectMvtEdges(rings, edges, levels) {
2035
+ let ring3, i, a, b;
2036
+ for (ring3 of rings) {
2037
+ if (ring3.length < 4 || ring3[0][0] !== ring3.at(-1)[0] || ring3[0][1] !== ring3.at(-1)[1]) {
2038
+ throw (
2039
+ /* alloc */
2040
+ new Error("MVT tessellation requires closed rings")
2041
+ );
2042
+ }
2043
+ for (i = 1; i < ring3.length; i++) {
2044
+ a = ring3[i - 1];
2045
+ b = ring3[i];
2046
+ if (!finiteMvtCoordinate(a) || !finiteMvtCoordinate(b)) {
2047
+ throw (
2048
+ /* alloc */
2049
+ new Error("Invalid MVT ring coordinate")
2050
+ );
2051
+ }
2052
+ levels.add(a[1]);
2053
+ if (a[1] !== b[1]) edges.push(
2054
+ /* alloc */
2055
+ [a[1] < b[1] ? a : b, a[1] < b[1] ? b : a]
2056
+ );
2057
+ }
2058
+ }
2059
+ }
2060
+
2061
+ // src/mvt/sortedMvtLevels.js
2062
+ function sortedMvtLevels(levels) {
2063
+ const sorted = (
2064
+ /* alloc */
2065
+ []
2066
+ );
2067
+ let y, gap = 0, i = 0, j, value, count = 0;
2068
+ for (y of levels) sorted.push(y);
2069
+ gap = Math.floor(sorted.length / 2);
2070
+ for (; gap > 0; gap = Math.floor(gap / 2)) {
2071
+ for (i = gap; i < sorted.length; i++) {
2072
+ value = sorted[i];
2073
+ j = i;
2074
+ while (j >= gap && sorted[j - gap] > value) {
2075
+ sorted[j] = sorted[j - gap];
2076
+ j -= gap;
2077
+ }
2078
+ sorted[j] = value;
2079
+ }
2080
+ }
2081
+ for (i = 0; i < sorted.length; i++) {
2082
+ if (!count || sorted[i] - sorted[count - 1] > 1e-8) sorted[count++] = sorted[i];
2083
+ }
2084
+ sorted.length = count;
2085
+ return sorted;
2086
+ }
2087
+
2088
+ // src/mvt/tessellateVectorRings.js
2089
+ function tessellateVectorRings(rings) {
2090
+ const edges = (
2091
+ /* alloc */
2092
+ []
2093
+ ), levels = (
2094
+ /* alloc */
2095
+ /* @__PURE__ */ new Set()
2096
+ ), triangles = (
2097
+ /* alloc */
2098
+ []
2099
+ );
2100
+ let sorted, i = 1, top, bottom;
2101
+ collectMvtEdges(rings, edges, levels);
2102
+ addMvtCrossings(edges, levels);
2103
+ sorted = sortedMvtLevels(levels);
2104
+ for (; i < sorted.length; i++) {
2105
+ top = sorted[i - 1];
2106
+ bottom = sorted[i];
2107
+ if (bottom > top) appendMvtBandTriangles(edges, top, bottom, triangles);
2108
+ }
2109
+ return triangles;
2110
+ }
2111
+
2112
+ // src/mvt/isMvtFallbackError.js
2113
+ function isMvtFallbackError(error) {
2114
+ return error.message === "Polygon triangulation area mismatch" || error.message === "Degenerate polygon ring" || error.message === "Unable to triangulate polygon: invalid or self-intersecting rings" || error.message === "Unable to connect polygon hole";
2115
+ }
2116
+
2117
+ // src/mvt/appendMvtPolygonTriangles.js
2118
+ function appendMvtPolygonTriangles(rings, paths, vertices, triangles) {
2119
+ let indices, index, triangle, points, i = 0;
2120
+ try {
2121
+ indices = triangulate(paths[0], paths.slice(1));
2122
+ for (triangle of indices) {
2123
+ points = /* alloc */
2124
+ [];
2125
+ for (index of triangle) points.push(
2126
+ /* alloc */
2127
+ [vertices[index].x, vertices[index].y]
2128
+ );
2129
+ triangles.push(points);
2130
+ }
2131
+ } catch (error) {
2132
+ if (!isMvtFallbackError(error)) throw error;
2133
+ indices = tessellateVectorRings(rings);
2134
+ for (; i < indices.length; i++) triangles.push(indices[i]);
2135
+ }
2136
+ }
2137
+
2138
+ // src/mvt/groupMvtPolygons.js
2139
+ function groupMvtPolygons(rings) {
2140
+ const polygons = (
2141
+ /* alloc */
2142
+ []
2143
+ );
2144
+ let polygon, exteriorSign, ring3, area2, i;
2145
+ for (ring3 of rings) {
2146
+ if (ring3.length < 4 || ring3[0][0] !== ring3.at(-1)[0] || ring3[0][1] !== ring3.at(-1)[1]) {
2147
+ throw (
2148
+ /* alloc */
2149
+ new Error("MVT tessellation requires closed rings")
2150
+ );
2151
+ }
2152
+ area2 = 0;
2153
+ for (i = 1; i < ring3.length; i++) {
2154
+ if (!finiteMvtCoordinate(ring3[i]) || !finiteMvtCoordinate(ring3[i - 1])) {
2155
+ throw (
2156
+ /* alloc */
2157
+ new Error("Invalid MVT ring coordinate")
2158
+ );
2159
+ }
2160
+ area2 += ring3[i - 1][0] * ring3[i][1] - ring3[i][0] * ring3[i - 1][1];
2161
+ }
2162
+ if (!polygon || area2 && Math.sign(area2) === exteriorSign) {
2163
+ polygon = /* alloc */
2164
+ [];
2165
+ polygons.push(polygon);
2166
+ exteriorSign = Math.sign(area2);
2167
+ }
2168
+ polygon.push(ring3);
2169
+ }
2170
+ return polygons;
2171
+ }
2172
+
2173
+ // src/mvt/mvtPathsVertices.js
2174
+ function mvtPathsVertices(paths) {
2175
+ const vertices = (
2176
+ /* alloc */
2177
+ []
2178
+ );
2179
+ let path, point;
2180
+ for (path of paths) {
2181
+ for (point of path) vertices.push(point);
2182
+ }
2183
+ return vertices;
2184
+ }
2185
+
2186
+ // src/mvt/mvtPolygonPaths.js
2187
+ function mvtPolygonPaths(rings) {
2188
+ const paths = (
2189
+ /* alloc */
2190
+ []
2191
+ );
2192
+ let ring3, i, path;
2193
+ for (ring3 of rings) {
2194
+ path = /* alloc */
2195
+ [];
2196
+ for (i = 0; i < ring3.length - 1; i++) {
2197
+ path.push(
2198
+ /* alloc */
2199
+ { x: ring3[i][0], y: ring3[i][1] }
2200
+ );
2201
+ }
2202
+ paths.push(path);
2203
+ }
2204
+ return paths;
2205
+ }
2206
+
2207
+ // src/mvt/triangulateVectorRings.js
2208
+ function triangulateVectorRings(rings) {
2209
+ const polygons = groupMvtPolygons(rings), triangles = (
2210
+ /* alloc */
2211
+ []
2212
+ );
2213
+ let polygon, paths, vertices;
2214
+ for (polygon of polygons) {
2215
+ paths = mvtPolygonPaths(polygon);
2216
+ vertices = mvtPathsVertices(paths);
2217
+ appendMvtPolygonTriangles(polygon, paths, vertices, triangles);
2218
+ }
2219
+ return triangles;
2220
+ }
2221
+
2222
+ // src/vector-tiles/simplifyVectorPath.js
2223
+ function simplifyVectorPath(path, tolerance) {
2224
+ if (path.length < 3) return path;
2225
+ const keep = (
2226
+ /* alloc */
2227
+ new Uint8Array(path.length)
2228
+ );
2229
+ const stack = (
2230
+ /* alloc */
2231
+ [
2232
+ /* alloc */
2233
+ [0, path.length - 1]
2234
+ ]
2235
+ ), output = (
2236
+ /* alloc */
2237
+ []
2238
+ );
2239
+ keep[0] = keep[path.length - 1] = 1;
2240
+ let start, end, a, b, dx, dy, length, index, distance, i, p, t, squared;
2241
+ while (stack.length) {
2242
+ [start, end] = stack.pop();
2243
+ a = path[start];
2244
+ b = path[end];
2245
+ dx = b[0] - a[0];
2246
+ dy = b[1] - a[1];
2247
+ length = dx * dx + dy * dy;
2248
+ index = -1;
2249
+ distance = tolerance * tolerance;
2250
+ for (i = start + 1; i < end; i++) {
2251
+ p = path[i];
2252
+ t = length ? Math.max(0, Math.min(1, ((p[0] - a[0]) * dx + (p[1] - a[1]) * dy) / length)) : 0;
2253
+ squared = (p[0] - a[0] - t * dx) ** 2 + (p[1] - a[1] - t * dy) ** 2;
2254
+ if (squared > distance) {
2255
+ distance = squared;
2256
+ index = i;
2257
+ }
2258
+ }
2259
+ if (index >= 0) {
2260
+ keep[index] = 1;
2261
+ stack.push(
2262
+ /* alloc */
2263
+ [start, index],
2264
+ /* alloc */
2265
+ [index, end]
2266
+ );
2267
+ }
2268
+ }
2269
+ for (i = 0; i < path.length; i++) if (keep[i]) output.push(path[i]);
2270
+ return output;
2271
+ }
2272
+
2273
+ // src/vector-tiles/projectVectorTriangles.js
2274
+ function projectVectorTriangles(geometry, coords, extent, tolerance) {
2275
+ const paths = (
2276
+ /* alloc */
2277
+ []
2278
+ ), rings = (
2279
+ /* alloc */
2280
+ []
2281
+ );
2282
+ let triangle, projected, point, ring3;
2283
+ for (ring3 of geometry) {
2284
+ ring3 = simplifyVectorPath(ring3, tolerance);
2285
+ if (ring3.length >= 4) rings.push(ring3);
2286
+ }
2287
+ for (triangle of triangulateVectorRings(rings)) {
2288
+ projected = /* alloc */
2289
+ [];
2290
+ for (point of triangle) projected.push(projectVectorPoint(point, coords, extent));
2291
+ paths.push(
2292
+ /* alloc */
2293
+ [projected]
2294
+ );
2295
+ }
2296
+ return paths;
2297
+ }
2298
+
2299
+ // src/vector-tiles/projectVectorPaths.js
2300
+ function projectVectorPaths(geometry, coords, extent, tolerance) {
2301
+ const paths = (
2302
+ /* alloc */
2303
+ []
2304
+ );
2305
+ let path, simplified, projected, point;
2306
+ for (path of geometry) {
2307
+ simplified = simplifyVectorPath(path, tolerance);
2308
+ if (simplified.length < 3 && Math.hypot(simplified.at(-1)[0] - simplified[0][0], simplified.at(-1)[1] - simplified[0][1]) < tolerance * 2) continue;
2309
+ projected = /* alloc */
2310
+ [];
2311
+ for (point of simplified) projected.push(projectVectorPoint(point, coords, extent));
2312
+ paths.push(projected);
2313
+ }
2314
+ return paths;
2315
+ }
2316
+
2317
+ // src/vector-tiles/vectorTolerance.js
2318
+ function vectorTolerance(job, layer) {
2319
+ return layer.extent / 256 / 2 ** Math.max(-1, job.mapZoom - job.coords.z) * 0.7;
2320
+ }
2321
+
2322
+ // src/vector-tiles/vectorFeaturePaths.js
2323
+ function vectorFeaturePaths(job, layer, entry, feature) {
2324
+ const cache = entry.type === "fill" ? job.triangles : job.projected;
2325
+ let paths = cache.get(feature);
2326
+ if (!paths) {
2327
+ const tolerance = vectorTolerance(job, layer);
2328
+ paths = entry.type === "fill" ? projectVectorTriangles(feature.geometry, job.coords, layer.extent, tolerance) : projectVectorPaths(feature.geometry, job.coords, layer.extent, tolerance);
2329
+ cache.set(feature, paths);
2330
+ }
2331
+ return paths;
2332
+ }
2333
+
2334
+ // src/vector-tiles/appendVectorBatch.js
2335
+ function appendVectorBatch(job, layer, entry, feature, order, batches, dependencies, paint) {
2336
+ const values = (
2337
+ /* alloc */
2338
+ []
2339
+ );
2340
+ let key, batch, path, count;
2341
+ for (key of dependencies) values.push(feature.properties[key]);
2342
+ key = JSON.stringify(
2343
+ /* alloc */
2344
+ [paint, values]
2345
+ );
2346
+ batch = batches.get(key);
2347
+ if (!batch) {
2348
+ batch = /* alloc */
2349
+ {
2350
+ kind: entry.type,
2351
+ order,
2352
+ paint,
2353
+ properties: feature.properties,
2354
+ entry,
2355
+ paths: (
2356
+ /* alloc */
2357
+ []
2358
+ ),
2359
+ coordinates: 0
2360
+ };
2361
+ batches.set(key, batch);
2362
+ }
2363
+ for (path of vectorFeaturePaths(job, layer, entry, feature)) {
2364
+ count = entry.type === "fill" ? 3 : path.length;
2365
+ if (batch.coordinates + count > 6e3 && batch.paths.length) {
2366
+ job.output.push(
2367
+ /* alloc */
2368
+ { ...batch, paths: batch.paths }
2369
+ );
2370
+ batch.paths = /* alloc */
2371
+ [];
2372
+ batch.coordinates = 0;
2373
+ }
2374
+ batch.paths.push(path);
2375
+ batch.coordinates += count;
2376
+ }
2377
+ }
2378
+
2379
+ // src/vector-tiles/prepareVectorStyleLayer.js
2380
+ function prepareVectorStyleLayer(job, layer, entry, order) {
2381
+ const batches = (
2382
+ /* alloc */
2383
+ /* @__PURE__ */ new Map()
2384
+ ), dependencies = vectorStyleDependencies(entry);
2385
+ const outlineColor = entry.type === "fill" ? entry.paint?.["fill-outline-color"] : null;
2386
+ const outline = outlineColor ? (
2387
+ /* alloc */
2388
+ {
2389
+ ...entry,
2390
+ type: "line",
2391
+ paint: (
2392
+ /* alloc */
2393
+ { "line-color": outlineColor, "line-width": 1 }
2394
+ )
2395
+ }
2396
+ ) : null;
2397
+ let feature, sampleZoom, paint, batch;
2398
+ for (feature of layer.features) {
2399
+ sampleZoom = vectorFeatureZoom(entry, feature.properties, job.zoom, job.maxZoom);
2400
+ if (sampleZoom < 0) continue;
2401
+ if (entry.type === "symbol") {
2402
+ prepareVectorLabel(job, layer, entry, feature, order);
2403
+ continue;
2404
+ }
2405
+ paint = vectorFeaturePaint(entry, feature, sampleZoom, job.maxZoom);
2406
+ if (!paint) continue;
2407
+ appendVectorBatch(job, layer, entry, feature, order, batches, dependencies, paint);
2408
+ if (outline && feature.type === 3) {
2409
+ appendVectorBatch(
2410
+ job,
2411
+ layer,
2412
+ outline,
2413
+ feature,
2414
+ order,
2415
+ batches,
2416
+ dependencies,
2417
+ vectorPaint(outline, feature.properties, sampleZoom)
2418
+ );
2419
+ }
2420
+ }
2421
+ for (batch of batches.values()) if (batch.paths.length) job.output.push(batch);
2422
+ }
2423
+
2424
+ // src/vector-tiles/prepareVectorTile.js
2425
+ function prepareVectorTile(buffer, coords, style, zoom, source, maxZoom = 19, mapZoom = zoom) {
2426
+ const layers = decodeVectorTile(buffer), byName = (
2427
+ /* alloc */
2428
+ /* @__PURE__ */ new Map()
2429
+ );
2430
+ const job = (
2431
+ /* alloc */
2432
+ {
2433
+ coords,
2434
+ zoom,
2435
+ mapZoom,
2436
+ maxZoom,
2437
+ output: (
2438
+ /* alloc */
2439
+ []
2440
+ ),
2441
+ labels: (
2442
+ /* alloc */
2443
+ []
2444
+ ),
2445
+ triangles: (
2446
+ /* alloc */
2447
+ /* @__PURE__ */ new WeakMap()
2448
+ ),
2449
+ projected: (
2450
+ /* alloc */
2451
+ /* @__PURE__ */ new WeakMap()
2452
+ )
2453
+ }
2454
+ );
2455
+ let layer, order, entry;
2456
+ for (layer of layers) byName.set(layer.name, layer);
2457
+ for ([order, entry] of style.entries()) {
2458
+ layer = byName.get(entry["source-layer"]);
2459
+ if (!layer || source !== void 0 && entry.source !== source) continue;
2460
+ prepareVectorStyleLayer(job, layer, entry, order);
2461
+ }
2462
+ return (
2463
+ /* alloc */
2464
+ { batches: job.output, labels: job.labels }
2465
+ );
2466
+ }
2467
+
2468
+ // src/workers/vectorTileWorker.js
2469
+ function onVectorTileMessage({ data }) {
2470
+ try {
2471
+ const result = prepareVectorTile(data.buffer, data.coords, data.style, data.zoom, data.source, data.maxZoom, data.mapZoom);
2472
+ self.postMessage(
2473
+ /* alloc */
2474
+ { id: data.id, ...result }
2475
+ );
2476
+ } catch (error) {
2477
+ self.postMessage(
2478
+ /* alloc */
2479
+ { id: data.id, error: error.message }
2480
+ );
2481
+ }
2482
+ }
2483
+
2484
+ // src/cog/validateCogRange.js
2485
+ function validateCogRange(source, offset, length) {
2486
+ if (!Number.isSafeInteger(offset) || !Number.isSafeInteger(length) || offset < 0 || length <= 0 || length > 32e6 || !Number.isSafeInteger(offset + length) || source.size !== void 0 && offset + length > source.size) {
2487
+ throw (
2488
+ /* alloc */
2489
+ new Error("Invalid COG byte range")
2490
+ );
2491
+ }
2492
+ }
2493
+
2494
+ // src/cog/validateCogContentRange.js
2495
+ function validateCogContentRange(source, range, rangeStart, length) {
2496
+ if (!range || Number(range[1]) !== rangeStart || Number(range[2]) !== rangeStart + length - 1 || !Number.isSafeInteger(Number(range[3])) || Number(range[3]) < rangeStart + length || (source.containerSize ?? source.size) !== void 0 && (source.containerSize ?? source.size) !== Number(range[3])) {
2497
+ throw (
2498
+ /* alloc */
2499
+ new Error("Invalid COG range response")
2500
+ );
2501
+ }
2502
+ }
2503
+
2504
+ // src/cog/invalidCogFullResponse.js
2505
+ function invalidCogFullResponse(response) {
2506
+ return response.status !== 200 || Number(response.headers.get("Content-Length")) > 32e6;
2507
+ }
2508
+
2509
+ // src/cog/validateCogResponse.js
2510
+ function validateCogResponse(source, response, rangeStart, length) {
2511
+ const etag = response.headers.get("ETag");
2512
+ if (source.etag && source.etag !== etag) throw (
2513
+ /* alloc */
2514
+ new Error("COG changed; recreate the layer")
2515
+ );
2516
+ if (etag) source.etag = etag;
2517
+ const range = /^bytes (\d+)-(\d+)\/(\d+)$/.exec(response.headers.get("Content-Range") || "");
2518
+ if (response.status === 206) {
2519
+ validateCogContentRange(source, range, rangeStart, length);
2520
+ if (source.baseOffset === void 0) source.size = Number(range[3]);
2521
+ } else if (invalidCogFullResponse(response)) {
2522
+ throw (
2523
+ /* alloc */
2524
+ new Error("COG server must support byte ranges for files over 32 MB")
2525
+ );
2526
+ }
2527
+ }
2528
+
2529
+ // src/cog/readCogBody.js
2530
+ async function readCogBody(response, length) {
2531
+ const reader = response.body.getReader(), chunks = (
2532
+ /* alloc */
2533
+ []
2534
+ );
2535
+ let total = 0, part;
2536
+ try {
2537
+ while (!(part = await reader.read()).done) {
2538
+ total += part.value.byteLength;
2539
+ if (total > (response.status === 206 ? length : 32e6)) {
2540
+ await reader.cancel();
2541
+ throw (
2542
+ /* alloc */
2543
+ new Error("Oversized COG response")
2544
+ );
2545
+ }
2546
+ chunks.push(part.value);
2547
+ }
2548
+ } finally {
2549
+ reader.releaseLock();
2550
+ }
2551
+ const data = (
2552
+ /* alloc */
2553
+ new Uint8Array(total)
2554
+ );
2555
+ let start = 0, chunk;
2556
+ for (chunk of chunks) {
2557
+ data.set(chunk, start);
2558
+ start += chunk.length;
2559
+ }
2560
+ return data;
2561
+ }
2562
+
2563
+ // src/cog/readCog.js
2564
+ async function readCog(source, offset, length, signal) {
2565
+ signal?.throwIfAborted();
2566
+ validateCogRange(source, offset, length);
2567
+ if (source.full) return source.full.slice(offset, offset + length);
2568
+ const rangeStart = (source.baseOffset || 0) + offset;
2569
+ const response = await fetch(
2570
+ source.url,
2571
+ /* alloc */
2572
+ {
2573
+ headers: (
2574
+ /* alloc */
2575
+ { Range: `bytes=${rangeStart}-${rangeStart + length - 1}` }
2576
+ ),
2577
+ signal
2578
+ }
2579
+ );
2580
+ if (!response.ok) throw (
2581
+ /* alloc */
2582
+ new Error(`Unable to read COG: HTTP ${response.status}`)
2583
+ );
2584
+ validateCogResponse(source, response, rangeStart, length);
2585
+ const data = await readCogBody(response, length);
2586
+ if (response.status === 200) {
2587
+ if (source.baseOffset === void 0) source.size = data.byteLength;
2588
+ else if (data.byteLength !== source.containerSize) throw (
2589
+ /* alloc */
2590
+ new Error("Truncated PMTiles COG container")
2591
+ );
2592
+ source.full = source.baseOffset === void 0 ? data.buffer : data.slice(source.baseOffset, source.baseOffset + source.size);
2593
+ }
2594
+ const result = response.status === 200 ? source.full.slice(offset, offset + length) : data.buffer;
2595
+ if (result.byteLength !== length) throw (
2596
+ /* alloc */
2597
+ new Error("Truncated COG byte range")
2598
+ );
2599
+ return result;
2600
+ }
2601
+
2602
+ // src/globals/cogTags.js
2603
+ var cogTags = [256, 257, 258, 259, 262, 277, 284, 317, 322, 323, 324, 325, 339, 33550, 33922, 34735];
2604
+
2605
+ // src/cog/readCogTag.js
2606
+ async function readCogTag(source, entries, i, tag, little) {
2607
+ const type = entries.getUint16(i * 12 + 2, little);
2608
+ const size = type === 3 ? 2 : type === 4 ? 4 : type === 12 ? 8 : 0;
2609
+ const length = entries.getUint32(i * 12 + 4, little);
2610
+ if (!size || !length || length > 1048576) throw (
2611
+ /* alloc */
2612
+ new Error(`Unsupported COG tag ${tag}`)
2613
+ );
2614
+ const data = size * length <= 4 ? (
2615
+ /* alloc */
2616
+ new DataView(entries.buffer, i * 12 + 8, 4)
2617
+ ) : (
2618
+ /* alloc */
2619
+ new DataView(await readCog(source, entries.getUint32(i * 12 + 8, little), size * length))
2620
+ );
2621
+ const values = (
2622
+ /* alloc */
2623
+ []
2624
+ );
2625
+ let j;
2626
+ for (j = 0; j < length; j++) values.push(type === 3 ? data.getUint16(j * size, little) : type === 4 ? data.getUint32(j * size, little) : data.getFloat64(j * size, little));
2627
+ return values;
2628
+ }
2629
+
2630
+ // src/cog/readCogDirectory.js
2631
+ async function readCogDirectory(source, offset) {
2632
+ const little = source.little;
2633
+ const count = (
2634
+ /* alloc */
2635
+ new DataView(await readCog(source, offset, 2)).getUint16(0, little)
2636
+ );
2637
+ if (!count || count > 128) throw (
2638
+ /* alloc */
2639
+ new Error("Invalid COG directory size")
2640
+ );
2641
+ const entries = (
2642
+ /* alloc */
2643
+ new DataView(await readCog(source, offset + 2, count * 12 + 4))
2644
+ );
2645
+ const tags = (
2646
+ /* alloc */
2647
+ /* @__PURE__ */ new Map()
2648
+ );
2649
+ let i, tag;
2650
+ for (i = 0; i < count; i++) {
2651
+ tag = entries.getUint16(i * 12, little);
2652
+ if (tag === 42113) throw (
2653
+ /* alloc */
2654
+ new Error("COG nodata tags are not supported; export complete tiles")
2655
+ );
2656
+ if (tag === 34264) throw (
2657
+ /* alloc */
2658
+ new Error("COG affine transformation matrices are not supported")
2659
+ );
2660
+ if (!cogTags.includes(tag)) continue;
2661
+ if (tags.has(tag)) throw (
2662
+ /* alloc */
2663
+ new Error("Duplicate COG tag")
2664
+ );
2665
+ tags.set(tag, await readCogTag(source, entries, i, tag, little));
2666
+ }
2667
+ return (
2668
+ /* alloc */
2669
+ { tags, next: entries.getUint32(count * 12, little) }
2670
+ );
2671
+ }
2672
+
2673
+ // src/cog/validCogDimensions.js
2674
+ function validCogDimensions(tags, width, height) {
2675
+ return !!width && !!height && width % 256 === 0 && height % 256 === 0 && tags.get(322)?.[0] === 256 && tags.get(323)?.[0] === 256 && (tags.get(259)?.[0] === 1 || tags.get(259)?.[0] === 8) && (tags.get(284)?.[0] ?? 1) === 1 && (tags.get(317)?.[0] ?? 1) === 1;
2676
+ }
2677
+
2678
+ // src/cog/validCogEncoding.js
2679
+ function validCogEncoding(tags, samples, bits, format, elevation, bytes, unsigned) {
2680
+ if (elevation) return tags.get(262)?.[0] === 1;
2681
+ return (samples === 3 || samples === 4) && bits?.length === samples && bytes && unsigned && tags.get(262)?.[0] === 2;
2682
+ }
2683
+
2684
+ // src/cog/getCogProfile.js
2685
+ function getCogProfile(tags) {
2686
+ const width = tags.get(256)?.[0], height = tags.get(257)?.[0], samples = tags.get(277)?.[0] ?? 1;
2687
+ const bits = tags.get(258), format = tags.get(339) || /* alloc */
2688
+ [1];
2689
+ let bytes = true, unsigned = true, floating = true, value;
2690
+ for (value of bits || /* alloc */
2691
+ []) bytes &&= value === 8;
2692
+ for (value of format) {
2693
+ unsigned &&= value === 1;
2694
+ floating &&= value === 3;
2695
+ }
2696
+ const elevation = samples === 1 && bits?.length === 1 && bits[0] === 32 && floating;
2697
+ if (!validCogDimensions(tags, width, height) || !validCogEncoding(tags, samples, bits, format, elevation, bytes, unsigned)) {
2698
+ throw (
2699
+ /* alloc */
2700
+ new Error("Unsupported COG profile: expected tiled 256px RGB(A) Uint8 or Float32, DEFLATE without predictor")
2701
+ );
2702
+ }
2703
+ return (
2704
+ /* alloc */
2705
+ { width, height, samples, elevation }
2706
+ );
2707
+ }
2708
+
2709
+ // src/cog/hasCogProjection.js
2710
+ function hasCogProjection(keys) {
2711
+ let projected = false, area2 = false, i;
2712
+ if (keys) for (i = 4; i < keys.length; i += 4) {
2713
+ if (keys[i + 1] !== 0 || keys[i + 2] !== 1) continue;
2714
+ if (keys[i] === 3072 && keys[i + 3] === 3857) projected = true;
2715
+ if (keys[i] === 1025 && keys[i + 3] === 1) area2 = true;
2716
+ }
2717
+ return projected && area2;
2718
+ }
2719
+
2720
+ // src/cog/finiteCogValues.js
2721
+ function finiteCogValues(values) {
2722
+ let value;
2723
+ for (value of values || /* alloc */
2724
+ []) if (!Number.isFinite(value)) return false;
2725
+ return true;
2726
+ }
2727
+
2728
+ // src/cog/validCogGeoArrays.js
2729
+ function validCogGeoArrays(keys, scale, tie) {
2730
+ return !!keys && keys[0] === 1 && keys.length === 4 + keys[3] * 4 && !!scale && scale.length === 3 && finiteCogValues(scale) && scale[0] > 0 && Math.abs(scale[0] - scale[1]) <= 1e-8 && !!tie && tie.length === 6 && finiteCogValues(tie) && tie[0] === 0 && tie[1] === 0;
2731
+ }
2732
+
2733
+ // src/cog/getCogBase.js
2734
+ function getCogBase(tags, width, height, elevation) {
2735
+ const keys = tags.get(34735), scale = tags.get(33550), tie = tags.get(33922);
2736
+ if (!validCogGeoArrays(keys, scale, tie) || !hasCogProjection(keys)) {
2737
+ throw (
2738
+ /* alloc */
2739
+ new Error("COG must use north-up EPSG:3857 PixelIsArea georeferencing")
2740
+ );
2741
+ }
2742
+ return (
2743
+ /* alloc */
2744
+ { width, height, resolution: scale[0], left: tie[3], top: tie[4], elevation }
2745
+ );
2746
+ }
2747
+
2748
+ // src/cog/validCogGrid.js
2749
+ function validCogGrid(source, base, profile, zoom, x, y, resolution) {
2750
+ return zoom >= 0 && zoom <= 24 && Math.abs(20037508342789244e-9 * 2 / (256 * 2 ** zoom) - resolution) <= resolution * 1e-7 && (!profile.elevation || Number.isInteger(x) && Number.isInteger(y)) && Math.abs(base.width / profile.width - base.height / profile.height) <= 1e-7 && profile.elevation === base.elevation && x >= 0 && y >= 0 && x + profile.width / 256 <= 2 ** zoom && y + profile.height / 256 <= 2 ** zoom && !source.levels.has(zoom) && !(base.elevation && source.levels.size);
2751
+ }
2752
+
2753
+ // src/cog/validateCogOffsets.js
2754
+ function validateCogOffsets(source, level) {
2755
+ if (level.offsets?.length !== level.columns * level.rows || level.lengths?.length !== level.offsets.length) {
2756
+ throw (
2757
+ /* alloc */
2758
+ new Error("Invalid COG tile offsets or lengths")
2759
+ );
2760
+ }
2761
+ let i;
2762
+ for (i = 0; i < level.offsets.length; i++) {
2763
+ if (!level.offsets[i] || !level.lengths[i] || level.lengths[i] > 1048576 || level.offsets[i] + level.lengths[i] > source.size) throw (
2764
+ /* alloc */
2765
+ new Error("Invalid COG tile offsets or lengths")
2766
+ );
2767
+ }
2768
+ }
2769
+
2770
+ // src/cog/readCogLevel.js
2771
+ function readCogLevel(source, directory, state) {
2772
+ const tags = directory.tags, profile = getCogProfile(tags);
2773
+ if (!state.base) state.base = getCogBase(tags, profile.width, profile.height, profile.elevation);
2774
+ const base = state.base, resolution = base.resolution * base.width / profile.width;
2775
+ const zoom = Math.round(Math.log2(20037508342789244e-9 * 2 / (256 * resolution)));
2776
+ const span = 20037508342789244e-9 * 2 / 2 ** zoom;
2777
+ let x = (base.left + 20037508342789244e-9) / span;
2778
+ let y = (20037508342789244e-9 - base.top) / span;
2779
+ if (Math.abs(x - Math.round(x)) < 1e-6) x = Math.round(x);
2780
+ if (Math.abs(y - Math.round(y)) < 1e-6) y = Math.round(y);
2781
+ if (!validCogGrid(source, base, profile, zoom, x, y, resolution)) {
2782
+ throw (
2783
+ /* alloc */
2784
+ new Error("COG levels must align with the Web Mercator XYZ tile grid")
2785
+ );
2786
+ }
2787
+ const level = (
2788
+ /* alloc */
2789
+ {
2790
+ x,
2791
+ y,
2792
+ columns: profile.width / 256,
2793
+ rows: profile.height / 256,
2794
+ samples: profile.samples,
2795
+ elevation: profile.elevation,
2796
+ compression: tags.get(259)[0],
2797
+ offsets: tags.get(324),
2798
+ lengths: tags.get(325)
2799
+ }
2800
+ );
2801
+ validateCogOffsets(source, level);
2802
+ source.levels.set(zoom, level);
2803
+ state.minZoom = Math.min(state.minZoom, zoom);
2804
+ state.maxZoom = Math.max(state.maxZoom, zoom);
2805
+ }
2806
+
2807
+ // src/cog/readCogDirectories.js
2808
+ async function readCogDirectories(source, offset) {
2809
+ const state = (
2810
+ /* alloc */
2811
+ { base: null, minZoom: Infinity, maxZoom: -Infinity }
2812
+ );
2813
+ const seen = (
2814
+ /* alloc */
2815
+ /* @__PURE__ */ new Set()
2816
+ );
2817
+ let directory;
2818
+ while (offset) {
2819
+ if (seen.has(offset) || seen.size >= 24) throw (
2820
+ /* alloc */
2821
+ new Error("Cyclic or excessive COG directories")
2822
+ );
2823
+ seen.add(offset);
2824
+ directory = await readCogDirectory(source, offset);
2825
+ readCogLevel(source, directory, state);
2826
+ offset = directory.next;
2827
+ }
2828
+ if (!state.base) throw (
2829
+ /* alloc */
2830
+ new Error("COG has no image directory")
2831
+ );
2832
+ if (source.levels.size !== state.maxZoom - state.minZoom + 1) {
2833
+ throw (
2834
+ /* alloc */
2835
+ new Error("COG overview zooms must be contiguous")
2836
+ );
2837
+ }
2838
+ return (
2839
+ /* alloc */
2840
+ { base: state.base, minZoom: state.minZoom, maxZoom: state.maxZoom }
2841
+ );
2842
+ }
2843
+
2844
+ // src/cog/decodeCogTile.js
2845
+ async function decodeCogTile(data, length, compression) {
2846
+ if (compression === 1) {
2847
+ if (data.byteLength !== length) throw (
2848
+ /* alloc */
2849
+ new Error("Invalid uncompressed COG tile size")
2850
+ );
2851
+ return data;
2852
+ }
2853
+ const stream = (
2854
+ /* alloc */
2855
+ new Blob(
2856
+ /* alloc */
2857
+ [data]
2858
+ ).stream().pipeThrough(
2859
+ /* alloc */
2860
+ new DecompressionStream("deflate")
2861
+ )
2862
+ );
2863
+ const reader = stream.getReader(), result = (
2864
+ /* alloc */
2865
+ new Uint8Array(length)
2866
+ );
2867
+ let offset = 0, chunk;
2868
+ try {
2869
+ while (!(chunk = await reader.read()).done) {
2870
+ if (offset + chunk.value.length > length) {
2871
+ await reader.cancel();
2872
+ throw (
2873
+ /* alloc */
2874
+ new Error("Oversized decoded COG tile")
2875
+ );
2876
+ }
2877
+ result.set(chunk.value, offset);
2878
+ offset += chunk.value.length;
2879
+ }
2880
+ } finally {
2881
+ reader.releaseLock();
2882
+ }
2883
+ if (offset !== length) throw (
2884
+ /* alloc */
2885
+ new Error("Truncated decoded COG tile")
2886
+ );
2887
+ return result.buffer;
2888
+ }
2889
+
2890
+ // src/cog/readCogBlock.js
2891
+ async function readCogBlock(source, level, x, y, signal) {
2892
+ const index = y * level.columns + x;
2893
+ return decodeCogTile(
2894
+ await readCog(source, level.offsets[index], level.lengths[index], signal),
2895
+ 256 * 256 * (level.elevation ? 4 : level.samples),
2896
+ level.compression
2897
+ );
2898
+ }
2899
+
2900
+ // src/cog/getElevationTile.js
2901
+ async function getElevationTile(source, level, x, y, signal) {
2902
+ const data = await readCogBlock(source, level, x - level.x, y - level.y, signal);
2903
+ signal?.throwIfAborted();
2904
+ const view = (
2905
+ /* alloc */
2906
+ new DataView(data)
2907
+ ), heights = (
2908
+ /* alloc */
2909
+ new Float32Array(256 * 256)
2910
+ );
2911
+ let i, height;
2912
+ for (i = 0; i < heights.length; i++) {
2913
+ height = view.getFloat32(i * 4, source.little);
2914
+ if (!Number.isFinite(height) || height < -12e3 || height > 1e4) {
2915
+ throw (
2916
+ /* alloc */
2917
+ new Error("Invalid or missing COG elevation sample")
2918
+ );
2919
+ }
2920
+ heights[i] = height;
2921
+ }
2922
+ return (
2923
+ /* alloc */
2924
+ { data: heights.buffer }
2925
+ );
2926
+ }
2927
+
2928
+ // src/cog/getColorTile.js
2929
+ async function getColorTile(source, level, x, y, signal) {
2930
+ const canvas = (
2931
+ /* alloc */
2932
+ new OffscreenCanvas(256, 256)
2933
+ ), context = canvas.getContext("2d");
2934
+ if (!context) throw (
2935
+ /* alloc */
2936
+ new Error("Unable to create COG tile canvas")
2937
+ );
2938
+ const left = (x - level.x) * 256, top = (y - level.y) * 256;
2939
+ let bx, by, pixels, rgba, i, j, bitmap;
2940
+ for (by = Math.max(0, Math.floor(top / 256)); by < Math.min(level.rows, Math.ceil(top / 256 + 1)); by++) {
2941
+ for (bx = Math.max(0, Math.floor(left / 256)); bx < Math.min(level.columns, Math.ceil(left / 256 + 1)); bx++) {
2942
+ pixels = /* alloc */
2943
+ new Uint8Array(await readCogBlock(source, level, bx, by, signal));
2944
+ signal?.throwIfAborted();
2945
+ rgba = /* alloc */
2946
+ new Uint8ClampedArray(256 * 256 * 4);
2947
+ for (i = 0, j = 0; i < pixels.length; i += level.samples, j += 4) {
2948
+ rgba[j] = pixels[i];
2949
+ rgba[j + 1] = pixels[i + 1];
2950
+ rgba[j + 2] = pixels[i + 2];
2951
+ rgba[j + 3] = level.samples === 4 ? pixels[i + 3] : 255;
2952
+ }
2953
+ bitmap = await createImageBitmap(
2954
+ /* alloc */
2955
+ new ImageData(rgba, 256, 256)
2956
+ );
2957
+ context.drawImage(bitmap, bx * 256 - left, by * 256 - top);
2958
+ bitmap.close();
2959
+ }
2960
+ }
2961
+ const image = canvas.transferToImageBitmap();
2962
+ if (signal?.aborted) {
2963
+ image.close();
2964
+ signal.throwIfAborted();
2965
+ }
2966
+ return (
2967
+ /* alloc */
2968
+ { image }
2969
+ );
2970
+ }
2971
+
2972
+ // src/cog/validCogTileCoordinates.js
2973
+ function validCogTileCoordinates(z, x, y) {
2974
+ return Number.isSafeInteger(z) && Number.isSafeInteger(x) && Number.isSafeInteger(y) && z >= 0 && z <= 24 && x >= 0 && y >= 0 && x < 2 ** z && y < 2 ** z;
2975
+ }
2976
+
2977
+ // src/cog/getCogTile.js
2978
+ async function getCogTile(source, z, x, y, signal) {
2979
+ signal?.throwIfAborted();
2980
+ if (!validCogTileCoordinates(z, x, y)) throw (
2981
+ /* alloc */
2982
+ new Error("Invalid COG tile coordinates")
2983
+ );
2984
+ const level = source.levels.get(z);
2985
+ if (!level || x + 1 <= level.x || y + 1 <= level.y || x >= level.x + level.columns || y >= level.y + level.rows) return;
2986
+ if (level.elevation) return getElevationTile(source, level, x, y, signal);
2987
+ return getColorTile(source, level, x, y, signal);
2988
+ }
2989
+
2990
+ // src/cog/getCogHeader.js
2991
+ function getCogHeader(source) {
2992
+ return source.header;
2993
+ }
2994
+
2995
+ // src/globals/headerOffsets.js
2996
+ var headerOffsets = [
2997
+ "rootDirectoryOffset",
2998
+ "rootDirectoryLength",
2999
+ "jsonMetadataOffset",
3000
+ "jsonMetadataLength",
3001
+ "leafDirectoryOffset",
3002
+ "leafDirectoryLength",
3003
+ "tileDataOffset",
3004
+ "tileDataLength",
3005
+ "numAddressedTiles",
3006
+ "numTileEntries",
3007
+ "numTileContents"
3008
+ ];
3009
+
3010
+ // src/globals/headerFlags.js
3011
+ var headerFlags = ["clustered", "internalCompression", "tileCompression", "tileType", "minZoom", "maxZoom"];
3012
+
3013
+ // src/globals/headerBounds.js
3014
+ var headerBounds = ["minLon", "minLat", "maxLon", "maxLat"];
3015
+
3016
+ // src/archives/decodeHeader.js
3017
+ function decodeHeader(data) {
3018
+ const view = data.byteLength >= 127 ? (
3019
+ /* alloc */
3020
+ new DataView(data)
3021
+ ) : null;
3022
+ if (!view || view.getUint32(0, true) !== 1767132496 || view.getUint32(4, true) !== 57894252) {
3023
+ throw (
3024
+ /* alloc */
3025
+ new Error("Expected a PMTiles v3 archive")
3026
+ );
3027
+ }
3028
+ const header = (
3029
+ /* alloc */
3030
+ {}
3031
+ );
3032
+ let i;
3033
+ for (i = 0; i < headerOffsets.length; i++) header[headerOffsets[i]] = Number(view.getBigUint64(8 + i * 8, true));
3034
+ for (i = 0; i < headerFlags.length; i++) header[headerFlags[i]] = view.getUint8(96 + i);
3035
+ for (i = 0; i < headerBounds.length; i++) header[headerBounds[i]] = view.getInt32(102 + i * 4, true) / 1e7;
3036
+ header.centerZoom = view.getUint8(118);
3037
+ header.centerLon = view.getInt32(119, true) / 1e7;
3038
+ header.centerLat = view.getInt32(123, true) / 1e7;
3039
+ return header;
3040
+ }
3041
+
3042
+ // src/cog/validateCogContainer.js
3043
+ function validateCogContainer(container, header) {
3044
+ if (header.tileType !== 0 || header.tileCompression !== 1 || !Number.isSafeInteger(header.jsonMetadataOffset) || !Number.isSafeInteger(header.jsonMetadataLength) || header.jsonMetadataLength <= 0 || header.jsonMetadataLength > 1048576 || !Number.isSafeInteger(header.tileDataOffset) || !Number.isSafeInteger(header.tileDataLength) || header.tileDataOffset < 127 || header.tileDataOffset + header.tileDataLength > container.size) {
3045
+ throw (
3046
+ /* alloc */
3047
+ new Error("Invalid PMTiles COG asset container")
3048
+ );
3049
+ }
3050
+ }
3051
+
3052
+ // src/archives/decompress.js
3053
+ async function decompress(data, compression) {
3054
+ if (compression === 1) return data;
3055
+ if (compression !== 2) {
3056
+ throw (
3057
+ /* alloc */
3058
+ new Error(`Unsupported PMTiles compression: ${compression}`)
3059
+ );
3060
+ }
3061
+ const blob = (
3062
+ /* alloc */
3063
+ new Blob(
3064
+ /* alloc */
3065
+ [data]
3066
+ )
3067
+ );
3068
+ const stream = (
3069
+ /* alloc */
3070
+ new DecompressionStream("gzip")
3071
+ );
3072
+ return (
3073
+ /* alloc */
3074
+ new Response(blob.stream().pipeThrough(stream)).arrayBuffer()
3075
+ );
3076
+ }
3077
+
3078
+ // src/cog/getCogAssetEntry.js
3079
+ async function getCogAssetEntry(container, header, asset) {
3080
+ const metadata = JSON.parse(
3081
+ /* alloc */
3082
+ new TextDecoder().decode(await decompress(
3083
+ await readCog(container, header.jsonMetadataOffset, header.jsonMetadataLength),
3084
+ header.internalCompression
3085
+ ))
3086
+ );
3087
+ const assets = metadata.cog_assets || /* alloc */
3088
+ {};
3089
+ const entry = Object.hasOwn(assets, asset) ? metadata.cog_assets[asset] : null;
3090
+ if (metadata.format !== "cog-assets-v1" || !entry || !Number.isSafeInteger(entry.offset) || !Number.isSafeInteger(entry.length) || entry.offset < 0 || entry.length < 8 || entry.offset + entry.length > header.tileDataLength) {
3091
+ throw (
3092
+ /* alloc */
3093
+ new Error(`Invalid or missing PMTiles COG asset: ${asset}`)
3094
+ );
3095
+ }
3096
+ return entry;
3097
+ }
3098
+
3099
+ // src/cog/openCogAsset.js
3100
+ async function openCogAsset(url, asset) {
3101
+ const container = (
3102
+ /* alloc */
3103
+ { url }
3104
+ );
3105
+ const header = decodeHeader(await readCog(container, 0, 127));
3106
+ validateCogContainer(container, header);
3107
+ const entry = await getCogAssetEntry(container, header, asset);
3108
+ const baseOffset = header.tileDataOffset + entry.offset;
3109
+ return (
3110
+ /* alloc */
3111
+ {
3112
+ url,
3113
+ baseOffset,
3114
+ size: entry.length,
3115
+ containerSize: container.size,
3116
+ etag: container.etag,
3117
+ full: container.full?.slice(baseOffset, baseOffset + entry.length)
3118
+ }
3119
+ );
3120
+ }
3121
+
3122
+ // src/cog/openCog.js
3123
+ async function openCog(url, asset) {
3124
+ const source = asset === void 0 ? (
3125
+ /* alloc */
3126
+ { url }
3127
+ ) : await openCogAsset(url, asset);
3128
+ source.levels = /* alloc */
3129
+ /* @__PURE__ */ new Map();
3130
+ const header = (
3131
+ /* alloc */
3132
+ new DataView(await readCog(source, 0, 8))
3133
+ );
3134
+ const order = header.getUint16(0);
3135
+ if (order !== 18761 && order !== 19789) throw (
3136
+ /* alloc */
3137
+ new Error("Invalid TIFF byte order")
3138
+ );
3139
+ source.little = order === 18761;
3140
+ if (header.getUint16(2, source.little) !== 42) throw (
3141
+ /* alloc */
3142
+ new Error("Expected classic TIFF (BigTIFF is not supported)")
3143
+ );
3144
+ const { base, minZoom, maxZoom } = await readCogDirectories(source, header.getUint32(4, source.little));
3145
+ const half = 20037508342789244e-9;
3146
+ const left = base.left + base.resolution / 2, right = base.left + (base.width - 0.5) * base.resolution;
3147
+ const top = base.top - base.resolution / 2, bottom = base.top - (base.height - 0.5) * base.resolution;
3148
+ source.header = /* alloc */
3149
+ {
3150
+ minZoom,
3151
+ maxZoom,
3152
+ tileType: base.elevation ? 0 : 2,
3153
+ minLon: left / half * 180,
3154
+ maxLon: right / half * 180,
3155
+ minLat: Math.atan(Math.sinh(bottom / half * Math.PI)) * 180 / Math.PI,
3156
+ maxLat: Math.atan(Math.sinh(top / half * Math.PI)) * 180 / Math.PI
3157
+ };
3158
+ return (
3159
+ /* alloc */
3160
+ {
3161
+ getHeader: getCogHeader.bind(null, source),
3162
+ getZxy: getCogTile.bind(null, source)
3163
+ }
3164
+ );
3165
+ }
3166
+
3167
+ // src/globals/workerCogSource.js
3168
+ var workerCogSource = { key: null, archive: null };
3169
+
3170
+ // src/workers/cogWorker.js
3171
+ async function onCogMessage({ data }) {
3172
+ try {
3173
+ const key = JSON.stringify(
3174
+ /* alloc */
3175
+ [data.url, data.asset]
3176
+ );
3177
+ if (workerCogSource.key !== key) {
3178
+ workerCogSource.key = key;
3179
+ workerCogSource.archive = openCog(data.url, data.asset);
3180
+ }
3181
+ const archive = await workerCogSource.archive;
3182
+ const result = data.z === void 0 ? await archive.getHeader() : await archive.getZxy(data.z, data.x, data.y);
3183
+ const transfers = (
3184
+ /* alloc */
3185
+ []
3186
+ );
3187
+ if (result?.data) transfers.push(result.data);
3188
+ if (result?.image) transfers.push(result.image);
3189
+ self.postMessage(result, transfers);
3190
+ } catch (error) {
3191
+ self.postMessage(
3192
+ /* alloc */
3193
+ { error: (
3194
+ /* alloc */
3195
+ { name: error.name, message: error.message }
3196
+ ) }
3197
+ );
3198
+ }
3199
+ }
3200
+
3201
+ // src/globals/geometryDecoder.js
3202
+ var geometryDecoder = new TextDecoder();
3203
+
3204
+ // src/models/hasUnsupportedPrimitives.js
3205
+ function hasUnsupportedPrimitives(meshes) {
3206
+ if (!meshes) return false;
3207
+ let mesh, primitive;
3208
+ for (mesh of meshes) {
3209
+ for (primitive of mesh.primitives) {
3210
+ if (primitive.targets || primitive.mode !== void 0 && primitive.mode !== 4) return true;
3211
+ }
3212
+ }
3213
+ return false;
3214
+ }
3215
+
3216
+ // src/models/decodeModel.js
3217
+ function decodeModel(data) {
3218
+ const view = (
3219
+ /* alloc */
3220
+ new DataView(data)
3221
+ );
3222
+ const decoder = geometryDecoder;
3223
+ let description;
3224
+ let offset, length, type, chunk;
3225
+ if (view.getUint32(0, true) === 1179937895) {
3226
+ if (view.getUint32(4, true) !== 2 || view.getUint32(8, true) !== data.byteLength) {
3227
+ throw (
3228
+ /* alloc */
3229
+ new Error("Invalid GLB header")
3230
+ );
3231
+ }
3232
+ let binary;
3233
+ for (offset = 12; offset < data.byteLength; ) {
3234
+ if (offset + 8 > data.byteLength) {
3235
+ throw (
3236
+ /* alloc */
3237
+ new Error("Truncated GLB chunk")
3238
+ );
3239
+ }
3240
+ length = view.getUint32(offset, true), type = view.getUint32(offset + 4, true);
3241
+ offset += 8;
3242
+ if (offset + length > data.byteLength) {
3243
+ throw (
3244
+ /* alloc */
3245
+ new Error("Truncated GLB data")
3246
+ );
3247
+ }
3248
+ chunk = data.slice(offset, offset + length);
3249
+ if (type === 1313821514) {
3250
+ description = JSON.parse(decoder.decode(chunk));
3251
+ }
3252
+ if (type === 5130562) {
3253
+ binary = chunk;
3254
+ }
3255
+ offset += length;
3256
+ }
3257
+ if (!description) {
3258
+ throw (
3259
+ /* alloc */
3260
+ new Error("Missing GLB JSON")
3261
+ );
3262
+ }
3263
+ if (binary) {
3264
+ description.buffers[0].binary = binary;
3265
+ }
3266
+ } else {
3267
+ description = JSON.parse(decoder.decode(data));
3268
+ }
3269
+ if (description.skins?.length || description.animations?.length || description.extensionsRequired?.length || hasUnsupportedPrimitives(description.meshes)) {
3270
+ throw (
3271
+ /* alloc */
3272
+ new Error("Layer3D supports static triangle glTF models without required extensions")
3273
+ );
3274
+ }
3275
+ return description;
3276
+ }
3277
+
3278
+ // src/models/loadBuffer.js
3279
+ async function loadBuffer(buffer, directory) {
3280
+ if (buffer.binary) return;
3281
+ const response = await fetch(
3282
+ /* alloc */
3283
+ new URL(buffer.uri, directory).href
3284
+ );
3285
+ if (!response.ok) throw (
3286
+ /* alloc */
3287
+ new Error(`Unable to load model buffer: HTTP ${response.status}`)
3288
+ );
3289
+ buffer.binary = await response.arrayBuffer();
3290
+ }
3291
+
3292
+ // src/globals/geometryImageOptions.js
3293
+ var geometryImageOptions = {
3294
+ premultiplyAlpha: "none",
3295
+ colorSpaceConversion: "none"
3296
+ };
3297
+
3298
+ // src/textures/loadModelImage.js
3299
+ async function loadImage(description, directory, image) {
3300
+ let blob;
3301
+ if (image.bufferView !== void 0) {
3302
+ const view = description.bufferViews[image.bufferView];
3303
+ const buffer = description.buffers[view.buffer].binary;
3304
+ blob = /* alloc */
3305
+ new Blob(
3306
+ /* alloc */
3307
+ [buffer.slice(view.byteOffset || 0, (view.byteOffset || 0) + view.byteLength)],
3308
+ /* alloc */
3309
+ { type: image.mimeType }
3310
+ );
3311
+ } else {
3312
+ const response = await fetch(
3313
+ /* alloc */
3314
+ new URL(image.uri, directory).href
3315
+ );
3316
+ if (!response.ok) throw (
3317
+ /* alloc */
3318
+ new Error(`Unable to load model texture: HTTP ${response.status}`)
3319
+ );
3320
+ blob = await response.blob();
3321
+ }
3322
+ return createImageBitmap(blob, geometryImageOptions);
3323
+ }
3324
+
3325
+ // src/models/loadModelResources.js
3326
+ async function loadModelResources(description, directory) {
3327
+ const bufferRequests = (
3328
+ /* alloc */
3329
+ []
3330
+ );
3331
+ let buffer;
3332
+ for (buffer of description.buffers || geometryEmpty) bufferRequests.push(loadBuffer(buffer, directory));
3333
+ await Promise.all(bufferRequests);
3334
+ const imageRequests = (
3335
+ /* alloc */
3336
+ []
3337
+ );
3338
+ let image;
3339
+ for (image of description.images || geometryEmpty) imageRequests.push(loadImage(description, directory, image));
3340
+ return Promise.all(imageRequests);
3341
+ }
3342
+
3343
+ // src/globals/geometrySemantics.js
3344
+ var geometrySemantics = [
3345
+ ["POSITION", "position"],
3346
+ ["NORMAL", "normal"],
3347
+ ["TEXCOORD_0", "uv"]
3348
+ ];
3349
+
3350
+ // src/globals/types.js
3351
+ var types = {
3352
+ 5120: Int8Array,
3353
+ 5121: Uint8Array,
3354
+ 5122: Int16Array,
3355
+ 5123: Uint16Array,
3356
+ 5125: Uint32Array,
3357
+ 5126: Float32Array
3358
+ };
3359
+
3360
+ // src/globals/sizes.js
3361
+ var sizes = {
3362
+ SCALAR: 1,
3363
+ VEC2: 2,
3364
+ VEC3: 3,
3365
+ VEC4: 4
3366
+ };
3367
+
3368
+ // src/models/readAccessorValue.js
3369
+ function readAccessorValue(input, Type, at) {
3370
+ return Type === Int8Array ? input.getInt8(at) : Type === Uint8Array ? input.getUint8(at) : Type === Int16Array ? input.getInt16(at, true) : Type === Uint16Array ? input.getUint16(at, true) : Type === Uint32Array ? input.getUint32(at, true) : input.getFloat32(at, true);
3371
+ }
3372
+
3373
+ // src/models/readAccessorValues.js
3374
+ function readAccessorValues(description, Type, size, data, accessorCount, indices, viewIndex, offset, count, stride) {
3375
+ const view = description.bufferViews[viewIndex], bytes = Type.BYTES_PER_ELEMENT;
3376
+ const buffer = description.buffers[view.buffer].binary;
3377
+ stride ||= size * bytes;
3378
+ if (!buffer || stride < size * bytes || offset < 0 || (view.byteOffset || 0) + view.byteLength > buffer.byteLength || (count - 1) * stride + size * bytes + offset > view.byteLength) throw (
3379
+ /* alloc */
3380
+ new Error("glTF accessor outside buffer view")
3381
+ );
3382
+ const input = (
3383
+ /* alloc */
3384
+ new DataView(buffer)
3385
+ );
3386
+ let i, j, at, output;
3387
+ for (i = 0; i < count; i++) {
3388
+ output = indices ? indices[i] : i;
3389
+ if (indices && output >= accessorCount) throw (
3390
+ /* alloc */
3391
+ new Error("Invalid sparse glTF index")
3392
+ );
3393
+ for (j = 0; j < size; j++) {
3394
+ at = (view.byteOffset || 0) + offset + i * stride + j * bytes;
3395
+ data[output * size + j] = readAccessorValue(input, Type, at);
3396
+ }
3397
+ }
3398
+ }
3399
+
3400
+ // src/models/readSparseAccessor.js
3401
+ function readSparseAccessor(description, spec, Type, size, data) {
3402
+ const sparse = spec.sparse, Index = types[sparse.indices.componentType];
3403
+ if (Index !== Uint8Array && Index !== Uint16Array && Index !== Uint32Array) {
3404
+ throw (
3405
+ /* alloc */
3406
+ new Error("Invalid sparse glTF indices")
3407
+ );
3408
+ }
3409
+ const view = description.bufferViews[sparse.indices.bufferView];
3410
+ const offset = (view.byteOffset || 0) + (sparse.indices.byteOffset || 0);
3411
+ if (sparse.count > spec.count || (sparse.indices.byteOffset || 0) + sparse.count * Index.BYTES_PER_ELEMENT > view.byteLength) {
3412
+ throw (
3413
+ /* alloc */
3414
+ new Error("Invalid sparse glTF indices")
3415
+ );
3416
+ }
3417
+ const indices = (
3418
+ /* alloc */
3419
+ new Index(description.buffers[view.buffer].binary.slice(
3420
+ offset,
3421
+ offset + sparse.count * Index.BYTES_PER_ELEMENT
3422
+ ))
3423
+ );
3424
+ readAccessorValues(
3425
+ description,
3426
+ Type,
3427
+ size,
3428
+ data,
3429
+ spec.count,
3430
+ indices,
3431
+ sparse.values.bufferView,
3432
+ sparse.values.byteOffset || 0,
3433
+ sparse.count,
3434
+ 0
3435
+ );
3436
+ }
3437
+
3438
+ // src/models/normalizeAccessor.js
3439
+ function normalizeAccessor(spec, Type, data, size) {
3440
+ const normalized = (
3441
+ /* alloc */
3442
+ new Float32Array(data.length)
3443
+ );
3444
+ let i;
3445
+ for (i = 0; i < data.length; i++) {
3446
+ normalized[i] = spec.normalized ? Math.max(-1, data[i] / (Type === Int8Array ? 127 : Type === Int16Array ? 32767 : Type === Uint8Array ? 255 : Type === Uint32Array ? 4294967295 : 65535)) : data[i];
3447
+ }
3448
+ return (
3449
+ /* alloc */
3450
+ { data: normalized, size, indices: data }
3451
+ );
3452
+ }
3453
+
3454
+ // src/models/readAccessor.js
3455
+ function readAccessor(description, index) {
3456
+ const spec = description.accessors[index], Type = types[spec.componentType], size = sizes[spec.type];
3457
+ if (!Type || !size || !Number.isInteger(spec.count) || spec.count < 0) {
3458
+ throw (
3459
+ /* alloc */
3460
+ new Error("Invalid glTF accessor")
3461
+ );
3462
+ }
3463
+ const data = (
3464
+ /* alloc */
3465
+ new Type(spec.count * size)
3466
+ );
3467
+ if (spec.bufferView !== void 0) {
3468
+ readAccessorValues(
3469
+ description,
3470
+ Type,
3471
+ size,
3472
+ data,
3473
+ spec.count,
3474
+ null,
3475
+ spec.bufferView,
3476
+ spec.byteOffset || 0,
3477
+ spec.count,
3478
+ description.bufferViews[spec.bufferView].byteStride
3479
+ );
3480
+ }
3481
+ if (spec.sparse) {
3482
+ readSparseAccessor(description, spec, Type, size, data);
3483
+ }
3484
+ if (spec.componentType === 5126) {
3485
+ return (
3486
+ /* alloc */
3487
+ { data, size }
3488
+ );
3489
+ }
3490
+ return normalizeAccessor(spec, Type, data, size);
3491
+ }
3492
+
3493
+ // src/models/readModelAttributes.js
3494
+ function readModelAttributes(description, primitive) {
3495
+ const attributes = (
3496
+ /* alloc */
3497
+ {}
3498
+ );
3499
+ let semantic, name;
3500
+ for ([semantic, name] of geometrySemantics) {
3501
+ if (primitive.attributes[semantic] !== void 0) {
3502
+ attributes[name] = readAccessor(description, primitive.attributes[semantic]);
3503
+ }
3504
+ }
3505
+ if (!attributes.position) throw (
3506
+ /* alloc */
3507
+ new Error("glTF mesh has no positions")
3508
+ );
3509
+ const vertexCount = attributes.position.data.length / 3;
3510
+ if (attributes.position.size !== 3 || vertexCount < 3) throw (
3511
+ /* alloc */
3512
+ new Error("Invalid glTF vertex attributes")
3513
+ );
3514
+ let attribute;
3515
+ for (attribute of Object.values(attributes)) {
3516
+ if (attribute.data.length / attribute.size !== vertexCount) throw (
3517
+ /* alloc */
3518
+ new Error("Invalid glTF vertex attributes")
3519
+ );
3520
+ }
3521
+ return (
3522
+ /* alloc */
3523
+ { attributes, vertexCount }
3524
+ );
3525
+ }
3526
+
3527
+ // src/models/readModelIndices.js
3528
+ function readModelIndices(description, primitive, attributes, vertexCount) {
3529
+ if (primitive.indices !== void 0) {
3530
+ const index = readAccessor(description, primitive.indices);
3531
+ attributes.index = /* alloc */
3532
+ { size: 1, data: index.indices };
3533
+ if (!(index.indices instanceof Uint8Array || index.indices instanceof Uint16Array || index.indices instanceof Uint32Array) || index.size !== 1 || index.indices.length % 3) {
3534
+ throw (
3535
+ /* alloc */
3536
+ new Error("Invalid glTF triangle indices")
3537
+ );
3538
+ }
3539
+ let value;
3540
+ for (value of index.indices) if (value >= vertexCount) throw (
3541
+ /* alloc */
3542
+ new Error("Invalid glTF triangle indices")
3543
+ );
3544
+ } else if (vertexCount % 3) {
3545
+ throw (
3546
+ /* alloc */
3547
+ new Error("Incomplete glTF triangle")
3548
+ );
3549
+ }
3550
+ }
3551
+
3552
+ // src/geometry/vertexNormals.js
3553
+ function vertexNormals(p, indices) {
3554
+ const normals = (
3555
+ /* alloc */
3556
+ new Float32Array(p.length)
3557
+ );
3558
+ let i, a, b, c, ux, uy, uz, vx, vy, vz, j, nx, ny, nz, index, length;
3559
+ for (i = 0; i < (indices?.length || p.length / 3); i += 3) {
3560
+ a = indices ? indices[i] : i, b = indices ? indices[i + 1] : i + 1, c = indices ? indices[i + 2] : i + 2;
3561
+ ux = p[b * 3] - p[a * 3], uy = p[b * 3 + 1] - p[a * 3 + 1], uz = p[b * 3 + 2] - p[a * 3 + 2];
3562
+ vx = p[c * 3] - p[a * 3], vy = p[c * 3 + 1] - p[a * 3 + 1], vz = p[c * 3 + 2] - p[a * 3 + 2];
3563
+ nx = uy * vz - uz * vy, ny = uz * vx - ux * vz, nz = ux * vy - uy * vx;
3564
+ for (j = 0; j < 3; j++) {
3565
+ index = j === 0 ? a : j === 1 ? b : c;
3566
+ normals[index * 3] += nx;
3567
+ normals[index * 3 + 1] += ny;
3568
+ normals[index * 3 + 2] += nz;
3569
+ }
3570
+ }
3571
+ for (i = 0; i < normals.length; i += 3) {
3572
+ length = Math.hypot(normals[i], normals[i + 1], normals[i + 2]) || 1;
3573
+ for (j = 0; j < 3; j++) {
3574
+ normals[i + j] /= length;
3575
+ }
3576
+ }
3577
+ return normals;
3578
+ }
3579
+
3580
+ // src/models/prepareModelPrimitive.js
3581
+ function prepareModelPrimitive(description, primitive) {
3582
+ const { attributes, vertexCount } = readModelAttributes(description, primitive);
3583
+ readModelIndices(description, primitive, attributes, vertexCount);
3584
+ if (!attributes.normal) {
3585
+ attributes.normal = /* alloc */
3586
+ {
3587
+ size: 3,
3588
+ data: vertexNormals(attributes.position.data, attributes.index?.data)
3589
+ };
3590
+ }
3591
+ if (attributes.index?.data instanceof Uint8Array) {
3592
+ attributes.index.data = Uint16Array.from(attributes.index.data);
3593
+ }
3594
+ primitive.preparedAttributes = attributes;
3595
+ return attributes;
3596
+ }
3597
+
3598
+ // src/models/prepareModelGeometry.js
3599
+ function prepareModelGeometry(description, scene) {
3600
+ const pending = scene.nodes.slice(), visited = (
3601
+ /* alloc */
3602
+ /* @__PURE__ */ new Set()
3603
+ );
3604
+ const meshes = (
3605
+ /* alloc */
3606
+ /* @__PURE__ */ new Set()
3607
+ ), transfers = (
3608
+ /* alloc */
3609
+ /* @__PURE__ */ new Set()
3610
+ );
3611
+ let index, spec, primitive, attribute, child;
3612
+ while (pending.length) {
3613
+ index = pending.pop();
3614
+ if (visited.has(index)) continue;
3615
+ visited.add(index);
3616
+ spec = description.nodes[index];
3617
+ if (!spec) throw (
3618
+ /* alloc */
3619
+ new Error("Invalid glTF node")
3620
+ );
3621
+ if (spec.mesh !== void 0) meshes.add(spec.mesh);
3622
+ for (child of spec.children || geometryEmpty) pending.push(child);
3623
+ }
3624
+ for (index of meshes) {
3625
+ for (primitive of description.meshes[index].primitives) {
3626
+ for (attribute of Object.values(prepareModelPrimitive(description, primitive))) {
3627
+ transfers.add(attribute.data.buffer);
3628
+ }
3629
+ }
3630
+ }
3631
+ return (
3632
+ /* alloc */
3633
+ [...transfers]
3634
+ );
3635
+ }
3636
+
3637
+ // src/models/prepareModel.js
3638
+ async function prepareModel(url) {
3639
+ const response = await fetch(url);
3640
+ if (!response.ok) throw (
3641
+ /* alloc */
3642
+ new Error(`Unable to load model: HTTP ${response.status}`)
3643
+ );
3644
+ const description = decodeModel(await response.arrayBuffer());
3645
+ const scene = description.scenes?.[description.scene || 0];
3646
+ if (!scene) throw (
3647
+ /* alloc */
3648
+ new Error("Missing glTF scene")
3649
+ );
3650
+ const images = await loadModelResources(
3651
+ description,
3652
+ /* alloc */
3653
+ new URL(".", url).href
3654
+ );
3655
+ const transfers = prepareModelGeometry(description, scene);
3656
+ let image;
3657
+ for (image of images) transfers.push(image);
3658
+ delete description.buffers;
3659
+ delete description.bufferViews;
3660
+ delete description.accessors;
3661
+ return (
3662
+ /* alloc */
3663
+ { description, images, transfers }
3664
+ );
3665
+ }
3666
+
3667
+ // src/workers/modelWorker.js
3668
+ async function onModelMessage({ data }) {
3669
+ try {
3670
+ const result = await prepareModel(data.url);
3671
+ self.postMessage(
3672
+ /* alloc */
3673
+ { description: result.description, images: result.images },
3674
+ result.transfers
3675
+ );
3676
+ } catch (error) {
3677
+ self.postMessage(
3678
+ /* alloc */
3679
+ { error: (
3680
+ /* alloc */
3681
+ { name: error.name, message: error.message }
3682
+ ) }
3683
+ );
3684
+ }
3685
+ }
3686
+
933
3687
  // src/workers/geometryWorker.js
934
3688
  function onGeometryMessage(event) {
3689
+ if (event.data.type === "model") {
3690
+ onModelMessage(event);
3691
+ return;
3692
+ }
3693
+ if (event.data.type === "cog") {
3694
+ onCogMessage(event);
3695
+ return;
3696
+ }
3697
+ if (event.data.type === "vector") {
3698
+ onVectorTileMessage(event);
3699
+ return;
3700
+ }
935
3701
  try {
936
3702
  const data = assembleAreaGeometry(event.data);
937
3703
  const transfers = (