Skip to content

Commit d8a0a92

Browse files
committed
refactor: add layer fallback for osm
1 parent 8e40771 commit d8a0a92

5 files changed

Lines changed: 460 additions & 13 deletions

File tree

web/src/classic/components/molecules/Visualizer/compatibility/FALLBACKS.md

Lines changed: 95 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ The fallback system provides temporary alternatives when Cesium Ion assets canno
66

77
## Data Flow
88

9+
### Scene Properties
910
```
1011
sceneProperty (from props)
1112
@@ -18,11 +19,22 @@ applyFallbacks() - Fallback when no Cesium Ion token
1819
overriddenSceneProperty (passed to consumers)
1920
```
2021

22+
### Layers
23+
```
24+
rootLayer (from props)
25+
26+
(no backward compatibility needed for layers)
27+
28+
applyLayerFallbacks() - Fallback when no Cesium Ion token
29+
30+
transformedRootLayer (passed to LayerStore)
31+
```
32+
2133
## When Fallbacks Apply
2234

2335
Fallbacks **ONLY** apply when:
2436
- No Cesium Ion token is provided (`sceneProperty.default?.ion` is empty/undefined)
25-
- The tile or terrain type requires Cesium Ion authentication
37+
- The tile, terrain, or layer type requires Cesium Ion authentication
2638

2739
If a Cesium Ion token is present, no fallbacks are applied and the original Cesium Ion assets are used.
2840

@@ -32,19 +44,25 @@ The fallback logic is implemented in three files:
3244

3345
### 1. `fallbacks.ts`
3446
Contains the core fallback logic:
35-
- `applyFallbacks()` - Main entry point that checks for token and applies fallbacks
47+
- `applyFallbacks()` - Main entry point that checks for token and applies fallbacks to scene properties
3648
- `fallbackTileType()` - Fallbacks for tiles requiring Cesium Ion
3749
- `fallbackTerrainType()` - Fallbacks for terrain requiring Cesium Ion
50+
- `fallbackLayerSourceType()` - Fallbacks for layer sourceType requiring Cesium Ion (OSM buildings)
51+
- `applyLayerFallbacks()` - Main entry point for layer tree transformations when no Cesium Ion token
3852

3953
### 2. `fallbacks.test.ts`
4054
Comprehensive unit tests covering:
4155
- All tile type fallbacks
4256
- Terrain type fallback
57+
- Layer sourceType fallback (individual function)
58+
- Layer tree fallbacks (applyLayerFallbacks)
4359
- Token presence/absence scenarios
44-
- Edge cases (empty token, mixed tile types, etc.)
60+
- Edge cases (empty token, mixed tile types, nested layers, etc.)
4561

4662
### 3. `hooks.ts`
47-
Integration point where fallbacks are applied after backward compatibility:
63+
Integration point where both scene property and layer fallbacks are applied:
64+
65+
**Scene Property Fallbacks:**
4866
```typescript
4967
const backwardCompatibleSceneProperty = useMemo(
5068
() => applyBackwardCompatibility(overriddenScenePropertyRaw),
@@ -57,6 +75,23 @@ const overriddenSceneProperty = useMemo(
5775
);
5876
```
5977

78+
**Layer Fallbacks:**
79+
```typescript
80+
const transformedRootLayer = useMemo(
81+
() => applyLayerFallbacks(rootLayer, !!overriddenSceneProperty?.default?.ion),
82+
[rootLayer, overriddenSceneProperty?.default?.ion],
83+
);
84+
```
85+
86+
The transformed layer tree is then passed to `useLayers`:
87+
```typescript
88+
const { layers, selectedLayer, ... } = useLayers({
89+
rootLayer: transformedRootLayer,
90+
selected: outerSelectedLayerId,
91+
onSelect: onLayerSelect,
92+
});
93+
```
94+
6095
## Tile Fallback Rules
6196

6297
### When No Cesium Ion Token
@@ -143,6 +178,49 @@ After fallback:
143178
}
144179
```
145180

181+
## Layer Fallback Rules
182+
183+
### When No Cesium Ion Token
184+
185+
| Source Type | Fallback Type | Notes |
186+
|-------------|----------------------|-----------------------------------------|
187+
| `osm` | `reearth-buildings` | Re:Earth Buildings (OSM Buildings) |
188+
189+
**Note:** Other layer sourceTypes remain unchanged (no fallback). This fallback is applied at the layer level in the Tileset component.
190+
191+
### Example
192+
193+
**Scenario: No Cesium Ion token**
194+
195+
A Tileset layer with sourceType "osm" will automatically use Re:Earth Buildings instead:
196+
197+
Before fallback:
198+
```typescript
199+
{
200+
sourceType: "osm",
201+
// This would require Cesium Ion asset ID 96188
202+
}
203+
```
204+
205+
After fallback:
206+
```typescript
207+
{
208+
sourceType: "reearth-buildings",
209+
// Uses https://buildings.reearth.land/tileset.json
210+
}
211+
```
212+
213+
**Scenario: With Cesium Ion token**
214+
215+
```typescript
216+
{
217+
sourceType: "osm",
218+
// Token available - uses Cesium Ion asset ID 96188
219+
}
220+
```
221+
222+
No fallback applied - layer uses OSM Buildings from Cesium Ion.
223+
146224
## Combined Example: Backward Compatibility + Fallbacks
147225

148226
### Input (Old Format)
@@ -196,9 +274,11 @@ Run the unit tests:
196274
yarn test fallbacks.test.ts
197275
```
198276

199-
All 22 tests should pass, covering:
277+
All 38 tests should pass, covering:
200278
- 8 tests for tile type fallbacks
201279
- 4 tests for terrain type fallback
280+
- 6 tests for layer sourceType fallback (individual function)
281+
- 10 tests for layer tree fallbacks (`applyLayerFallbacks()`)
202282
- 10 tests for the main `applyFallbacks()` function
203283

204284
## Future Removal
@@ -210,9 +290,15 @@ This fallback system is temporary and will be removed in a future release when:
210290

211291
## Notes
212292

213-
- **Token Check**: Fallbacks only apply when `sceneProperty.default?.ion` is falsy (undefined, null, or empty string)
214-
- **Immutability**: The original `sceneProperty` is never modified. All transformations return new objects.
293+
- **Token Check**: Fallbacks only apply when `sceneProperty.default?.ion` is falsy (undefined, null, or empty string).
294+
- **Data Flow Separation**:
295+
- **Scene Property Fallbacks**: Applied to tiles and terrain in `applyFallbacks()` function in hooks.ts
296+
- **Layer Fallbacks**: Applied to entire layer tree in `applyLayerFallbacks()` function in hooks.ts
297+
- Both are applied early in the data pipeline, before the data reaches rendering components
298+
- **Immutability**: The original `sceneProperty` and `rootLayer` are never modified. All transformations return new objects.
215299
- **Performance**: Uses `useMemo` to avoid re-computing fallbacks on every render.
216-
- **Deep cloning**: Uses `lodash-es/cloneDeep` to ensure nested objects are properly cloned.
217-
- **Selective Fallback**: Only specific Cesium Ion asset IDs are mapped to fallbacks. Unknown asset IDs remain unchanged.
300+
- **Deep cloning**: Uses `lodash-es/cloneDeep` to ensure nested objects are properly cloned for scene properties.
301+
- **Shallow cloning for layers**: Layer fallbacks use shallow cloning with object spread for better performance.
302+
- **Recursive transformation**: Layer fallbacks recursively transform the entire layer tree, including all children.
303+
- **Selective Fallback**: Only specific Cesium Ion asset IDs and sourceTypes are mapped to fallbacks. Unknown types remain unchanged.
218304
- **No Double Fallback**: If backward compatibility already changed the type to something other than `cesium_ion`, fallbacks won't apply.

0 commit comments

Comments
 (0)