Skip to content

Commit b50fc51

Browse files
committed
save refactor
1 parent e930f19 commit b50fc51

9 files changed

Lines changed: 57 additions & 185 deletions

File tree

src/three/plugins/images/PMTilesPlugin.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,38 @@ export class PMTilesPlugin extends EllipsoidProjectionTilesPlugin {
1313

1414
}
1515

16+
// Intercept pmtiles:// URLs and fetch from the PMTiles archive
1617
fetchData( url, options ) {
1718

1819
if ( url.startsWith( 'pmtiles://' ) ) {
1920

20-
return this.imageSource.fetchInternal( url, options );
21+
const parts = url.split( '/' );
22+
const y = parseInt( parts.pop() );
23+
const x = parseInt( parts.pop() );
24+
const z = parseInt( parts.pop() );
25+
26+
return this.imageSource.instance.getZxy( z, x, y, options?.signal )
27+
.then( res => {
28+
29+
if ( ! res || ! res.data ) {
30+
31+
return new ArrayBuffer( 0 );
32+
33+
}
34+
35+
const data = res.data;
36+
37+
// Handle both ArrayBuffer and Uint8Array
38+
if ( data instanceof ArrayBuffer ) {
39+
40+
return data;
41+
42+
}
43+
44+
// Uint8Array - copy to new ArrayBuffer to avoid shared buffer issues
45+
return data.slice().buffer;
46+
47+
} );
2148

2249
}
2350

src/three/plugins/images/sources/PMTilesImageSource.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ export class PMTilesImageSource {
1616

1717
init(): Promise<void>;
1818
getUrl( x: number, y: number, level: number ): string;
19-
fetchInternal( url: string, options: { signal?: AbortSignal } ): Promise<ArrayBuffer | null>;
20-
processBufferToTexture( buffer: ArrayBuffer ): Promise<Texture>;
19+
fetchItem( tokens: [ number, number, number ], signal?: AbortSignal ): Promise<Texture>;
2120

2221
}

src/three/plugins/images/sources/PMTilesImageSource.js

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,60 @@
11
import { MVTImageSource } from './MVTImageSource.js';
2-
import { PMTilesFetcher } from './fetchers/PMTilesFetcher.js';
32
import { ProjectionScheme } from '../utils/ProjectionScheme.js';
3+
import { PMTiles } from 'pmtiles';
44

55
export class PMTilesImageSource extends MVTImageSource {
66

77
constructor( options = {} ) {
88

99
super( options );
1010

11-
// Use composed fetcher
12-
this._fetcher = new PMTilesFetcher( { url: options.url } );
11+
this.pmtilesUrl = options.url.replace( /^pmtiles:\/\//, '' );
12+
this.instance = new PMTiles( this.pmtilesUrl );
1313
this.tiling.flipY = true;
1414

1515
}
1616

17-
get pmtilesUrl() {
18-
19-
return this._fetcher.pmtilesUrl;
20-
21-
}
22-
23-
get instance() {
24-
25-
return this._fetcher.instance;
26-
27-
}
28-
2917
getUrl( x, y, level ) {
3018

31-
return this._fetcher.getUrl( x, y, level );
19+
return `pmtiles://${level}/${x}/${y}`;
3220

3321
}
3422

3523
async init() {
3624

37-
const metadata = await this._fetcher.init();
25+
const header = await this.instance.getHeader();
3826
this.tiling.setProjection( new ProjectionScheme( 'EPSG:3857' ) );
39-
this.tiling.generateLevels( metadata.maxZoom, this.tiling.projection.tileCountX, this.tiling.projection.tileCountY, {
27+
this.tiling.generateLevels( header.maxZoom, this.tiling.projection.tileCountX, this.tiling.projection.tileCountY, {
4028
tilePixelWidth: this.tileDimension,
4129
tilePixelHeight: this.tileDimension,
4230
} );
4331

4432
}
4533

46-
async fetchInternal( url, options ) {
34+
// Override fetchItem to fetch directly from PMTiles archive (bypasses plugin fetchData chain)
35+
fetchItem( tokens, signal ) {
36+
37+
const [ x, y, level ] = tokens;
38+
39+
return this.instance.getZxy( level, x, y, signal )
40+
.then( res => {
41+
42+
if ( ! res || ! res.data ) {
43+
44+
return this._createEmptyTexture();
45+
46+
}
47+
48+
// res.data is Uint8Array - convert to ArrayBuffer for processBufferToTexture
49+
const data = res.data;
50+
const buffer = data.buffer.slice(
51+
data.byteOffset,
52+
data.byteOffset + data.byteLength
53+
);
4754

48-
const parts = url.split( '/' );
49-
const y = parseInt( parts.pop() );
50-
const x = parseInt( parts.pop() );
51-
const z = parseInt( parts.pop() );
55+
return this.processBufferToTexture( buffer );
5256

53-
return this._fetcher.fetchTile( x, y, z, options.signal );
57+
} );
5458

5559
}
5660

src/three/plugins/images/sources/fetchers/HTTPTileFetcher.d.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/three/plugins/images/sources/fetchers/HTTPTileFetcher.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/three/plugins/images/sources/fetchers/PMTilesFetcher.d.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/three/plugins/images/sources/fetchers/PMTilesFetcher.js

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/three/plugins/images/sources/fetchers/TileFetcher.d.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/three/plugins/images/sources/fetchers/TileFetcher.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)