Skip to content

Commit

Permalink
improved(core): Enhanced [Provider.clear](https://heremaps.github.io/…
Browse files Browse the repository at this point in the history
…xyz-maps/docs/classes/core.featureprovider.html#clear) to support more flexible inputs, including bounding boxes and tiles.

simplified tile clearing logic by improving inheritance structure.

Signed-off-by: Tim Deubler <[email protected]>
  • Loading branch information
TerminalTim committed Nov 29, 2024
1 parent 3dc78e7 commit 78dbfca
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {ClusterFeature} from '../../features/ClusterFeature';
import {LocalProvider, LocalProviderOptions} from '../LocalProvider';
import {Feature} from '../../features/Feature';
import {GeoJSONBBox, GeoJSONFeature} from '../../features/GeoJSON';
import Tile from '../../tile/Tile';

export type GeoJSONPointFeature = GeoJSONFeature<'Point'>;

Expand All @@ -40,8 +41,8 @@ export class ClusterProvider extends LocalProvider {
return feature.properties.clusterSize ? ClusterFeature : Feature;
}

clear(bbox?: GeoJSONBBox, triggerEvent?: boolean): string[] | null {
const tiles = super.clear(bbox, triggerEvent);
clear(bbox?: GeoJSONBBox | Tile | Tile[], triggerEvent?: boolean): string[] | null {
const tiles = super.clear(bbox);
if (bbox) {
for (const qk of tiles) {
const tile = this.getCachedTile(qk);
Expand Down
52 changes: 9 additions & 43 deletions packages/core/src/providers/FeatureProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -923,59 +923,25 @@ export class FeatureProvider extends Provider {
}
}

/**
* Clear all tiles and features of a given bounding box or do a full wipe if no parameter is given.
*
* @param bbox - array of geographical coordinates [minLon, minLat, maxLon, maxLat] defining the area to clear.
*/
clear(bbox?: number[]): string[] | null;

clear(bbox?: number[], triggerEvent?: boolean): string[] | null;
clear(bbox?: number[], triggerEvent: boolean = true): string[] | null {
clear(bbox?: GeoJSONBBox | Tile | Tile[], triggerEvent?: boolean): string[] | null {
const provider = this;
let dataQuads = null;

if (arguments.length > 3) {
bbox = Array.prototype.slice.call(arguments, 0, 4);
}

if ( // wipe all cached tiles containing provided bbox
bbox instanceof Array
) {
dataQuads = provider.getCachedTiles(bbox, provider.level);

for (let d = 0, tile; d < dataQuads.length; d++) {
tile = dataQuads[d];

provider._removeTile(tile, false);

dataQuads[d] = tile.quadkey;
}
} else { // full wipe
if (!bbox) {
// full wipe
provider.tree?.clear();

const featuresInfo = provider.fReg.toArray();
provider.fReg.clear();
const {fReg} = provider;
const featuresInfo = fReg.toArray();
fReg.clear();

for (const feature of featuresInfo) {
if (!provider.isDroppable(feature)) {
provider.fReg.set(feature.id, new FeatureRegistryInfo(feature));

fReg.set(feature.id, new FeatureRegistryInfo(feature));
provider.tree?.insert(feature);
}
}

provider.cnt = provider.fReg.size();

// Provider clears complete tile storage
Provider.prototype.clear.call(this, bbox);
provider.cnt = fReg.size();
}

if (triggerEvent) {
provider.dispatchEvent('clear', {tiles: dataQuads});
}

return dataQuads;
return super.clear(bbox, triggerEvent);
};

_insert(feature: GeoJSONFeature): Feature {
Expand Down
31 changes: 0 additions & 31 deletions packages/core/src/providers/ImageProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import GenericLoader from '../loaders/Manager';
import {HTTPLoader} from '../loaders/HTTPLoader';
import {TileLoadDelegator} from './RemoteTileProvider/TileLoadDelegator';
import {ImageProviderOptions} from './ImageProviderOptions';
import {FixedLevelTileLoadDelegator} from './RemoteTileProvider/FixedLevelTileLoadDelegator';

/**
* Tile Provider for Image/Raster data.
Expand Down Expand Up @@ -104,36 +103,6 @@ export class ImageProvider extends TileProvider {
this.tileLoader.drop(tile);
};

/**
* Clear tiles in a given bounding box or all tiles called without parameter.
*
* @param bbox - array of geographical coordinates [minLon, minLat, maxLon, maxLat] defining the area to clear.
*/
clear(bbox ?: number[]) {
const provider = this;
let dataQuads = null;

if ( // wipe all cached tiles containing provided bbox
bbox instanceof Array
) {
dataQuads = provider.getCachedTiles(bbox, provider.level);

for (let d = 0, tile; d < dataQuads.length; d++) {
tile = dataQuads[d];

this.tileLoader.drop(tile);
// provider.storage.remove(tile);

dataQuads[d] = tile.quadkey;
}
} else if (arguments.length == 0) {
this.storage.clear();
}

provider.dispatchEvent('clear', {tiles: dataQuads});
}
;

/**
* Cancel ongoing request(s) and drop the tile.
*
Expand Down
47 changes: 44 additions & 3 deletions packages/core/src/providers/TileProvider/TileProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {TileStorage} from '../../storage/TileStorage';
import Level2TileStorage from '../../storage/Level2Storage';
import {Listener} from '@here/xyz-maps-common';
import {TileProviderOptions} from './TileProviderOptions';
import {GeoJSONBBox} from '@here/xyz-maps-core';

const TILESIZE = 256;
const DEFAULT_EXPIRE_SECONDS = Infinity;
Expand Down Expand Up @@ -147,10 +148,50 @@ export default abstract class TileProvider {
}

/**
* Clear all features in.
* Clears the given tile(s) from the provider's cache.
*
* @param tiles - A single `Tile` or an array of `Tile`s to be cleared.
*/
clear(tiles: Tile | Tile[]): void;
/**
* Clears all tiles within a specified geographical bounding box.
*
* @param bbox - An array of geographical coordinates `[minLon, minLat, maxLon, maxLat]`
* defining the rectangular area from which tiles will be cleared.
*/
clear(bbox: GeoJSONBBox): void;
/**
* Clears all cached tiles of the provider.
*/
clear(bbox?) {
this.storage.clear();
clear(): void;
clear(bbox?: GeoJSONBBox | Tile | Tile[], triggerEvents?: boolean): string[]|null;
clear(bbox?: GeoJSONBBox | Tile | Tile[], triggerEvents: boolean = true): string[]|null {
// this.storage.clear();
const provider = this;
let clearTiles = null;

if (!bbox) {
provider.storage.clear();
} else {
if (bbox instanceof Tile) {
bbox = [bbox];
}
if (Array.isArray(bbox)) {
clearTiles = typeof bbox[0] == 'number'
? provider.getCachedTiles(bbox as number[], provider.level)
: bbox;

for (let d = 0, tile; d < clearTiles.length; d++) {
tile = clearTiles[d];
this._removeTile(tile);
clearTiles[d] = tile.quadkey;
}
}
}
if (triggerEvents) {
this.dispatchEvent('clear', {tiles: clearTiles});
}
return clearTiles;
};

/**
Expand Down

0 comments on commit 78dbfca

Please sign in to comment.