Adding a feature filter function

This commit is contained in:
Eric van der Vlist 2023-01-19 14:04:32 +01:00
parent 66fc287e5c
commit 420d75520c
1 changed files with 14 additions and 4 deletions

View File

@ -6,8 +6,10 @@ import VectorTileSource from 'ol/source/VectorTile.js';
class ClusterableVectorTileSource extends VectorTileSource { class ClusterableVectorTileSource extends VectorTileSource {
extent: Extent | null = null; extent: Extent | null = null;
resolution: number | undefined;
extentProjected: Extent | null = null; extentProjected: Extent | null = null;
featuresSent: boolean = false; featuresSent: boolean = false;
featureFilter = (feature: Feature, resolution: number | undefined) => feature;
changeHandler = () => { changeHandler = () => {
this.featuresSent = false; this.featuresSent = false;
@ -20,7 +22,11 @@ class ClusterableVectorTileSource extends VectorTileSource {
constructor(properties: any) { constructor(properties: any) {
properties.format = new MVT({ featureClass: Feature }); properties.format = new MVT({ featureClass: Feature });
super(properties); const { featureFilter, ...otherProperties } = properties;
super(otherProperties);
if (featureFilter) {
this.featureFilter = featureFilter;
}
this.addEventListener('change', this.changeHandler); this.addEventListener('change', this.changeHandler);
console.log({ console.log({
caller: 'ClusterableVectorTileSource', caller: 'ClusterableVectorTileSource',
@ -42,6 +48,7 @@ class ClusterableVectorTileSource extends VectorTileSource {
projection, projection,
this: this, this: this,
}); });
this.resolution = resolution;
if (this.extent === null || !equals(this.extent, extent)) { if (this.extent === null || !equals(this.extent, extent)) {
this.extent = extent; this.extent = extent;
if (this.projection != null) { if (this.projection != null) {
@ -58,10 +65,11 @@ class ClusterableVectorTileSource extends VectorTileSource {
}; };
getFeatures = () => { getFeatures = () => {
const result = const result = (
this.extentProjected !== null this.extentProjected !== null
? super.getFeaturesInExtent(this.extentProjected) ? super.getFeaturesInExtent(this.extentProjected)
: []; : []
).filter((feature) => this.featureFilter(feature, this.resolution));
console.log({ console.log({
caller: 'ClusterableVectorTileSource', caller: 'ClusterableVectorTileSource',
method: 'getFeatures', method: 'getFeatures',
@ -74,7 +82,9 @@ class ClusterableVectorTileSource extends VectorTileSource {
}; };
getFeaturesInExtent = (extent: Extent) => { getFeaturesInExtent = (extent: Extent) => {
const features = super.getFeaturesInExtent(extent); const features = super
.getFeaturesInExtent(extent)
.filter((feature) => this.featureFilter(feature, this.resolution));
console.log({ console.log({
caller: 'ClusterableVectorTileSource', caller: 'ClusterableVectorTileSource',
method: 'getFeaturesInExtent', method: 'getFeaturesInExtent',