Skip to content

Commit

Permalink
To ensure backwards compatibility, feature ID queries are now typeles…
Browse files Browse the repository at this point in the history
…s again.

Signed-off-by: Tim Deubler <[email protected]>
  • Loading branch information
TerminalTim committed Feb 15, 2024
1 parent eb2e075 commit 0f6df44
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 35 deletions.
54 changes: 19 additions & 35 deletions packages/core/src/providers/FeatureProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {TileProviderOptions} from './TileProvider/TileProviderOptions';
import {GeoPoint} from '../geo/GeoPoint';
import {GeoRect} from '../geo/GeoRect';
import {PathFinder} from '../route/Route';
import {FeatureRegistry, FeatureRegistryInfo} from './FeatureRegistry';


const REMOVE_FEATURE_EVENT = 'featureRemove';
Expand All @@ -41,25 +42,12 @@ const MODIFY_FEATURE_COORDINATES_EVENT = 'featureCoordinatesChange';

let UNDEF;


class FeatureStorageInfo {
feature: Feature;
tiles: Set<string>;

constructor(feature: Feature) {
this.feature = feature;
this.tiles = new Set();
}
}

// FeatureStorageInfo.prototype.cnt = 0;

/**
* Feature provider.
*
*/
export class FeatureProvider extends Provider {
fStore: Map<string | number, FeatureStorageInfo> = new Map();
private fReg: FeatureRegistry; // Map<string | number, FeatureStorageInfo> = new Map();

Feature: any;

Expand All @@ -82,6 +70,7 @@ export class FeatureProvider extends Provider {
super(options);

this.tree = new RTree(9);
this.fReg = new FeatureRegistry();

this.Feature = this.Feature || Feature;

Expand All @@ -107,7 +96,7 @@ export class FeatureProvider extends Provider {
unique[unique.length] = provider.getFeature(prepared.id);
}
} else {
// unkown feature
// unknown feature
console.warn('unkown feature detected..', feature.geometry.type, feature);
}
}
Expand Down Expand Up @@ -276,10 +265,7 @@ export class FeatureProvider extends Provider {
* Get all the features that are currently present in the provider.
*/
all(): Feature[] {
const data = new Array(this.cnt);
let i = 0;
this.fStore.forEach((f) => data[i++] = f.feature);
return data;
return this.fReg.toArray();
};

/**
Expand All @@ -290,7 +276,7 @@ export class FeatureProvider extends Provider {
* @returns the found feature or undefined if feature is not present.
*/
getFeature(id: string | number): Feature | undefined {
return this.fStore.get(id)?.feature;
return this.fReg.get(id)?.feature;
};

/**
Expand Down Expand Up @@ -539,8 +525,8 @@ export class FeatureProvider extends Provider {
* @param feature - Object literal containing "id" property.
* @returns the {@link Feature} if it is found, otherwise undefined
*/
exists(feature: { id: number | string }): Feature {
return this.fStore.get(feature.id)?.feature;
exists(feature: { id: number | string }): { feature?: Feature } {
return this.fReg.get(feature.id); // ?.feature;
};


Expand Down Expand Up @@ -639,7 +625,7 @@ export class FeatureProvider extends Provider {

this.cnt--;

this.fStore.delete(feature.id);
this.fReg.delete(feature.id);

this.tree?.remove(feature);

Expand Down Expand Up @@ -914,20 +900,18 @@ export class FeatureProvider extends Provider {
} else if (arguments.length == 0) { // full wipe
provider.tree?.clear();

const featuresInfo = Array.from(provider.fStore);
provider.fStore.clear();

for (const [id, featureInfo] of featuresInfo) {
const feature = featureInfo.feature;
const featuresInfo = provider.fReg.toArray();
provider.fReg.clear();

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

provider.tree?.insert(feature);
}
}

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

// Provider clears complete tile storage
Provider.prototype.clear.call(this, bbox);
Expand All @@ -949,7 +933,7 @@ export class FeatureProvider extends Provider {
if (!filter || filter(feature)) {
// filter out the duplicates!!
if (
!this.fStore.has(id)
!this.fReg.has(id)
) { // not in tree already??
this.cnt++;

Expand All @@ -963,7 +947,7 @@ export class FeatureProvider extends Provider {

this.updateBBox(feature);

this.fStore.set(id, new FeatureStorageInfo(feature as Feature));
this.fReg.set(String(id), new FeatureRegistryInfo(feature as Feature));

inserted = feature;
}
Expand All @@ -974,7 +958,7 @@ export class FeatureProvider extends Provider {


_mark(o, tile: Tile) {
this.fStore.get(o.id)?.tiles.add(tile.quadkey);
this.fReg.get(o.id)?.tiles.add(tile.quadkey);
};


Expand All @@ -995,7 +979,7 @@ export class FeatureProvider extends Provider {

_dropFeature(feature: Feature, qk: string, trigger?: boolean) {
const provider = this;
const featureStoreInfo = provider.fStore.get(feature.id);
const featureStoreInfo = provider.fReg.get(feature.id);

if (featureStoreInfo) {
const tiles = featureStoreInfo.tiles;
Expand All @@ -1004,7 +988,7 @@ export class FeatureProvider extends Provider {
if (provider.isDroppable(feature)) {
if (!tiles.size) {
provider.cnt--;
provider.fStore.delete(feature.id);
provider.fReg.delete(feature.id);

provider.tree?.remove(feature);

Expand Down
66 changes: 66 additions & 0 deletions packages/core/src/providers/FeatureRegistry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (C) 2019-2023 HERE Europe B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* License-Filename: LICENSE
*/

import {Feature} from '../features/Feature';


export class FeatureRegistryInfo {
feature: Feature;
tiles: Set<string>;

constructor(feature: Feature) {
this.feature = feature;
this.tiles = new Set();
}
}

export class FeatureRegistry {
private _map: Map<string, FeatureRegistryInfo> = new Map();

has(id: string | number): boolean {
return this._map.has(String(id));
}

get(id: number | string): FeatureRegistryInfo {
return this._map.get(String(id));
}

set(id: string | number, featureStorageInfo: FeatureRegistryInfo) {
this._map.set(String(id), featureStorageInfo);
}

delete(id: string | number) {
this._map.delete(String(id));
}

toArray(): FeatureRegistryInfo['feature'][] {
const array = new Array(this._map.size);
let i = 0;
this._map.forEach((f) => array[i++] = f.feature);
return array;
}

size(): number {
return this._map.size;
}

clear() {
this._map.clear();
}
}

0 comments on commit 0f6df44

Please sign in to comment.