s2maps-gpu
Version:
S2 Maps GPU - An open source, high-performance, and GPU-accelerated map engine for rendering large-scale, interactive maps.
75 lines (74 loc) • 2.59 kB
JavaScript
import Source from './source.js';
import { TileStore } from 'gis-tools/index.js';
import { WMPointCluster } from './pointCluster/index.js';
/**
* # JSON Source
*
* ## Description
* A source that is a json file that contains geojson or s2json data.
*
* The json can be stored as a tile store or a point cluster.
*/
export default class JSONSource extends Source {
json;
/**
* @param mapID - the id of the map to build for
* @param metadata - the metadata for the source
*/
async build(mapID, metadata) {
const json = metadata?.data ?? (await this._fetch(`${this.path}`, mapID, true));
if (json === undefined) {
this.active = false;
console.error(`FAILED TO extrapolate ${this.path} json data`);
return;
}
if (metadata?.cluster === true) {
this.json = new WMPointCluster(metadata);
this.json.addManyPoints(json);
this.json.cluster();
}
else {
// use the projection from the style
this.json = new TileStore(json, {
...metadata,
projection: this.projection === 'S2' ? 'S2' : 'WG',
});
}
const { projection, minzoom, maxzoom, faces } = this.json;
this._buildMetadata({
type: 'vector',
minzoom,
maxzoom,
faces: [...faces],
layers: { default: { minzoom, maxzoom, drawTypes: [], shape: {} } },
extension: projection === 'S2' ? 's2json' : 'geojson',
attributions: 'attributions' in json ? json.attributions : {},
}, mapID);
}
/**
* Fetch a tile
* @param mapID - the id of the map requesting data
* @param tile - the tile request
*/
async _tileRequest(mapID, tile) {
const { name, json, session, textEncoder } = this;
const { id, face } = tile;
if (json.faces.has(face)) {
// grab the data
const vectorTile = json.getTile(id);
// prep worker
const worker = session.requestWorker();
if (Object.values(vectorTile?.layers ?? {}).length === 0) {
this._flush(mapID, tile, name);
return;
}
// compress
const data = await textEncoder.encode(JSON.stringify(vectorTile)).buffer;
// send off
worker.postMessage({ mapID, type: 'jsondata', tile, sourceName: name, data }, [data]);
}
else {
this._flush(mapID, tile, name);
}
}
}