@xgis/xgis3d 0.3.23
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.
- package/LICENSE +21 -0
- package/README.md +144 -0
- package/dist/Map3D.d.ts +7 -0
- package/dist/cameraBoundsGuard.d.ts +6 -0
- package/dist/cameraScale.d.ts +7 -0
- package/dist/capabilities/cesiumCamera.d.ts +17 -0
- package/dist/capabilities/cesiumEngineHealth.d.ts +6 -0
- package/dist/capabilities/cesiumViewLimits.d.ts +3 -0
- package/dist/cesiumBaseUrl.d.ts +6 -0
- package/dist/entryCamera.d.ts +20 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +1460 -0
- package/dist/index.js.map +1 -0
- package/dist/interactions/canvasPressRelay.d.ts +2 -0
- package/dist/interactions/cesiumKeyboardHandler.d.ts +5 -0
- package/dist/interactions/nadirLimit.d.ts +8 -0
- package/dist/pickGroundPoint.d.ts +5 -0
- package/dist/projection.d.ts +46 -0
- package/dist/reconcilers/useCesiumBasemapAttachments.d.ts +11 -0
- package/dist/reconcilers/useCesiumBasemapReconciler.d.ts +2 -0
- package/dist/reconcilers/useCesiumEntryCamera.d.ts +9 -0
- package/dist/reconcilers/useCesiumLayerReconciler.d.ts +2 -0
- package/dist/reconcilers/useCesiumTerrainReconciler.d.ts +2 -0
- package/dist/reconcilers/useCesiumUserMarker.d.ts +2 -0
- package/dist/reconcilers/useCesiumViewBounds.d.ts +3 -0
- package/dist/utils/create3DTilesetLayer.d.ts +10 -0
- package/dist/utils/createKMLLayer.d.ts +9 -0
- package/dist/utils/createOGCMapsLayer.d.ts +3 -0
- package/dist/utils/createOGCTilesLayer.d.ts +3 -0
- package/dist/utils/createTMSLayer.d.ts +3 -0
- package/dist/utils/createWMSLayer.d.ts +3 -0
- package/dist/utils/createWMTSLayer.d.ts +3 -0
- package/dist/utils/kmlEntityOpacity.d.ts +15 -0
- package/dist/utils/kmlEntityStyle.d.ts +4 -0
- package/dist/utils/layerCreation.d.ts +15 -0
- package/dist/utils/safeMinimumLevel.d.ts +3 -0
- package/package.json +62 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,1460 @@
|
|
|
1
|
+
import { jsx } from 'react/jsx-runtime';
|
|
2
|
+
import 'cesium/Build/Cesium/Widgets/widgets.css';
|
|
3
|
+
import { epsg3301ToWgs84 as epsg3301ToWgs84$1, resolutionForZoom, wrapToRange, TWO_PI, ZOOM_0_RESOLUTION, wgs84ToEpsg3301 as wgs84ToEpsg3301$1, asExtent3301, RADIANS_TO_DEGREES, extentCenter, zoomForExtent, createEngineErrorChannel, WEBGL_CONTEXT_LOST_ERROR_NAME, zoomToFit, clampExtentToBounds, MIN_ZOOM, KML_DEFAULT_COLOR, KML_LABEL_OUTLINE_COLOR, kmlExtractStyles, KML_LABEL_FONT, KML_MARKER_SIZE_PX, KML_LABEL_OUTLINE_PX, KML_LABEL_FONT_SIZE_PX, ogcMapsCesiumCrs, withQuery, ogcMapsMapUrl, ogcMapsQuery, parseTmsTileMap, tmsCesiumSchemeFor, tmsLevelRange, tmsTileBase, tmsTileSuffix, parseWmsCaps, parseWmtsCapabilities, toAlpha, useBasemapStore, useActiveBasemapPlan, useBasemapServiceError, useMapViewStore, useTerrainStore, useUserMarkerStore, MAX_ZOOM } from '@xgis/xgis-common';
|
|
4
|
+
import { Cartesian3, HeadingPitchRange, Matrix4, Rectangle, Cartesian2, Transforms, Matrix3, Math as Math$1, EllipsoidTerrainProvider, Ellipsoid, Cartographic, sampleTerrainMostDetailed, Cesium3DTileStyle, I3SDataProvider, Cesium3DTileset, Color, ImageMaterialProperty, ColorMaterialProperty, ConstantProperty, Iso8601, ArcType, PointGraphics, Resource, HeightReference, PolygonHierarchy, KmlDataSource, UrlTemplateImageryProvider, WebMercatorTilingScheme, GeographicTilingScheme, WebMercatorProjection, WebMapServiceImageryProvider, WebMapTileServiceImageryProvider, CesiumTerrainProvider, ConstantPositionProperty } from 'cesium';
|
|
5
|
+
import { useRef, useMemo, useEffect, useState, useCallback } from 'react';
|
|
6
|
+
import { Viewer } from 'resium';
|
|
7
|
+
|
|
8
|
+
function ensureCesiumBaseUrl() {
|
|
9
|
+
if (typeof window === "undefined") {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
if (window.CESIUM_BASE_URL) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
window.CESIUM_BASE_URL = `/cesium-${"1.143.0"}`;
|
|
16
|
+
}
|
|
17
|
+
ensureCesiumBaseUrl();
|
|
18
|
+
|
|
19
|
+
function epsg3301ToWgs84(x, y) {
|
|
20
|
+
const { lon, lat } = epsg3301ToWgs84$1(x, y);
|
|
21
|
+
return [lon, lat];
|
|
22
|
+
}
|
|
23
|
+
function wgs84ToEpsg3301(lon, lat) {
|
|
24
|
+
return wgs84ToEpsg3301$1({ lon, lat });
|
|
25
|
+
}
|
|
26
|
+
function fovSpanPx(viewportWidthPx, viewportHeightPx) {
|
|
27
|
+
return Math.max(viewportWidthPx, viewportHeightPx);
|
|
28
|
+
}
|
|
29
|
+
function zoomToRange(zoom, spanPx, fov) {
|
|
30
|
+
return resolutionForZoom(zoom) * spanPx / (2 * Math.tan(fov / 2));
|
|
31
|
+
}
|
|
32
|
+
function rangeToZoom(range, spanPx, fov) {
|
|
33
|
+
const resolution = 2 * range * Math.tan(fov / 2) / spanPx;
|
|
34
|
+
return Math.max(0, Math.log2(ZOOM_0_RESOLUTION / resolution));
|
|
35
|
+
}
|
|
36
|
+
function focusRangeForZoom(height, focusRange, maxRangeFactor) {
|
|
37
|
+
const maxRange = height * maxRangeFactor;
|
|
38
|
+
if (focusRange === void 0) {
|
|
39
|
+
return maxRange;
|
|
40
|
+
}
|
|
41
|
+
return Math.min(focusRange, maxRange);
|
|
42
|
+
}
|
|
43
|
+
function flipAngleDirection(radians) {
|
|
44
|
+
return wrapToRange(-radians, TWO_PI);
|
|
45
|
+
}
|
|
46
|
+
const cesiumHeadingToRotation = flipAngleDirection;
|
|
47
|
+
const rotationToCesiumHeading = flipAngleDirection;
|
|
48
|
+
function cameraFallbackExtent3301(centerLon, centerLat, zoom, viewportWidthPx, viewportHeightPx) {
|
|
49
|
+
const [cx, cy] = wgs84ToEpsg3301(centerLon, centerLat);
|
|
50
|
+
const resolution = resolutionForZoom(zoom);
|
|
51
|
+
const halfWidth = resolution * viewportWidthPx / 2;
|
|
52
|
+
const halfHeight = resolution * viewportHeightPx / 2;
|
|
53
|
+
return [cx - halfWidth, cy - halfHeight, cx + halfWidth, cy + halfHeight];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const DEFAULT_FOV = Math.PI / 3;
|
|
57
|
+
const FALLBACK_SPAN_PX = 1e3;
|
|
58
|
+
function opticsOf(viewer) {
|
|
59
|
+
const canvas = viewer.scene.canvas;
|
|
60
|
+
const frustum = viewer.camera.frustum;
|
|
61
|
+
const fov = "fov" in frustum && frustum.fov !== void 0 ? frustum.fov : DEFAULT_FOV;
|
|
62
|
+
const spanPx = fovSpanPx(canvas.clientWidth, canvas.clientHeight);
|
|
63
|
+
return { spanPx: spanPx > 0 ? spanPx : FALLBACK_SPAN_PX, fov };
|
|
64
|
+
}
|
|
65
|
+
function rangeForZoom(viewer, zoom) {
|
|
66
|
+
const { spanPx, fov } = opticsOf(viewer);
|
|
67
|
+
return zoomToRange(zoom, spanPx, fov);
|
|
68
|
+
}
|
|
69
|
+
function zoomForRange(viewer, range) {
|
|
70
|
+
const { spanPx, fov } = opticsOf(viewer);
|
|
71
|
+
return rangeToZoom(range, spanPx, fov);
|
|
72
|
+
}
|
|
73
|
+
function reportedZoomGrid(zoom) {
|
|
74
|
+
return Math.round(zoom * 10) / 10;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function pickGroundPoint(viewer, pixel) {
|
|
78
|
+
const scene = viewer.scene;
|
|
79
|
+
const pickRay = viewer.camera.getPickRay(pixel);
|
|
80
|
+
const terrainPoint = pickRay ? scene.globe.pick(pickRay, scene) : void 0;
|
|
81
|
+
const point = terrainPoint ?? viewer.camera.pickEllipsoid(pixel, scene.globe.ellipsoid);
|
|
82
|
+
return point ? { point, onTerrain: terrainPoint !== void 0 } : void 0;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const MAX_FOCUS_RANGE_FACTOR = 10;
|
|
86
|
+
function jumpTargetHeight(focus) {
|
|
87
|
+
return focus?.height ?? 0;
|
|
88
|
+
}
|
|
89
|
+
function cameraFocusFromPoint(viewer, point, onTerrain) {
|
|
90
|
+
const carto = Cartographic.fromCartesian(point);
|
|
91
|
+
return {
|
|
92
|
+
lon: carto.longitude * RADIANS_TO_DEGREES,
|
|
93
|
+
lat: carto.latitude * RADIANS_TO_DEGREES,
|
|
94
|
+
height: carto.height,
|
|
95
|
+
range: Cartesian3.distance(viewer.camera.position, point),
|
|
96
|
+
onTerrain
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
function screenCenterGroundFocus(viewer) {
|
|
100
|
+
const canvas = viewer.scene.canvas;
|
|
101
|
+
const centerPx = new Cartesian2(canvas.clientWidth / 2, canvas.clientHeight / 2);
|
|
102
|
+
const picked = pickGroundPoint(viewer, centerPx);
|
|
103
|
+
return picked ? cameraFocusFromPoint(viewer, picked.point, picked.onTerrain) : void 0;
|
|
104
|
+
}
|
|
105
|
+
function focusIsUnmeasurable(viewer, focus) {
|
|
106
|
+
if (!focus || focus.onTerrain) {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
return !(viewer.terrainProvider instanceof EllipsoidTerrainProvider);
|
|
110
|
+
}
|
|
111
|
+
function headingPitchAtFocus(camera, lon, lat) {
|
|
112
|
+
const enuRotation = Matrix4.getMatrix3(
|
|
113
|
+
Transforms.eastNorthUpToFixedFrame(Cartesian3.fromDegrees(lon, lat)),
|
|
114
|
+
new Matrix3()
|
|
115
|
+
);
|
|
116
|
+
const localRotation = Matrix3.transpose(enuRotation, new Matrix3());
|
|
117
|
+
const localDirection = Matrix3.multiplyByVector(
|
|
118
|
+
localRotation,
|
|
119
|
+
camera.directionWC,
|
|
120
|
+
new Cartesian3()
|
|
121
|
+
);
|
|
122
|
+
const localUp = Matrix3.multiplyByVector(localRotation, camera.upWC, new Cartesian3());
|
|
123
|
+
const pitch = Math$1.PI_OVER_TWO - Math$1.acosClamped(localDirection.z);
|
|
124
|
+
const nearVertical = Math$1.equalsEpsilon(
|
|
125
|
+
Math.abs(localDirection.z),
|
|
126
|
+
1,
|
|
127
|
+
Math$1.EPSILON3
|
|
128
|
+
);
|
|
129
|
+
const rawHeading = nearVertical ? Math.atan2(localUp.y, localUp.x) - Math$1.PI_OVER_TWO : Math.atan2(localDirection.y, localDirection.x) - Math$1.PI_OVER_TWO;
|
|
130
|
+
const heading = Math$1.TWO_PI - Math$1.zeroToTwoPi(rawHeading);
|
|
131
|
+
return { heading, pitch };
|
|
132
|
+
}
|
|
133
|
+
function orientationAtFocus(camera, focus) {
|
|
134
|
+
if (!focus) {
|
|
135
|
+
return { heading: camera.heading, pitch: camera.pitch };
|
|
136
|
+
}
|
|
137
|
+
return headingPitchAtFocus(camera, focus.lon, focus.lat);
|
|
138
|
+
}
|
|
139
|
+
function focusAtHeight(viewer, height) {
|
|
140
|
+
const scene = viewer.scene;
|
|
141
|
+
const canvas = scene.canvas;
|
|
142
|
+
const centerPx = new Cartesian2(canvas.clientWidth / 2, canvas.clientHeight / 2);
|
|
143
|
+
const base = scene.globe.ellipsoid ?? Ellipsoid.default;
|
|
144
|
+
const point = viewer.camera.pickEllipsoid(
|
|
145
|
+
centerPx,
|
|
146
|
+
new Ellipsoid(base.radii.x + height, base.radii.y + height, base.radii.z + height)
|
|
147
|
+
);
|
|
148
|
+
if (!point) {
|
|
149
|
+
return void 0;
|
|
150
|
+
}
|
|
151
|
+
const carto = Cartographic.fromCartesian(point);
|
|
152
|
+
return {
|
|
153
|
+
lon: carto.longitude * RADIANS_TO_DEGREES,
|
|
154
|
+
lat: carto.latitude * RADIANS_TO_DEGREES,
|
|
155
|
+
height,
|
|
156
|
+
range: Cartesian3.distance(viewer.camera.position, point),
|
|
157
|
+
onTerrain: false
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
const ROTATION_EPSILON = 1e-4;
|
|
161
|
+
function createCesiumCameraCapability(viewer) {
|
|
162
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
163
|
+
const rotationListeners = /* @__PURE__ */ new Set();
|
|
164
|
+
viewer.camera.percentageChanged = 0.01;
|
|
165
|
+
let lastRotation;
|
|
166
|
+
let lastTerrainHeight;
|
|
167
|
+
function focusForPlacement() {
|
|
168
|
+
const focus = screenCenterGroundFocus(viewer);
|
|
169
|
+
if (!focus) {
|
|
170
|
+
return void 0;
|
|
171
|
+
}
|
|
172
|
+
if (focus.onTerrain) {
|
|
173
|
+
lastTerrainHeight = focus.height;
|
|
174
|
+
return focus;
|
|
175
|
+
}
|
|
176
|
+
if (lastTerrainHeight === void 0) {
|
|
177
|
+
return focus;
|
|
178
|
+
}
|
|
179
|
+
return focusAtHeight(viewer, lastTerrainHeight) ?? focus;
|
|
180
|
+
}
|
|
181
|
+
let settleDropped = false;
|
|
182
|
+
function fireRotation(rotation) {
|
|
183
|
+
if (lastRotation !== void 0 && Math.abs(rotation - lastRotation) < ROTATION_EPSILON) {
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
lastRotation = rotation;
|
|
187
|
+
rotationListeners.forEach((cb) => cb(rotation));
|
|
188
|
+
}
|
|
189
|
+
function emitChange(focus, heading, pitch) {
|
|
190
|
+
const camera = viewer.camera;
|
|
191
|
+
const height = camera.positionCartographic.height;
|
|
192
|
+
const range = focusRangeForZoom(height, focus?.range, MAX_FOCUS_RANGE_FACTOR);
|
|
193
|
+
const zoom = reportedZoomGrid(zoomForRange(viewer, range));
|
|
194
|
+
if (!Number.isFinite(zoom)) {
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
const change = {
|
|
198
|
+
zoom,
|
|
199
|
+
rotation: cesiumHeadingToRotation(heading),
|
|
200
|
+
pitch
|
|
201
|
+
};
|
|
202
|
+
const canvas = viewer.scene.canvas;
|
|
203
|
+
const [centerLon, centerLat] = focus ? [focus.lon, focus.lat] : [
|
|
204
|
+
camera.positionCartographic.longitude * RADIANS_TO_DEGREES,
|
|
205
|
+
camera.positionCartographic.latitude * RADIANS_TO_DEGREES
|
|
206
|
+
];
|
|
207
|
+
change.bbox = asExtent3301(
|
|
208
|
+
cameraFallbackExtent3301(centerLon, centerLat, zoom, canvas.clientWidth, canvas.clientHeight)
|
|
209
|
+
);
|
|
210
|
+
settleDropped = false;
|
|
211
|
+
listeners.forEach((cb) => cb(change));
|
|
212
|
+
}
|
|
213
|
+
function fireChange(focus, heading, pitch) {
|
|
214
|
+
if (focusIsUnmeasurable(viewer, focus)) {
|
|
215
|
+
settleDropped = true;
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
emitChange(focus, heading, pitch);
|
|
219
|
+
}
|
|
220
|
+
function currentView() {
|
|
221
|
+
const camera = viewer.camera;
|
|
222
|
+
const focus = screenCenterGroundFocus(viewer);
|
|
223
|
+
const { heading, pitch } = focus ? headingPitchAtFocus(camera, focus.lon, focus.lat) : { heading: camera.heading, pitch: camera.pitch };
|
|
224
|
+
return { focus, heading, pitch };
|
|
225
|
+
}
|
|
226
|
+
viewer.camera.changed.addEventListener(() => {
|
|
227
|
+
const { focus, heading, pitch } = currentView();
|
|
228
|
+
fireRotation(cesiumHeadingToRotation(heading));
|
|
229
|
+
fireChange(focus, heading, pitch);
|
|
230
|
+
});
|
|
231
|
+
viewer.scene.postRender.addEventListener(() => {
|
|
232
|
+
if (!settleDropped || !viewer.scene.globe.tilesLoaded) {
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
const { focus, heading, pitch } = currentView();
|
|
236
|
+
emitChange(focus, heading, pitch);
|
|
237
|
+
});
|
|
238
|
+
return {
|
|
239
|
+
flyTo(extent) {
|
|
240
|
+
const [west, south] = epsg3301ToWgs84(extent[0], extent[1]);
|
|
241
|
+
const [east, north] = epsg3301ToWgs84(extent[2], extent[3]);
|
|
242
|
+
viewer.camera.flyTo({
|
|
243
|
+
destination: Rectangle.fromDegrees(west, south, east, north),
|
|
244
|
+
duration: 0
|
|
245
|
+
});
|
|
246
|
+
},
|
|
247
|
+
fitExtent(extent) {
|
|
248
|
+
const camera = viewer.camera;
|
|
249
|
+
const canvas = viewer.scene.canvas;
|
|
250
|
+
const [cx, cy] = extentCenter(extent);
|
|
251
|
+
const [lon, lat] = epsg3301ToWgs84(cx, cy);
|
|
252
|
+
const focus = focusForPlacement();
|
|
253
|
+
const { heading, pitch } = orientationAtFocus(camera, focus);
|
|
254
|
+
const zoom = zoomForExtent(extent, canvas.clientWidth, canvas.clientHeight);
|
|
255
|
+
camera.lookAt(
|
|
256
|
+
Cartesian3.fromDegrees(lon, lat, jumpTargetHeight(focus)),
|
|
257
|
+
new HeadingPitchRange(heading, pitch, rangeForZoom(viewer, zoom))
|
|
258
|
+
);
|
|
259
|
+
camera.lookAtTransform(Matrix4.IDENTITY);
|
|
260
|
+
},
|
|
261
|
+
setExtent(extent) {
|
|
262
|
+
const camera = viewer.camera;
|
|
263
|
+
const [cx, cy] = extentCenter(extent);
|
|
264
|
+
const [lon, lat] = epsg3301ToWgs84(cx, cy);
|
|
265
|
+
const focus = focusForPlacement();
|
|
266
|
+
const { heading, pitch } = orientationAtFocus(camera, focus);
|
|
267
|
+
camera.lookAt(
|
|
268
|
+
Cartesian3.fromDegrees(lon, lat, jumpTargetHeight(focus)),
|
|
269
|
+
new HeadingPitchRange(heading, pitch, focus?.range ?? camera.positionCartographic.height)
|
|
270
|
+
);
|
|
271
|
+
camera.lookAtTransform(Matrix4.IDENTITY);
|
|
272
|
+
},
|
|
273
|
+
setZoom(zoom) {
|
|
274
|
+
const camera = viewer.camera;
|
|
275
|
+
const range = rangeForZoom(viewer, zoom);
|
|
276
|
+
const focus = focusForPlacement();
|
|
277
|
+
if (!focus) {
|
|
278
|
+
camera.setView({
|
|
279
|
+
destination: Cartesian3.fromRadians(
|
|
280
|
+
camera.positionCartographic.longitude,
|
|
281
|
+
camera.positionCartographic.latitude,
|
|
282
|
+
range
|
|
283
|
+
),
|
|
284
|
+
orientation: { heading: camera.heading, pitch: camera.pitch, roll: camera.roll }
|
|
285
|
+
});
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
const { heading, pitch } = orientationAtFocus(camera, focus);
|
|
289
|
+
camera.lookAt(
|
|
290
|
+
Cartesian3.fromDegrees(focus.lon, focus.lat, focus.height),
|
|
291
|
+
new HeadingPitchRange(heading, pitch, range)
|
|
292
|
+
);
|
|
293
|
+
camera.lookAtTransform(Matrix4.IDENTITY);
|
|
294
|
+
},
|
|
295
|
+
setRotation(rotation) {
|
|
296
|
+
const camera = viewer.camera;
|
|
297
|
+
const heading = rotationToCesiumHeading(rotation);
|
|
298
|
+
const focus = focusForPlacement();
|
|
299
|
+
if (!focus) {
|
|
300
|
+
camera.setView({ orientation: { heading, pitch: camera.pitch, roll: 0 } });
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
camera.lookAt(
|
|
304
|
+
Cartesian3.fromDegrees(focus.lon, focus.lat, focus.height),
|
|
305
|
+
new HeadingPitchRange(heading, orientationAtFocus(camera, focus).pitch, focus.range)
|
|
306
|
+
);
|
|
307
|
+
camera.lookAtTransform(Matrix4.IDENTITY);
|
|
308
|
+
},
|
|
309
|
+
setPitch(pitch) {
|
|
310
|
+
const camera = viewer.camera;
|
|
311
|
+
const focus = focusForPlacement();
|
|
312
|
+
if (!focus) {
|
|
313
|
+
camera.setView({
|
|
314
|
+
destination: camera.position,
|
|
315
|
+
orientation: { heading: camera.heading, pitch, roll: 0 }
|
|
316
|
+
});
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
camera.lookAt(
|
|
320
|
+
Cartesian3.fromDegrees(focus.lon, focus.lat, focus.height),
|
|
321
|
+
new HeadingPitchRange(orientationAtFocus(camera, focus).heading, pitch, focus.range)
|
|
322
|
+
);
|
|
323
|
+
camera.lookAtTransform(Matrix4.IDENTITY);
|
|
324
|
+
},
|
|
325
|
+
zoomTo(coord, zoom) {
|
|
326
|
+
const camera = viewer.camera;
|
|
327
|
+
const [lon, lat] = epsg3301ToWgs84(coord[0], coord[1]);
|
|
328
|
+
const focus = focusForPlacement();
|
|
329
|
+
const { heading, pitch } = orientationAtFocus(camera, focus);
|
|
330
|
+
camera.lookAt(
|
|
331
|
+
Cartesian3.fromDegrees(lon, lat, jumpTargetHeight(focus)),
|
|
332
|
+
new HeadingPitchRange(heading, pitch, rangeForZoom(viewer, zoom))
|
|
333
|
+
);
|
|
334
|
+
camera.lookAtTransform(Matrix4.IDENTITY);
|
|
335
|
+
},
|
|
336
|
+
flyToPosition(position, zoom) {
|
|
337
|
+
const camera = viewer.camera;
|
|
338
|
+
const focus = focusForPlacement();
|
|
339
|
+
const { heading, pitch } = orientationAtFocus(camera, focus);
|
|
340
|
+
const newHeight = jumpTargetHeight(focus) + rangeForZoom(viewer, zoom);
|
|
341
|
+
camera.flyTo({
|
|
342
|
+
destination: Cartesian3.fromDegrees(position.lon, position.lat, newHeight),
|
|
343
|
+
orientation: { heading, pitch, roll: 0 },
|
|
344
|
+
duration: 1
|
|
345
|
+
});
|
|
346
|
+
},
|
|
347
|
+
getCenter() {
|
|
348
|
+
const carto = viewer.camera.positionCartographic;
|
|
349
|
+
return [carto.longitude * RADIANS_TO_DEGREES, carto.latitude * RADIANS_TO_DEGREES];
|
|
350
|
+
},
|
|
351
|
+
getBbox() {
|
|
352
|
+
return asExtent3301([282560, 6375274, 751542, 6658862]);
|
|
353
|
+
},
|
|
354
|
+
onViewChange(callback) {
|
|
355
|
+
listeners.add(callback);
|
|
356
|
+
return () => {
|
|
357
|
+
listeners.delete(callback);
|
|
358
|
+
};
|
|
359
|
+
},
|
|
360
|
+
onRotationChange(callback) {
|
|
361
|
+
rotationListeners.add(callback);
|
|
362
|
+
return () => {
|
|
363
|
+
rotationListeners.delete(callback);
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
function createCesiumEngineHealth(viewer) {
|
|
370
|
+
const channel = createEngineErrorChannel();
|
|
371
|
+
function handleContextLost() {
|
|
372
|
+
const error = new Error("WebGL context lost");
|
|
373
|
+
error.name = WEBGL_CONTEXT_LOST_ERROR_NAME;
|
|
374
|
+
channel.reportError(error, { severity: "fatal" });
|
|
375
|
+
}
|
|
376
|
+
function handleRenderError(_scene, error) {
|
|
377
|
+
channel.reportError(error instanceof Error ? error : new Error(String(error)), {
|
|
378
|
+
severity: "fatal"
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
viewer.canvas.addEventListener("webglcontextlost", handleContextLost);
|
|
382
|
+
viewer.scene.renderError.addEventListener(handleRenderError);
|
|
383
|
+
return {
|
|
384
|
+
reportError: channel.reportError,
|
|
385
|
+
onError: channel.onError,
|
|
386
|
+
dispose() {
|
|
387
|
+
viewer.canvas.removeEventListener("webglcontextlost", handleContextLost);
|
|
388
|
+
viewer.scene.renderError.removeEventListener(handleRenderError);
|
|
389
|
+
}
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
const MOVE_EPSILON_METERS = 1;
|
|
394
|
+
function boundsFloor(viewer, bounds) {
|
|
395
|
+
const canvas = viewer.scene.canvas;
|
|
396
|
+
return zoomToFit(bounds, { width: canvas.clientWidth, height: canvas.clientHeight });
|
|
397
|
+
}
|
|
398
|
+
function createCameraBoundsGuard(viewer, bounds) {
|
|
399
|
+
let lastPosition;
|
|
400
|
+
let lastCanvasKey = "";
|
|
401
|
+
function clampToBounds() {
|
|
402
|
+
const camera = viewer.camera;
|
|
403
|
+
const canvas = viewer.scene.canvas;
|
|
404
|
+
const canvasKey = `${canvas.clientWidth}x${canvas.clientHeight}`;
|
|
405
|
+
const settled = lastPosition && Cartesian3.equals(camera.position, lastPosition) && canvasKey === lastCanvasKey;
|
|
406
|
+
if (settled) {
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
lastPosition = Cartesian3.clone(camera.position, lastPosition);
|
|
410
|
+
lastCanvasKey = canvasKey;
|
|
411
|
+
const focus = screenCenterGroundFocus(viewer);
|
|
412
|
+
const [lon, lat] = focus ? [focus.lon, focus.lat] : [
|
|
413
|
+
camera.positionCartographic.longitude * RADIANS_TO_DEGREES,
|
|
414
|
+
camera.positionCartographic.latitude * RADIANS_TO_DEGREES
|
|
415
|
+
];
|
|
416
|
+
const viewport = { width: canvas.clientWidth, height: canvas.clientHeight };
|
|
417
|
+
const currentRange = focus ? focus.range : camera.positionCartographic.height * MAX_FOCUS_RANGE_FACTOR;
|
|
418
|
+
const range = Math.min(currentRange, rangeForZoom(viewer, boundsFloor(viewer, bounds)));
|
|
419
|
+
const footprint = asExtent3301(
|
|
420
|
+
cameraFallbackExtent3301(
|
|
421
|
+
lon,
|
|
422
|
+
lat,
|
|
423
|
+
zoomForRange(viewer, range),
|
|
424
|
+
viewport.width,
|
|
425
|
+
viewport.height
|
|
426
|
+
)
|
|
427
|
+
);
|
|
428
|
+
const [x, y] = wgs84ToEpsg3301(lon, lat);
|
|
429
|
+
const [targetX, targetY] = extentCenter(clampExtentToBounds(footprint, bounds));
|
|
430
|
+
if (Math.abs(targetX - x) < MOVE_EPSILON_METERS && Math.abs(targetY - y) < MOVE_EPSILON_METERS && currentRange - range < MOVE_EPSILON_METERS) {
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
433
|
+
const [targetLon, targetLat] = epsg3301ToWgs84(targetX, targetY);
|
|
434
|
+
if (focus) {
|
|
435
|
+
const { heading, pitch } = headingPitchAtFocus(camera, focus.lon, focus.lat);
|
|
436
|
+
camera.lookAt(
|
|
437
|
+
Cartesian3.fromDegrees(targetLon, targetLat),
|
|
438
|
+
new HeadingPitchRange(heading, pitch, range)
|
|
439
|
+
);
|
|
440
|
+
camera.lookAtTransform(Matrix4.IDENTITY);
|
|
441
|
+
} else {
|
|
442
|
+
camera.setView({
|
|
443
|
+
destination: Cartesian3.fromDegrees(targetLon, targetLat, range / MAX_FOCUS_RANGE_FACTOR),
|
|
444
|
+
orientation: { heading: camera.heading, pitch: camera.pitch, roll: camera.roll }
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
lastPosition = Cartesian3.clone(camera.position, lastPosition);
|
|
448
|
+
}
|
|
449
|
+
return viewer.scene.preRender.addEventListener(clampToBounds);
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
function createCesiumViewLimitsCapability(viewer) {
|
|
453
|
+
return {
|
|
454
|
+
getZoomFloor(bounds) {
|
|
455
|
+
if (!bounds) {
|
|
456
|
+
return MIN_ZOOM;
|
|
457
|
+
}
|
|
458
|
+
return reportedZoomGrid(boundsFloor(viewer, bounds));
|
|
459
|
+
}
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
const MAX_ENTRY_PITCH = Math$1.toRadians(-5);
|
|
464
|
+
const ELLIPSOID_HEIGHT = 0;
|
|
465
|
+
function entryTarget(viewer, view) {
|
|
466
|
+
const [centerX, centerY] = extentCenter(view.bbox);
|
|
467
|
+
const [lon, lat] = epsg3301ToWgs84(centerX, centerY);
|
|
468
|
+
return {
|
|
469
|
+
lon,
|
|
470
|
+
lat,
|
|
471
|
+
heading: rotationToCesiumHeading(view.rotation),
|
|
472
|
+
pitch: Math.min(view.pitch, MAX_ENTRY_PITCH),
|
|
473
|
+
range: rangeForZoom(viewer, view.zoom)
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
function placeEntryCamera(viewer, target, height) {
|
|
477
|
+
viewer.camera.lookAt(
|
|
478
|
+
Cartesian3.fromDegrees(target.lon, target.lat, height),
|
|
479
|
+
new HeadingPitchRange(target.heading, target.pitch, target.range)
|
|
480
|
+
);
|
|
481
|
+
viewer.camera.lookAtTransform(Matrix4.IDENTITY);
|
|
482
|
+
}
|
|
483
|
+
function raiseEntryCameraToTerrain(viewer, target, height) {
|
|
484
|
+
const camera = viewer.camera;
|
|
485
|
+
const ellipsoidPoint = Cartesian3.fromDegrees(target.lon, target.lat, ELLIPSOID_HEIGHT);
|
|
486
|
+
const range = Cartesian3.distance(camera.position, ellipsoidPoint);
|
|
487
|
+
const { heading, pitch } = headingPitchAtFocus(camera, target.lon, target.lat);
|
|
488
|
+
camera.lookAt(
|
|
489
|
+
Cartesian3.fromDegrees(target.lon, target.lat, height),
|
|
490
|
+
new HeadingPitchRange(heading, pitch, range)
|
|
491
|
+
);
|
|
492
|
+
camera.lookAtTransform(Matrix4.IDENTITY);
|
|
493
|
+
}
|
|
494
|
+
async function sampleEntryHeight(provider, target) {
|
|
495
|
+
if (provider instanceof EllipsoidTerrainProvider) {
|
|
496
|
+
return void 0;
|
|
497
|
+
}
|
|
498
|
+
const [sampled] = await sampleTerrainMostDetailed(
|
|
499
|
+
provider,
|
|
500
|
+
[Cartographic.fromDegrees(target.lon, target.lat)],
|
|
501
|
+
true
|
|
502
|
+
);
|
|
503
|
+
return sampled?.height;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
function createCanvasPressRelay(viewer) {
|
|
507
|
+
const canvas = viewer.canvas;
|
|
508
|
+
function relayAsMouseDown(event) {
|
|
509
|
+
if (!event.defaultPrevented) {
|
|
510
|
+
return;
|
|
511
|
+
}
|
|
512
|
+
canvas.dispatchEvent(
|
|
513
|
+
new MouseEvent("mousedown", {
|
|
514
|
+
bubbles: true,
|
|
515
|
+
clientX: event.clientX,
|
|
516
|
+
clientY: event.clientY,
|
|
517
|
+
button: event.button
|
|
518
|
+
})
|
|
519
|
+
);
|
|
520
|
+
}
|
|
521
|
+
canvas.addEventListener("pointerdown", relayAsMouseDown);
|
|
522
|
+
return () => {
|
|
523
|
+
canvas.removeEventListener("pointerdown", relayAsMouseDown);
|
|
524
|
+
};
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
const KEYBOARD_MOVE_DIVISOR = 10;
|
|
528
|
+
function horizontalDirection(vec, normal) {
|
|
529
|
+
const [vecX, vecY, vecZ] = vec;
|
|
530
|
+
const [normalX, normalY, normalZ] = normal;
|
|
531
|
+
const dot = vecX * normalX + vecY * normalY + vecZ * normalZ;
|
|
532
|
+
const hX = vecX - dot * normalX;
|
|
533
|
+
const hY = vecY - dot * normalY;
|
|
534
|
+
const hZ = vecZ - dot * normalZ;
|
|
535
|
+
const len = Math.hypot(hX, hY, hZ);
|
|
536
|
+
if (len < 1e-6) {
|
|
537
|
+
return null;
|
|
538
|
+
}
|
|
539
|
+
return [hX / len, hY / len, hZ / len];
|
|
540
|
+
}
|
|
541
|
+
function createCesiumKeyboardHandler(viewer) {
|
|
542
|
+
const canvas = viewer.canvas;
|
|
543
|
+
canvas.setAttribute("tabindex", "0");
|
|
544
|
+
function focusCanvas() {
|
|
545
|
+
canvas.focus();
|
|
546
|
+
}
|
|
547
|
+
function panForward(amount) {
|
|
548
|
+
const camera = viewer.camera;
|
|
549
|
+
const normal = viewer.scene.globe.ellipsoid.geodeticSurfaceNormal(
|
|
550
|
+
camera.positionWC,
|
|
551
|
+
new Cartesian3()
|
|
552
|
+
);
|
|
553
|
+
const forward = horizontalDirection(
|
|
554
|
+
[camera.up.x, camera.up.y, camera.up.z],
|
|
555
|
+
[normal.x, normal.y, normal.z]
|
|
556
|
+
);
|
|
557
|
+
if (!forward) {
|
|
558
|
+
return;
|
|
559
|
+
}
|
|
560
|
+
const [forwardX, forwardY, forwardZ] = forward;
|
|
561
|
+
camera.move(new Cartesian3(forwardX, forwardY, forwardZ), amount);
|
|
562
|
+
}
|
|
563
|
+
function onKeyDown(event) {
|
|
564
|
+
const camera = viewer.camera;
|
|
565
|
+
const moveRate = camera.positionCartographic.height / KEYBOARD_MOVE_DIVISOR;
|
|
566
|
+
switch (event.key) {
|
|
567
|
+
case "ArrowUp":
|
|
568
|
+
panForward(moveRate);
|
|
569
|
+
break;
|
|
570
|
+
case "ArrowDown":
|
|
571
|
+
panForward(-moveRate);
|
|
572
|
+
break;
|
|
573
|
+
case "ArrowLeft":
|
|
574
|
+
camera.moveLeft(moveRate);
|
|
575
|
+
break;
|
|
576
|
+
case "ArrowRight":
|
|
577
|
+
camera.moveRight(moveRate);
|
|
578
|
+
break;
|
|
579
|
+
case "+":
|
|
580
|
+
case "=":
|
|
581
|
+
camera.zoomIn(moveRate);
|
|
582
|
+
break;
|
|
583
|
+
case "-":
|
|
584
|
+
camera.zoomOut(moveRate);
|
|
585
|
+
break;
|
|
586
|
+
default:
|
|
587
|
+
return;
|
|
588
|
+
}
|
|
589
|
+
event.preventDefault();
|
|
590
|
+
}
|
|
591
|
+
canvas.addEventListener("click", focusCanvas);
|
|
592
|
+
canvas.addEventListener("keydown", onKeyDown);
|
|
593
|
+
return () => {
|
|
594
|
+
canvas.removeEventListener("click", focusCanvas);
|
|
595
|
+
canvas.removeEventListener("keydown", onKeyDown);
|
|
596
|
+
};
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
const DOWN = new Cartesian3(0, 0, -1);
|
|
600
|
+
function angleToNadir(camera) {
|
|
601
|
+
const angle = Math$1.acosClamped(Cartesian3.dot(camera.direction, DOWN));
|
|
602
|
+
return camera.up.z >= 0 ? angle : -angle;
|
|
603
|
+
}
|
|
604
|
+
function createNadirLimit(viewer) {
|
|
605
|
+
const camera = viewer.camera;
|
|
606
|
+
const rotateUp = camera.rotateUp.bind(camera);
|
|
607
|
+
camera.rotateUp = (angle) => {
|
|
608
|
+
const requested = angle ?? camera.defaultRotateAmount;
|
|
609
|
+
if (Matrix4.equals(camera.transform, Matrix4.IDENTITY)) {
|
|
610
|
+
rotateUp(requested);
|
|
611
|
+
return;
|
|
612
|
+
}
|
|
613
|
+
const allowed = Math.max(requested, -angleToNadir(camera));
|
|
614
|
+
if (allowed !== 0) {
|
|
615
|
+
rotateUp(allowed);
|
|
616
|
+
}
|
|
617
|
+
};
|
|
618
|
+
return () => {
|
|
619
|
+
Reflect.deleteProperty(camera, "rotateUp");
|
|
620
|
+
};
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
const MEGABYTE = 1024 * 1024;
|
|
624
|
+
const DEFAULT_MEM_USAGE = 512;
|
|
625
|
+
const DEFAULT_FILL_COLOR = [234, 231, 221, 1];
|
|
626
|
+
const DEFAULT_SCREENSPACE_ERROR = 16;
|
|
627
|
+
function colorExpression([red, green, blue, alpha], opacity) {
|
|
628
|
+
return `rgba(${red}, ${green}, ${blue}, ${alpha * opacity})`;
|
|
629
|
+
}
|
|
630
|
+
function tilesetOptions(source) {
|
|
631
|
+
return {
|
|
632
|
+
maximumScreenSpaceError: source.maximumScreenSpaceError ?? DEFAULT_SCREENSPACE_ERROR,
|
|
633
|
+
skipLevelOfDetail: source.skipLevelOfDetail ?? true,
|
|
634
|
+
cullWithChildrenBounds: source.cullWithChildrenBounds ?? true,
|
|
635
|
+
dynamicScreenSpaceError: source.dynamicScreenSpaceError ?? true,
|
|
636
|
+
dynamicScreenSpaceErrorDensity: 0.99,
|
|
637
|
+
cacheBytes: source.maximumMemoryUsage ? source.maximumMemoryUsage * MEGABYTE : DEFAULT_MEM_USAGE * MEGABYTE,
|
|
638
|
+
cullRequestsWhileMoving: true,
|
|
639
|
+
cullRequestsWhileMovingMultiplier: 300,
|
|
640
|
+
foveatedScreenSpaceError: true
|
|
641
|
+
};
|
|
642
|
+
}
|
|
643
|
+
function i3sUrl(source) {
|
|
644
|
+
const withLayer = source.layerId === void 0 || /\/layers\/\d+\/?$/.test(source.url) ? source.url : `${source.url.replace(/\/+$/, "")}/layers/${source.layerId}`;
|
|
645
|
+
if (!source.token) {
|
|
646
|
+
return withLayer;
|
|
647
|
+
}
|
|
648
|
+
return `${withLayer}${withLayer.includes("?") ? "&" : "?"}token=${encodeURIComponent(source.token)}`;
|
|
649
|
+
}
|
|
650
|
+
async function create3DTileset(source) {
|
|
651
|
+
if (!source.url) {
|
|
652
|
+
return null;
|
|
653
|
+
}
|
|
654
|
+
if (source.type === "i3s") {
|
|
655
|
+
const provider = await I3SDataProvider.fromUrl(i3sUrl(source), {
|
|
656
|
+
cesium3dTilesetOptions: tilesetOptions(source)
|
|
657
|
+
});
|
|
658
|
+
return { primitive: provider, source };
|
|
659
|
+
}
|
|
660
|
+
const tileset = await Cesium3DTileset.fromUrl(source.url, tilesetOptions(source));
|
|
661
|
+
return { primitive: tileset, styleable: tileset, source };
|
|
662
|
+
}
|
|
663
|
+
function apply3DTilesetOpacity(created, opacity) {
|
|
664
|
+
const tileset = created.styleable;
|
|
665
|
+
if (!tileset) {
|
|
666
|
+
return;
|
|
667
|
+
}
|
|
668
|
+
tileset.style = new Cesium3DTileStyle({
|
|
669
|
+
color: colorExpression(created.source.fillColor ?? DEFAULT_FILL_COLOR, opacity)
|
|
670
|
+
});
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
function constantColor(property, fallback) {
|
|
674
|
+
const value = property?.getValue(Iso8601.MINIMUM_VALUE);
|
|
675
|
+
return value instanceof Color ? value : fallback;
|
|
676
|
+
}
|
|
677
|
+
function colorSlot(current, fallback, install) {
|
|
678
|
+
const base = constantColor(current, fallback);
|
|
679
|
+
if (current instanceof ConstantProperty) {
|
|
680
|
+
return { base, property: current };
|
|
681
|
+
}
|
|
682
|
+
const property = new ConstantProperty(base);
|
|
683
|
+
install(property);
|
|
684
|
+
return { base, property };
|
|
685
|
+
}
|
|
686
|
+
function materialSlot(graphic) {
|
|
687
|
+
const material = graphic.material;
|
|
688
|
+
if (material instanceof ImageMaterialProperty || material instanceof ColorMaterialProperty) {
|
|
689
|
+
return colorSlot(material.color, Color.WHITE, (property) => {
|
|
690
|
+
material.color = property;
|
|
691
|
+
});
|
|
692
|
+
}
|
|
693
|
+
const created = new ColorMaterialProperty(Color.WHITE);
|
|
694
|
+
graphic.material = created;
|
|
695
|
+
return colorSlot(created.color, Color.WHITE, (property) => {
|
|
696
|
+
created.color = property;
|
|
697
|
+
});
|
|
698
|
+
}
|
|
699
|
+
function fillSlots(graphic) {
|
|
700
|
+
return graphic ? [materialSlot(graphic)] : [];
|
|
701
|
+
}
|
|
702
|
+
function outlineSlots(graphic) {
|
|
703
|
+
if (!graphic) {
|
|
704
|
+
return [];
|
|
705
|
+
}
|
|
706
|
+
return [
|
|
707
|
+
colorSlot(graphic.outlineColor, Color.BLACK, (property) => {
|
|
708
|
+
graphic.outlineColor = property;
|
|
709
|
+
})
|
|
710
|
+
];
|
|
711
|
+
}
|
|
712
|
+
function tintSlots(graphic) {
|
|
713
|
+
if (!graphic) {
|
|
714
|
+
return [];
|
|
715
|
+
}
|
|
716
|
+
return [
|
|
717
|
+
colorSlot(graphic.color, Color.WHITE, (property) => {
|
|
718
|
+
graphic.color = property;
|
|
719
|
+
})
|
|
720
|
+
];
|
|
721
|
+
}
|
|
722
|
+
function entitySlots(entity) {
|
|
723
|
+
const { polygon, polyline, wall, rectangle, path, billboard, point } = entity;
|
|
724
|
+
return [
|
|
725
|
+
...fillSlots(polygon),
|
|
726
|
+
...outlineSlots(polygon),
|
|
727
|
+
...fillSlots(polyline),
|
|
728
|
+
...fillSlots(wall),
|
|
729
|
+
...outlineSlots(wall),
|
|
730
|
+
...fillSlots(rectangle),
|
|
731
|
+
...fillSlots(path),
|
|
732
|
+
...tintSlots(billboard),
|
|
733
|
+
...tintSlots(point),
|
|
734
|
+
...outlineSlots(point)
|
|
735
|
+
];
|
|
736
|
+
}
|
|
737
|
+
function collectKmlOpacitySlots(entities) {
|
|
738
|
+
return entities.values.flatMap(entitySlots);
|
|
739
|
+
}
|
|
740
|
+
const scratchColor = new Color();
|
|
741
|
+
function applyKmlOpacity(slots, opacity) {
|
|
742
|
+
for (const slot of slots) {
|
|
743
|
+
slot.property.setValue(Color.fromAlpha(slot.base, slot.base.alpha * opacity, scratchColor));
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
const DEFAULT_COLOR = Color.fromCssColorString(KML_DEFAULT_COLOR);
|
|
748
|
+
const LABEL_OUTLINE_COLOR = Color.fromCssColorString(KML_LABEL_OUTLINE_COLOR);
|
|
749
|
+
const SDF_FONT_SIZE_PX = 48;
|
|
750
|
+
const LABEL_OUTLINE_WIDTH = KML_LABEL_OUTLINE_PX * SDF_FONT_SIZE_PX / KML_LABEL_FONT_SIZE_PX;
|
|
751
|
+
const MIN_DRAWN_OUTLINE_WIDTH = 1;
|
|
752
|
+
function constantValue(property) {
|
|
753
|
+
return property?.getValue(Iso8601.MINIMUM_VALUE);
|
|
754
|
+
}
|
|
755
|
+
function markerHeightReference(clampToGround) {
|
|
756
|
+
return clampToGround ? HeightReference.CLAMP_TO_GROUND : HeightReference.NONE;
|
|
757
|
+
}
|
|
758
|
+
function makeLabelReadable(label) {
|
|
759
|
+
label.font = new ConstantProperty(KML_LABEL_FONT);
|
|
760
|
+
label.outlineColor = new ConstantProperty(LABEL_OUTLINE_COLOR);
|
|
761
|
+
label.outlineWidth = new ConstantProperty(LABEL_OUTLINE_WIDTH);
|
|
762
|
+
}
|
|
763
|
+
function normaliseKmlEntities(entities, clampToGround) {
|
|
764
|
+
for (const entity of entities.values) {
|
|
765
|
+
if (entity.label) {
|
|
766
|
+
makeLabelReadable(entity.label);
|
|
767
|
+
}
|
|
768
|
+
if (entity.polyline && clampToGround) {
|
|
769
|
+
entity.polyline.clampToGround = new ConstantProperty(true);
|
|
770
|
+
entity.polyline.arcType = new ConstantProperty(ArcType.GEODESIC);
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
function ringPositions(polygon) {
|
|
775
|
+
const hierarchy = constantValue(polygon.hierarchy);
|
|
776
|
+
if (!(hierarchy instanceof PolygonHierarchy)) {
|
|
777
|
+
return [];
|
|
778
|
+
}
|
|
779
|
+
return [hierarchy.positions, ...hierarchy.holes.map((hole) => hole.positions)];
|
|
780
|
+
}
|
|
781
|
+
function boundaryColor(polygon, stroke) {
|
|
782
|
+
if (stroke) {
|
|
783
|
+
return stroke;
|
|
784
|
+
}
|
|
785
|
+
const outlineColor = constantValue(polygon.outlineColor);
|
|
786
|
+
if (outlineColor instanceof Color && !Color.equals(outlineColor, Color.WHITE)) {
|
|
787
|
+
return outlineColor;
|
|
788
|
+
}
|
|
789
|
+
return DEFAULT_COLOR;
|
|
790
|
+
}
|
|
791
|
+
function boundaryEntities(entity, polygon, stroke, width, clampToGround) {
|
|
792
|
+
const rings = ringPositions(polygon);
|
|
793
|
+
if (rings.length === 0) {
|
|
794
|
+
return [];
|
|
795
|
+
}
|
|
796
|
+
const color = boundaryColor(polygon, stroke);
|
|
797
|
+
polygon.outline = new ConstantProperty(false);
|
|
798
|
+
return rings.map((positions) => ({
|
|
799
|
+
parent: entity,
|
|
800
|
+
polyline: {
|
|
801
|
+
positions,
|
|
802
|
+
width,
|
|
803
|
+
material: new ColorMaterialProperty(color),
|
|
804
|
+
clampToGround,
|
|
805
|
+
arcType: clampToGround ? ArcType.GEODESIC : ArcType.NONE
|
|
806
|
+
}
|
|
807
|
+
}));
|
|
808
|
+
}
|
|
809
|
+
function styleFilled(graphic, fill, stroke) {
|
|
810
|
+
if (!graphic) {
|
|
811
|
+
return;
|
|
812
|
+
}
|
|
813
|
+
if (fill) {
|
|
814
|
+
graphic.material = new ColorMaterialProperty(fill);
|
|
815
|
+
}
|
|
816
|
+
if (stroke) {
|
|
817
|
+
graphic.outline = new ConstantProperty(true);
|
|
818
|
+
graphic.outlineColor = new ConstantProperty(stroke);
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
function stylePolyline(polyline, stroke, width) {
|
|
822
|
+
if (!polyline) {
|
|
823
|
+
return;
|
|
824
|
+
}
|
|
825
|
+
if (stroke) {
|
|
826
|
+
polyline.material = new ColorMaterialProperty(stroke);
|
|
827
|
+
}
|
|
828
|
+
if (width !== void 0) {
|
|
829
|
+
polyline.width = new ConstantProperty(width);
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
function hasAuthoredIcon(entity) {
|
|
833
|
+
const image = constantValue(entity.billboard?.image);
|
|
834
|
+
return image instanceof Resource || typeof image === "string";
|
|
835
|
+
}
|
|
836
|
+
function stylePoint(entity, params, markerColor, clampToGround) {
|
|
837
|
+
const billboard = entity.billboard;
|
|
838
|
+
if (!billboard) {
|
|
839
|
+
return;
|
|
840
|
+
}
|
|
841
|
+
if (params.symbol) {
|
|
842
|
+
billboard.image = new ConstantProperty(params.symbol);
|
|
843
|
+
billboard.width = void 0;
|
|
844
|
+
billboard.height = void 0;
|
|
845
|
+
billboard.scale = void 0;
|
|
846
|
+
billboard.pixelOffset = void 0;
|
|
847
|
+
billboard.imageSubRegion = void 0;
|
|
848
|
+
return;
|
|
849
|
+
}
|
|
850
|
+
if (!markerColor || hasAuthoredIcon(entity)) {
|
|
851
|
+
return;
|
|
852
|
+
}
|
|
853
|
+
entity.billboard = void 0;
|
|
854
|
+
entity.point = new PointGraphics({
|
|
855
|
+
pixelSize: KML_MARKER_SIZE_PX,
|
|
856
|
+
color: markerColor,
|
|
857
|
+
heightReference: markerHeightReference(clampToGround)
|
|
858
|
+
});
|
|
859
|
+
}
|
|
860
|
+
function applyKmlStyle(entities, params, clampToGround) {
|
|
861
|
+
const stroke = params.lineColor ? Color.fromCssColorString(params.lineColor) : void 0;
|
|
862
|
+
const fill = params.fillColor ? Color.fromCssColorString(params.fillColor) : void 0;
|
|
863
|
+
const width = params.lineThickness;
|
|
864
|
+
const markerColor = kmlExtractStyles(params) ? void 0 : fill ?? stroke ?? DEFAULT_COLOR;
|
|
865
|
+
const boundaries = [];
|
|
866
|
+
for (const entity of entities.values) {
|
|
867
|
+
styleFilled(entity.polygon, fill, stroke);
|
|
868
|
+
styleFilled(entity.wall, fill, stroke);
|
|
869
|
+
stylePolyline(entity.polyline, stroke, width);
|
|
870
|
+
stylePoint(entity, params, markerColor, clampToGround);
|
|
871
|
+
const polygon = entity.polygon;
|
|
872
|
+
if (polygon && width !== void 0 && width > MIN_DRAWN_OUTLINE_WIDTH) {
|
|
873
|
+
boundaries.push(...boundaryEntities(entity, polygon, stroke, width, clampToGround));
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
for (const boundary of boundaries) {
|
|
877
|
+
entities.add(boundary);
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
const CLAMPED_ALTITUDE_MODE = "clampToGround";
|
|
882
|
+
const GEOMETRY_TAGS = ["Point", "LineString", "LinearRing", "Polygon", "Track", "MultiTrack"];
|
|
883
|
+
function declaresOwnAltitude(geometry) {
|
|
884
|
+
return Array.from(geometry.children).some((child) => {
|
|
885
|
+
if (child.localName !== "altitudeMode") {
|
|
886
|
+
return false;
|
|
887
|
+
}
|
|
888
|
+
const mode = (child.textContent ?? "").trim();
|
|
889
|
+
return mode !== "" && mode !== CLAMPED_ALTITUDE_MODE;
|
|
890
|
+
});
|
|
891
|
+
}
|
|
892
|
+
function shouldClampToGround(kmlDocument) {
|
|
893
|
+
return !GEOMETRY_TAGS.some(
|
|
894
|
+
(tag) => Array.from(kmlDocument.getElementsByTagNameNS("*", tag)).some(declaresOwnAltitude)
|
|
895
|
+
);
|
|
896
|
+
}
|
|
897
|
+
async function createKML(params, camera, canvas) {
|
|
898
|
+
const kmlDocument = new DOMParser().parseFromString(params.objects, "application/xml");
|
|
899
|
+
const clampToGround = shouldClampToGround(kmlDocument);
|
|
900
|
+
const dataSource = await KmlDataSource.load(kmlDocument, { camera, canvas, clampToGround });
|
|
901
|
+
normaliseKmlEntities(dataSource.entities, clampToGround);
|
|
902
|
+
applyKmlStyle(dataSource.entities, params, clampToGround);
|
|
903
|
+
return { dataSource, opacitySlots: collectKmlOpacitySlots(dataSource.entities) };
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
const TILE_SIZE = 256;
|
|
907
|
+
const BBOX_TEMPLATE = {
|
|
908
|
+
geographic: {
|
|
909
|
+
eastingFirst: "{westDegrees},{southDegrees},{eastDegrees},{northDegrees}",
|
|
910
|
+
northingFirst: "{southDegrees},{westDegrees},{northDegrees},{eastDegrees}"
|
|
911
|
+
},
|
|
912
|
+
webMercator: {
|
|
913
|
+
eastingFirst: "{westProjected},{southProjected},{eastProjected},{northProjected}",
|
|
914
|
+
northingFirst: "{southProjected},{westProjected},{northProjected},{eastProjected}"
|
|
915
|
+
}
|
|
916
|
+
};
|
|
917
|
+
function createOGCMaps(source, presentation, capabilities) {
|
|
918
|
+
if (!capabilities) {
|
|
919
|
+
return null;
|
|
920
|
+
}
|
|
921
|
+
const resolved = ogcMapsCesiumCrs(capabilities);
|
|
922
|
+
if (!resolved) {
|
|
923
|
+
return null;
|
|
924
|
+
}
|
|
925
|
+
const { projection, crs, axisOrder } = resolved;
|
|
926
|
+
return new UrlTemplateImageryProvider({
|
|
927
|
+
url: withQuery(ogcMapsMapUrl(source), {
|
|
928
|
+
...ogcMapsQuery(source),
|
|
929
|
+
crs,
|
|
930
|
+
"bbox-crs": crs,
|
|
931
|
+
bbox: BBOX_TEMPLATE[projection][axisOrder],
|
|
932
|
+
width: "{width}",
|
|
933
|
+
height: "{height}"
|
|
934
|
+
}),
|
|
935
|
+
tilingScheme: projection === "webMercator" ? new WebMercatorTilingScheme() : new GeographicTilingScheme(),
|
|
936
|
+
tileWidth: TILE_SIZE,
|
|
937
|
+
tileHeight: TILE_SIZE,
|
|
938
|
+
credit: presentation.copyright
|
|
939
|
+
});
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
function createOGCTiles(presentation, tileset) {
|
|
943
|
+
const { cesium } = tileset;
|
|
944
|
+
if (!cesium) {
|
|
945
|
+
return null;
|
|
946
|
+
}
|
|
947
|
+
const labels = cesium.tileMatrixLabels;
|
|
948
|
+
return new UrlTemplateImageryProvider({
|
|
949
|
+
url: cesium.url,
|
|
950
|
+
tilingScheme: cesium.tilingScheme === "webMercator" ? new WebMercatorTilingScheme() : new GeographicTilingScheme(),
|
|
951
|
+
tileWidth: cesium.tileWidth,
|
|
952
|
+
tileHeight: cesium.tileHeight,
|
|
953
|
+
minimumLevel: cesium.minimumLevel,
|
|
954
|
+
maximumLevel: cesium.maximumLevel,
|
|
955
|
+
customTags: labels ? {
|
|
956
|
+
tileMatrix: (_provider, _x, _y, level) => labels[level] ?? ""
|
|
957
|
+
} : void 0,
|
|
958
|
+
credit: presentation.copyright
|
|
959
|
+
});
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
function safeMinimumLevel(scheme, rectangle, minimumLevel) {
|
|
963
|
+
const southwest = scheme.positionToTileXY(Rectangle.southwest(rectangle), minimumLevel);
|
|
964
|
+
const northeast = scheme.positionToTileXY(Rectangle.northeast(rectangle), minimumLevel);
|
|
965
|
+
if (!southwest || !northeast) {
|
|
966
|
+
return 0;
|
|
967
|
+
}
|
|
968
|
+
const tiles = (Math.abs(northeast.x - southwest.x) + 1) * (Math.abs(northeast.y - southwest.y) + 1);
|
|
969
|
+
return tiles > 4 ? 0 : minimumLevel;
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
function tilesetRectangle(tileMap, geographic) {
|
|
973
|
+
const [minX, minY, maxX, maxY] = tileMap.extent;
|
|
974
|
+
if (geographic) {
|
|
975
|
+
return Rectangle.fromDegrees(minX, minY, maxX, maxY);
|
|
976
|
+
}
|
|
977
|
+
const projection = new WebMercatorProjection();
|
|
978
|
+
const southwest = projection.unproject(new Cartesian3(minX, minY, 0));
|
|
979
|
+
const northeast = projection.unproject(new Cartesian3(maxX, maxY, 0));
|
|
980
|
+
return new Rectangle(
|
|
981
|
+
southwest.longitude,
|
|
982
|
+
southwest.latitude,
|
|
983
|
+
northeast.longitude,
|
|
984
|
+
northeast.latitude
|
|
985
|
+
);
|
|
986
|
+
}
|
|
987
|
+
function confineToScheme(rectangle, scheme) {
|
|
988
|
+
return Rectangle.intersection(rectangle, scheme.rectangle) ?? scheme.rectangle;
|
|
989
|
+
}
|
|
990
|
+
function createTMS(source, presentation, capabilitiesText) {
|
|
991
|
+
const tileMap = parseTmsTileMap(capabilitiesText);
|
|
992
|
+
const scheme = tileMap ? tmsCesiumSchemeFor(tileMap) : void 0;
|
|
993
|
+
if (!tileMap || !scheme) {
|
|
994
|
+
return null;
|
|
995
|
+
}
|
|
996
|
+
const geographic = scheme === "geographic";
|
|
997
|
+
const tilingScheme = geographic ? new GeographicTilingScheme() : new WebMercatorTilingScheme();
|
|
998
|
+
const rectangle = confineToScheme(tilesetRectangle(tileMap, geographic), tilingScheme);
|
|
999
|
+
const { minimum, maximum } = tmsLevelRange(tileMap);
|
|
1000
|
+
return new UrlTemplateImageryProvider({
|
|
1001
|
+
url: `${tmsTileBase(source.href)}{z}/{x}/{reverseY}${tmsTileSuffix(tileMap.extension)}`,
|
|
1002
|
+
tilingScheme,
|
|
1003
|
+
rectangle,
|
|
1004
|
+
tileWidth: tileMap.tileWidth,
|
|
1005
|
+
tileHeight: tileMap.tileHeight,
|
|
1006
|
+
minimumLevel: safeMinimumLevel(tilingScheme, rectangle, minimum),
|
|
1007
|
+
maximumLevel: maximum,
|
|
1008
|
+
credit: presentation.copyright
|
|
1009
|
+
});
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
function createWMS(source, presentation, capabilities) {
|
|
1013
|
+
if (!capabilities) {
|
|
1014
|
+
return null;
|
|
1015
|
+
}
|
|
1016
|
+
const request = parseWmsCaps(capabilities, source.layers, source.version);
|
|
1017
|
+
if (!request) {
|
|
1018
|
+
return null;
|
|
1019
|
+
}
|
|
1020
|
+
return new WebMapServiceImageryProvider({
|
|
1021
|
+
url: source.href,
|
|
1022
|
+
layers: source.layers.join(","),
|
|
1023
|
+
parameters: {
|
|
1024
|
+
format: source.format ?? "image/png",
|
|
1025
|
+
transparent: false,
|
|
1026
|
+
version: request.version,
|
|
1027
|
+
...source.params
|
|
1028
|
+
},
|
|
1029
|
+
crs: request.crs,
|
|
1030
|
+
srs: request.crs,
|
|
1031
|
+
tilingScheme: request.projection === "webMercator" ? new WebMercatorTilingScheme() : new GeographicTilingScheme(),
|
|
1032
|
+
credit: presentation.copyright
|
|
1033
|
+
});
|
|
1034
|
+
}
|
|
1035
|
+
|
|
1036
|
+
const ESTONIA_RECTANGLE = Rectangle.fromDegrees(10.11, 53.26, 35.2, 63.3);
|
|
1037
|
+
function coarsestLevel(tilingScheme, minimumLevel) {
|
|
1038
|
+
if (minimumLevel === void 0) {
|
|
1039
|
+
return void 0;
|
|
1040
|
+
}
|
|
1041
|
+
return safeMinimumLevel(tilingScheme, ESTONIA_RECTANGLE, minimumLevel);
|
|
1042
|
+
}
|
|
1043
|
+
function createWMTS(source, presentation, capabilitiesText) {
|
|
1044
|
+
const parsed = parseWmtsCapabilities(capabilitiesText, source.layer);
|
|
1045
|
+
if (!parsed) {
|
|
1046
|
+
return null;
|
|
1047
|
+
}
|
|
1048
|
+
const style = source.styles ?? "default";
|
|
1049
|
+
const url = `${source.href}/1.0.0/${source.layer}/${style}/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}`;
|
|
1050
|
+
const tilingScheme = parsed.tilingScheme === "geographic" ? new GeographicTilingScheme() : new WebMercatorTilingScheme();
|
|
1051
|
+
return new WebMapTileServiceImageryProvider({
|
|
1052
|
+
url,
|
|
1053
|
+
layer: source.layer,
|
|
1054
|
+
style,
|
|
1055
|
+
tileMatrixSetID: parsed.tileMatrixSetID,
|
|
1056
|
+
tileMatrixLabels: parsed.tileMatrixLabels.length > 0 ? parsed.tileMatrixLabels : void 0,
|
|
1057
|
+
format: source.format,
|
|
1058
|
+
tilingScheme,
|
|
1059
|
+
maximumLevel: parsed.maximumLevel,
|
|
1060
|
+
minimumLevel: coarsestLevel(tilingScheme, parsed.minimumLevel),
|
|
1061
|
+
credit: presentation.copyright,
|
|
1062
|
+
rectangle: ESTONIA_RECTANGLE
|
|
1063
|
+
});
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
function createCesiumBaseLayer(entry, viewer) {
|
|
1067
|
+
const { layer, capabilities, tileset } = entry;
|
|
1068
|
+
const { source } = layer;
|
|
1069
|
+
switch (source.kind) {
|
|
1070
|
+
case "wms": {
|
|
1071
|
+
const provider = createWMS(source, layer, capabilities);
|
|
1072
|
+
return provider ? { kind: "imagery", provider } : null;
|
|
1073
|
+
}
|
|
1074
|
+
case "wmts": {
|
|
1075
|
+
if (!capabilities) {
|
|
1076
|
+
return null;
|
|
1077
|
+
}
|
|
1078
|
+
const provider = createWMTS(source, layer, capabilities);
|
|
1079
|
+
return provider ? { kind: "imagery", provider } : null;
|
|
1080
|
+
}
|
|
1081
|
+
case "ogcMaps": {
|
|
1082
|
+
const provider = createOGCMaps(source, layer, capabilities);
|
|
1083
|
+
return provider ? { kind: "imagery", provider } : null;
|
|
1084
|
+
}
|
|
1085
|
+
case "ogcTiles": {
|
|
1086
|
+
const provider = tileset ? createOGCTiles(layer, tileset) : null;
|
|
1087
|
+
return provider ? { kind: "imagery", provider } : null;
|
|
1088
|
+
}
|
|
1089
|
+
case "tms": {
|
|
1090
|
+
const provider = capabilities ? createTMS(source, layer, capabilities) : null;
|
|
1091
|
+
return provider ? { kind: "imagery", provider } : null;
|
|
1092
|
+
}
|
|
1093
|
+
case "tileset3d":
|
|
1094
|
+
return { kind: "tileset", tileset: create3DTileset(source.tileset) };
|
|
1095
|
+
case "kml":
|
|
1096
|
+
return {
|
|
1097
|
+
kind: "kml",
|
|
1098
|
+
kml: createKML(source.params, viewer.scene.camera, viewer.scene.canvas)
|
|
1099
|
+
};
|
|
1100
|
+
default:
|
|
1101
|
+
return source;
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
|
|
1105
|
+
function useCesiumBasemapAttachments(viewer) {
|
|
1106
|
+
const imageryRef = useRef([]);
|
|
1107
|
+
const kmlRef = useRef([]);
|
|
1108
|
+
const tilesetRef = useRef([]);
|
|
1109
|
+
const opacityRef = useRef(100);
|
|
1110
|
+
return useMemo(
|
|
1111
|
+
() => ({
|
|
1112
|
+
addImagery(provider) {
|
|
1113
|
+
provider.errorEvent.addEventListener(() => {
|
|
1114
|
+
});
|
|
1115
|
+
const layer = viewer.imageryLayers.addImageryProvider(provider, 0);
|
|
1116
|
+
layer.alpha = toAlpha(opacityRef.current);
|
|
1117
|
+
imageryRef.current.push(layer);
|
|
1118
|
+
viewer.scene.requestRender();
|
|
1119
|
+
},
|
|
1120
|
+
addTileset(tileset) {
|
|
1121
|
+
tilesetRef.current.push(tileset);
|
|
1122
|
+
viewer.scene.primitives.add(tileset.primitive);
|
|
1123
|
+
apply3DTilesetOpacity(tileset, toAlpha(opacityRef.current));
|
|
1124
|
+
viewer.scene.requestRender();
|
|
1125
|
+
},
|
|
1126
|
+
addKml(kml) {
|
|
1127
|
+
kmlRef.current.push(kml);
|
|
1128
|
+
viewer.dataSources.add(kml.dataSource);
|
|
1129
|
+
applyKmlOpacity(kml.opacitySlots, toAlpha(opacityRef.current));
|
|
1130
|
+
viewer.scene.requestRender();
|
|
1131
|
+
},
|
|
1132
|
+
detachAll() {
|
|
1133
|
+
for (const layer of imageryRef.current) {
|
|
1134
|
+
viewer.imageryLayers.remove(layer);
|
|
1135
|
+
}
|
|
1136
|
+
imageryRef.current = [];
|
|
1137
|
+
for (const kml of kmlRef.current) {
|
|
1138
|
+
viewer.dataSources.remove(kml.dataSource, true);
|
|
1139
|
+
}
|
|
1140
|
+
kmlRef.current = [];
|
|
1141
|
+
for (const tileset of tilesetRef.current) {
|
|
1142
|
+
viewer.scene.primitives.remove(tileset.primitive);
|
|
1143
|
+
}
|
|
1144
|
+
tilesetRef.current = [];
|
|
1145
|
+
},
|
|
1146
|
+
setOpacity(opacity) {
|
|
1147
|
+
opacityRef.current = opacity;
|
|
1148
|
+
for (const layer of imageryRef.current) {
|
|
1149
|
+
layer.alpha = toAlpha(opacity);
|
|
1150
|
+
}
|
|
1151
|
+
for (const tileset of tilesetRef.current) {
|
|
1152
|
+
apply3DTilesetOpacity(tileset, toAlpha(opacity));
|
|
1153
|
+
}
|
|
1154
|
+
for (const kml of kmlRef.current) {
|
|
1155
|
+
applyKmlOpacity(kml.opacitySlots, toAlpha(opacity));
|
|
1156
|
+
}
|
|
1157
|
+
viewer.scene.requestRender();
|
|
1158
|
+
}
|
|
1159
|
+
}),
|
|
1160
|
+
[viewer]
|
|
1161
|
+
);
|
|
1162
|
+
}
|
|
1163
|
+
|
|
1164
|
+
function useCesiumBasemapReconciler(viewer) {
|
|
1165
|
+
const basemapOpacity = useBasemapStore((s) => s.opacity3d);
|
|
1166
|
+
const plan = useActiveBasemapPlan("3d");
|
|
1167
|
+
const { entries, ready } = plan;
|
|
1168
|
+
const reportServiceError = useBasemapServiceError("3d", plan);
|
|
1169
|
+
const attachments = useCesiumBasemapAttachments(viewer);
|
|
1170
|
+
useEffect(() => {
|
|
1171
|
+
attachments.setOpacity(basemapOpacity);
|
|
1172
|
+
}, [attachments, basemapOpacity]);
|
|
1173
|
+
useEffect(() => {
|
|
1174
|
+
if (!ready) {
|
|
1175
|
+
return;
|
|
1176
|
+
}
|
|
1177
|
+
attachments.detachAll();
|
|
1178
|
+
let cancelled = false;
|
|
1179
|
+
const removeListeners = [];
|
|
1180
|
+
const addImagery = (provider) => {
|
|
1181
|
+
attachments.addImagery(provider);
|
|
1182
|
+
removeListeners.push(provider.errorEvent.addEventListener(reportServiceError));
|
|
1183
|
+
};
|
|
1184
|
+
const attachAsync = (load, attach, failure) => {
|
|
1185
|
+
void (async () => {
|
|
1186
|
+
try {
|
|
1187
|
+
const value = await load;
|
|
1188
|
+
if (!cancelled && value) {
|
|
1189
|
+
attach(value);
|
|
1190
|
+
}
|
|
1191
|
+
} catch (error) {
|
|
1192
|
+
console.warn(failure, error);
|
|
1193
|
+
reportServiceError();
|
|
1194
|
+
}
|
|
1195
|
+
})();
|
|
1196
|
+
};
|
|
1197
|
+
for (const entry of entries) {
|
|
1198
|
+
const created = createCesiumBaseLayer(entry, viewer);
|
|
1199
|
+
if (!created) {
|
|
1200
|
+
continue;
|
|
1201
|
+
}
|
|
1202
|
+
if (created.kind === "imagery") {
|
|
1203
|
+
addImagery(created.provider);
|
|
1204
|
+
} else if (created.kind === "tileset") {
|
|
1205
|
+
attachAsync(created.tileset, attachments.addTileset, "Failed to load 3D tileset basemap");
|
|
1206
|
+
} else {
|
|
1207
|
+
attachAsync(created.kml, attachments.addKml, "Failed to load KML basemap");
|
|
1208
|
+
}
|
|
1209
|
+
}
|
|
1210
|
+
return () => {
|
|
1211
|
+
cancelled = true;
|
|
1212
|
+
for (const remove of removeListeners) {
|
|
1213
|
+
remove();
|
|
1214
|
+
}
|
|
1215
|
+
};
|
|
1216
|
+
}, [viewer, attachments, entries, ready, reportServiceError]);
|
|
1217
|
+
useEffect(() => attachments.detachAll, [attachments]);
|
|
1218
|
+
}
|
|
1219
|
+
|
|
1220
|
+
const TAKEOVER_EVENTS = ["pointerdown", "wheel"];
|
|
1221
|
+
const CENTRE_TOLERANCE_METRES = 1;
|
|
1222
|
+
function viewRecentred(entryBbox, currentBbox) {
|
|
1223
|
+
const [entryX, entryY] = extentCenter(entryBbox);
|
|
1224
|
+
const [currentX, currentY] = extentCenter(currentBbox);
|
|
1225
|
+
return Math.hypot(currentX - entryX, currentY - entryY) >= CENTRE_TOLERANCE_METRES;
|
|
1226
|
+
}
|
|
1227
|
+
function useCesiumEntryCamera(viewer, view) {
|
|
1228
|
+
useEffect(() => {
|
|
1229
|
+
const canvas = viewer.scene.canvas;
|
|
1230
|
+
let done = false;
|
|
1231
|
+
let userTookOver = false;
|
|
1232
|
+
let stopListening = () => {
|
|
1233
|
+
};
|
|
1234
|
+
function noteTakeover() {
|
|
1235
|
+
userTookOver = true;
|
|
1236
|
+
}
|
|
1237
|
+
function finish() {
|
|
1238
|
+
done = true;
|
|
1239
|
+
stopListening();
|
|
1240
|
+
TAKEOVER_EVENTS.forEach((event) => canvas.removeEventListener(event, noteTakeover));
|
|
1241
|
+
}
|
|
1242
|
+
async function refine() {
|
|
1243
|
+
if (done) {
|
|
1244
|
+
return;
|
|
1245
|
+
}
|
|
1246
|
+
try {
|
|
1247
|
+
const height = await sampleEntryHeight(viewer.terrainProvider, entryTarget(viewer, view));
|
|
1248
|
+
if (done || height === void 0) {
|
|
1249
|
+
return;
|
|
1250
|
+
}
|
|
1251
|
+
if (userTookOver || viewRecentred(view.bbox, useMapViewStore.getState().bbox)) {
|
|
1252
|
+
finish();
|
|
1253
|
+
return;
|
|
1254
|
+
}
|
|
1255
|
+
raiseEntryCameraToTerrain(viewer, entryTarget(viewer, view), height);
|
|
1256
|
+
finish();
|
|
1257
|
+
} catch (error) {
|
|
1258
|
+
console.error("Failed to sample terrain for the entry camera", error);
|
|
1259
|
+
}
|
|
1260
|
+
}
|
|
1261
|
+
TAKEOVER_EVENTS.forEach((event) => canvas.addEventListener(event, noteTakeover));
|
|
1262
|
+
stopListening = viewer.scene.globe.terrainProviderChanged.addEventListener(() => {
|
|
1263
|
+
refine();
|
|
1264
|
+
});
|
|
1265
|
+
refine();
|
|
1266
|
+
return finish;
|
|
1267
|
+
}, [viewer, view]);
|
|
1268
|
+
}
|
|
1269
|
+
|
|
1270
|
+
function useCesiumTerrainReconciler(viewer) {
|
|
1271
|
+
const terrainUrl = useTerrainStore((s) => s.terrainUrl);
|
|
1272
|
+
useEffect(() => {
|
|
1273
|
+
if (!terrainUrl) {
|
|
1274
|
+
return;
|
|
1275
|
+
}
|
|
1276
|
+
let cancelled = false;
|
|
1277
|
+
async function loadTerrain(url) {
|
|
1278
|
+
try {
|
|
1279
|
+
const provider = await CesiumTerrainProvider.fromUrl(url);
|
|
1280
|
+
if (!cancelled) {
|
|
1281
|
+
viewer.terrainProvider = provider;
|
|
1282
|
+
}
|
|
1283
|
+
} catch (err) {
|
|
1284
|
+
console.error("Failed to load terrain provider", err);
|
|
1285
|
+
}
|
|
1286
|
+
}
|
|
1287
|
+
loadTerrain(terrainUrl);
|
|
1288
|
+
return () => {
|
|
1289
|
+
cancelled = true;
|
|
1290
|
+
viewer.terrainProvider = new EllipsoidTerrainProvider();
|
|
1291
|
+
};
|
|
1292
|
+
}, [viewer, terrainUrl]);
|
|
1293
|
+
}
|
|
1294
|
+
|
|
1295
|
+
function useCesiumUserMarker(viewer) {
|
|
1296
|
+
const marker = useUserMarkerStore((s) => s.marker);
|
|
1297
|
+
const entityRef = useRef(null);
|
|
1298
|
+
useEffect(() => {
|
|
1299
|
+
const entity = viewer.entities.add({
|
|
1300
|
+
point: {
|
|
1301
|
+
pixelSize: 12,
|
|
1302
|
+
color: Color.fromCssColorString("#2563EB"),
|
|
1303
|
+
outlineColor: Color.WHITE,
|
|
1304
|
+
outlineWidth: 2,
|
|
1305
|
+
heightReference: HeightReference.CLAMP_TO_GROUND,
|
|
1306
|
+
disableDepthTestDistance: Number.POSITIVE_INFINITY
|
|
1307
|
+
},
|
|
1308
|
+
show: false
|
|
1309
|
+
});
|
|
1310
|
+
entityRef.current = entity;
|
|
1311
|
+
return () => {
|
|
1312
|
+
viewer.entities.remove(entity);
|
|
1313
|
+
entityRef.current = null;
|
|
1314
|
+
};
|
|
1315
|
+
}, [viewer]);
|
|
1316
|
+
useEffect(() => {
|
|
1317
|
+
const entity = entityRef.current;
|
|
1318
|
+
if (!entity) {
|
|
1319
|
+
return;
|
|
1320
|
+
}
|
|
1321
|
+
if (marker === null) {
|
|
1322
|
+
entity.show = false;
|
|
1323
|
+
} else {
|
|
1324
|
+
entity.position = new ConstantPositionProperty(Cartesian3.fromDegrees(marker.lon, marker.lat));
|
|
1325
|
+
entity.show = true;
|
|
1326
|
+
}
|
|
1327
|
+
viewer.scene.requestRender();
|
|
1328
|
+
}, [marker, viewer]);
|
|
1329
|
+
}
|
|
1330
|
+
|
|
1331
|
+
function useCesiumViewBounds(viewer) {
|
|
1332
|
+
const constraintBbox = useMapViewStore((s) => s.constraintBbox);
|
|
1333
|
+
useEffect(() => {
|
|
1334
|
+
if (!constraintBbox) {
|
|
1335
|
+
return;
|
|
1336
|
+
}
|
|
1337
|
+
return createCameraBoundsGuard(viewer, constraintBbox);
|
|
1338
|
+
}, [viewer, constraintBbox]);
|
|
1339
|
+
}
|
|
1340
|
+
|
|
1341
|
+
const CAMERA_INERTIA = 0;
|
|
1342
|
+
function applyZoomDistanceLimits(viewer) {
|
|
1343
|
+
viewer.scene.screenSpaceCameraController.maximumZoomDistance = rangeForZoom(viewer, MIN_ZOOM);
|
|
1344
|
+
viewer.scene.screenSpaceCameraController.minimumZoomDistance = rangeForZoom(viewer, MAX_ZOOM);
|
|
1345
|
+
}
|
|
1346
|
+
const MSAA = 1;
|
|
1347
|
+
function applyGlobeSettings(viewer) {
|
|
1348
|
+
const globe = viewer.scene.globe;
|
|
1349
|
+
globe.baseColor = Color.BLACK;
|
|
1350
|
+
globe.showGroundAtmosphere = false;
|
|
1351
|
+
globe.maximumScreenSpaceError = 2.5;
|
|
1352
|
+
}
|
|
1353
|
+
function CesiumLayerSync({ viewer, entry }) {
|
|
1354
|
+
useCesiumBasemapReconciler(viewer);
|
|
1355
|
+
useCesiumTerrainReconciler(viewer);
|
|
1356
|
+
useCesiumEntryCamera(viewer, entry);
|
|
1357
|
+
useCesiumUserMarker(viewer);
|
|
1358
|
+
useCesiumViewBounds(viewer);
|
|
1359
|
+
return null;
|
|
1360
|
+
}
|
|
1361
|
+
function Map3D({ onEngineReady }) {
|
|
1362
|
+
ensureCesiumBaseUrl();
|
|
1363
|
+
const [cesiumViewer, setCesiumViewer] = useState(null);
|
|
1364
|
+
const [entry, setEntry] = useState(null);
|
|
1365
|
+
const onViewerInit = useCallback((ref) => {
|
|
1366
|
+
if (!ref?.cesiumElement) {
|
|
1367
|
+
return;
|
|
1368
|
+
}
|
|
1369
|
+
const viewer = ref.cesiumElement;
|
|
1370
|
+
const view = useMapViewStore.getState();
|
|
1371
|
+
placeEntryCamera(viewer, entryTarget(viewer, view), ELLIPSOID_HEIGHT);
|
|
1372
|
+
applyZoomDistanceLimits(viewer);
|
|
1373
|
+
applyGlobeSettings(viewer);
|
|
1374
|
+
viewer.scene.screenSpaceCameraController.minimumCollisionTerrainHeight = Number.POSITIVE_INFINITY;
|
|
1375
|
+
viewer.scene.screenSpaceCameraController.inertiaSpin = CAMERA_INERTIA;
|
|
1376
|
+
viewer.scene.screenSpaceCameraController.inertiaZoom = CAMERA_INERTIA;
|
|
1377
|
+
setEntry(view);
|
|
1378
|
+
setCesiumViewer(viewer);
|
|
1379
|
+
}, []);
|
|
1380
|
+
useEffect(() => {
|
|
1381
|
+
if (!cesiumViewer) {
|
|
1382
|
+
return;
|
|
1383
|
+
}
|
|
1384
|
+
const health = createCesiumEngineHealth(cesiumViewer);
|
|
1385
|
+
const engine = {
|
|
1386
|
+
type: "3d",
|
|
1387
|
+
capabilities: {
|
|
1388
|
+
camera: createCesiumCameraCapability(cesiumViewer),
|
|
1389
|
+
viewLimits: createCesiumViewLimitsCapability(cesiumViewer)
|
|
1390
|
+
},
|
|
1391
|
+
destroy() {
|
|
1392
|
+
},
|
|
1393
|
+
reportError: health.reportError,
|
|
1394
|
+
onError: health.onError
|
|
1395
|
+
};
|
|
1396
|
+
const { unregister } = onEngineReady(engine);
|
|
1397
|
+
return () => {
|
|
1398
|
+
health.dispose();
|
|
1399
|
+
unregister();
|
|
1400
|
+
};
|
|
1401
|
+
}, [cesiumViewer, onEngineReady]);
|
|
1402
|
+
useEffect(() => {
|
|
1403
|
+
if (!cesiumViewer) {
|
|
1404
|
+
return;
|
|
1405
|
+
}
|
|
1406
|
+
return createCesiumKeyboardHandler(cesiumViewer);
|
|
1407
|
+
}, [cesiumViewer]);
|
|
1408
|
+
useEffect(() => {
|
|
1409
|
+
if (!cesiumViewer) {
|
|
1410
|
+
return;
|
|
1411
|
+
}
|
|
1412
|
+
return createCanvasPressRelay(cesiumViewer);
|
|
1413
|
+
}, [cesiumViewer]);
|
|
1414
|
+
useEffect(() => {
|
|
1415
|
+
if (!cesiumViewer) {
|
|
1416
|
+
return;
|
|
1417
|
+
}
|
|
1418
|
+
return createNadirLimit(cesiumViewer);
|
|
1419
|
+
}, [cesiumViewer]);
|
|
1420
|
+
useEffect(() => {
|
|
1421
|
+
if (!cesiumViewer) {
|
|
1422
|
+
return;
|
|
1423
|
+
}
|
|
1424
|
+
const observer = new ResizeObserver(() => applyZoomDistanceLimits(cesiumViewer));
|
|
1425
|
+
observer.observe(cesiumViewer.scene.canvas);
|
|
1426
|
+
return () => {
|
|
1427
|
+
observer.disconnect();
|
|
1428
|
+
};
|
|
1429
|
+
}, [cesiumViewer]);
|
|
1430
|
+
return /* @__PURE__ */ jsx(
|
|
1431
|
+
Viewer,
|
|
1432
|
+
{
|
|
1433
|
+
ref: onViewerInit,
|
|
1434
|
+
full: true,
|
|
1435
|
+
animation: false,
|
|
1436
|
+
baseLayerPicker: false,
|
|
1437
|
+
fullscreenButton: false,
|
|
1438
|
+
geocoder: false,
|
|
1439
|
+
homeButton: false,
|
|
1440
|
+
infoBox: false,
|
|
1441
|
+
sceneModePicker: false,
|
|
1442
|
+
selectionIndicator: false,
|
|
1443
|
+
timeline: false,
|
|
1444
|
+
navigationHelpButton: false,
|
|
1445
|
+
baseLayer: false,
|
|
1446
|
+
requestRenderMode: true,
|
|
1447
|
+
maximumRenderTimeChange: Infinity,
|
|
1448
|
+
scene3DOnly: true,
|
|
1449
|
+
useBrowserRecommendedResolution: false,
|
|
1450
|
+
msaaSamples: MSAA,
|
|
1451
|
+
skyBox: false,
|
|
1452
|
+
skyAtmosphere: false,
|
|
1453
|
+
showRenderLoopErrors: false,
|
|
1454
|
+
children: cesiumViewer && entry && /* @__PURE__ */ jsx(CesiumLayerSync, { viewer: cesiumViewer, entry })
|
|
1455
|
+
}
|
|
1456
|
+
);
|
|
1457
|
+
}
|
|
1458
|
+
|
|
1459
|
+
export { Map3D, ensureCesiumBaseUrl };
|
|
1460
|
+
//# sourceMappingURL=index.js.map
|