@@ -14,35 +14,35 @@ import {
1414} from '../services/mesh' ;
1515import { buildGltfDocument } from '../services/gltf' ;
1616import { calculateTileBounds , createRootTile } from '../services/tiles' ;
17+ import { getR2ObjectETag } from '../services/r2' ;
1718
1819type Bindings = {
19- R2_ACCOUNT_ID : string ;
20- R2_ACCESS_KEY_ID : string ;
21- R2_SECRET_ACCESS_KEY : string ;
22- R2_BUCKET_NAME : string ;
23- R2_ENDPOINT : string ;
2420 R2_PUBLIC_ARPENTRY_ENDPOINT : string ;
21+ KV_ARPENTRY : KVNamespace ;
2522} ;
2623
2724const glb = new Hono < { Bindings : Bindings } > ( ) ;
2825
26+ type Bounds = [ number , number , number , number ] ;
27+ type Coordinate = [ number , number ] ;
28+
2929// Configuration
3030const TILE_SIZE = 512 ;
3131const QUADTREE_MAX_LEVEL = 5 ;
3232
3333// Global coordinate system reference
34- let GLOBAL_BOUNDS : [ number , number , number , number ] | null = null ;
35- let TILESET_CENTER : [ number , number ] | null = null ;
34+ let GLOBAL_BOUNDS : Bounds | null = null ;
35+ let TILESET_CENTER : Coordinate | null = null ;
3636
3737/**
3838 * Tileset JSON endpoint - provides 3D Tiles structure
3939 */
4040glb . get (
4141 '/tileset.json' ,
42- cache ( {
43- cacheName : 'tileset' ,
44- cacheControl : 'max-age=3600' ,
45- } ) ,
42+ // cache({
43+ // cacheName: 'tileset',
44+ // cacheControl: 'max-age=3600',
45+ // }),
4646 async ( c ) => {
4747 console . log ( '🌍 Tileset JSON endpoint' ) ;
4848 try {
@@ -56,9 +56,7 @@ glb.get(
5656 console . log ( '🌍 Bounding box:' , bbox ) ;
5757
5858 console . log ( '🌍 Creating square bounds...' ) ;
59- const square = createSquareBounds (
60- bbox as [ number , number , number , number ] ,
61- ) ;
59+ const square = createSquareBounds ( bbox as Bounds ) ;
6260 console . log ( '🌍 Square bounds created' ) ;
6361 console . log ( '🌍 Square bounds:' , square ) ;
6462
@@ -69,6 +67,18 @@ glb.get(
6967 ( square [ 1 ] + square [ 3 ] ) / 2 ,
7068 ] ;
7169
70+ const globalBounds = await c . env . KV_ARPENTRY . put (
71+ 'global_bounds' ,
72+ JSON . stringify ( square ) ,
73+ ) ;
74+ const tilesetCenter = await c . env . KV_ARPENTRY . put (
75+ 'tileset_center' ,
76+ JSON . stringify ( TILESET_CENTER ) ,
77+ ) ;
78+
79+ console . log ( '🌍 Global bounds stored in KV' , globalBounds ) ;
80+ console . log ( '🌍 Tileset center stored in KV' , tilesetCenter ) ;
81+
7282 const minH = 0 ;
7383 const maxH = 4500 ; // Swiss terrain height range
7484
@@ -121,9 +131,31 @@ glb.get(
121131 return c . json ( { error : 'Invalid tile coordinates' } , 400 ) ;
122132 }
123133
134+ // Fetch and initialize global bounds and tileset center from KV if not initialized yet
124135 if ( ! GLOBAL_BOUNDS || ! TILESET_CENTER ) {
125- console . error ( '❌ Global bounds not initialized' ) ;
126- return c . json ( { error : 'Global bounds not available' } , 500 ) ;
136+ console . error (
137+ '❌ Global bounds and/or tileset center not initialized, fetching from KV...' ,
138+ ) ;
139+ try {
140+ const globalBounds = ( await c . env . KV_ARPENTRY . get (
141+ 'global_bounds' ,
142+ ) ) as string ;
143+ const tilesetCenter = ( await c . env . KV_ARPENTRY . get (
144+ 'tileset_center' ,
145+ ) ) as string ;
146+
147+ GLOBAL_BOUNDS = JSON . parse ( globalBounds ) as Bounds ;
148+ TILESET_CENTER = JSON . parse ( tilesetCenter ) as Coordinate ;
149+
150+ console . log (
151+ '🌍 KV fetched, global bounds and tileset center initialized' ,
152+ ) ;
153+ } catch ( err ) {
154+ const errorMessage =
155+ 'Failed to fetch from KV, global bounds and tileset center cannot be initialized' ;
156+ console . error ( '❌ ' , errorMessage , err ) ;
157+ return c . json ( { error : errorMessage } , 500 ) ;
158+ }
127159 }
128160
129161 const elevKey = 'swissalti3d/swissalti3d_web_mercator.tif' ;
0 commit comments