Refactoring: add generics to some featureSources

This commit is contained in:
Pieter Vander Vennet 2023-05-05 01:25:12 +02:00
parent e038394331
commit dec4296204
3 changed files with 21 additions and 38 deletions

View file

@ -18,7 +18,7 @@ export interface Tiled {
/**
* A feature source which only contains features for the defined layer
*/
export interface FeatureSourceForLayer extends FeatureSource {
export interface FeatureSourceForLayer<T extends Feature = Feature> extends FeatureSource<T> {
readonly layer: FilteredLayer
}

View file

@ -1,30 +1,28 @@
import { FeatureSource , FeatureSourceForLayer, Tiled } from "../FeatureSource"
import { ImmutableStore, Store } from "../../UIEventSource"
import FilteredLayer from "../../../Models/FilteredLayer"
import { BBox } from "../../BBox"
import { Feature } from "geojson"
import {FeatureSource} from "../FeatureSource"
import {ImmutableStore, Store} from "../../UIEventSource"
import {Feature} from "geojson"
/**
* A simple, read only feature store.
*/
export default class StaticFeatureSource implements FeatureSource {
public readonly features: Store<Feature[]>
export default class StaticFeatureSource<T extends Feature = Feature> implements FeatureSource<T> {
public readonly features: Store<T[]>
constructor(
features:
| Store<Feature[]>
| Feature[]
| { features: Feature[] }
| { features: Store<Feature[]> }
| Store<T[]>
| T[]
| { features: T[] }
| { features: Store<T[]> }
) {
if (features === undefined) {
throw "Static feature source received undefined as source"
}
let feats: Feature[] | Store<Feature[]>
let feats: T[] | Store<T[]>
if (features["features"]) {
feats = features["features"]
} else {
feats = <Feature[] | Store<Feature[]>>features
feats = <T[] | Store<T[]>>features
}
if (Array.isArray(feats)) {
@ -34,23 +32,7 @@ export default class StaticFeatureSource implements FeatureSource {
}
}
public static fromGeojson(geojson: Feature[]): StaticFeatureSource {
public static fromGeojson<T extends Feature>(geojson: T[]): StaticFeatureSource<T> {
return new StaticFeatureSource(geojson)
}
}
export class TiledStaticFeatureSource
extends StaticFeatureSource
implements Tiled, FeatureSourceForLayer
{
public readonly bbox: BBox = BBox.global
public readonly tileIndex: number
public readonly layer: FilteredLayer
constructor(features: Store<Feature[]>, layer: FilteredLayer, tileIndex: number = 0) {
super(features)
this.tileIndex = tileIndex
this.layer = layer
this.bbox = BBox.fromTileIndex(this.tileIndex)
}
}

View file

@ -1,14 +1,15 @@
import { FeatureSource, FeatureSourceForLayer } from "../FeatureSource"
import {FeatureSource, FeatureSourceForLayer} from "../FeatureSource"
import StaticFeatureSource from "./StaticFeatureSource"
import { BBox } from "../../BBox"
import {BBox} from "../../BBox"
import FilteredLayer from "../../../Models/FilteredLayer"
import { Store } from "../../UIEventSource"
import {Store} from "../../UIEventSource"
import {Feature} from "geojson";
/**
* Results in a feature source which has all the elements that touch the given features
*/
export default class BBoxFeatureSource extends StaticFeatureSource {
constructor(features: FeatureSource, mustTouch: Store<BBox>) {
export default class BBoxFeatureSource<T extends Feature = Feature> extends StaticFeatureSource<T> {
constructor(features: FeatureSource<T>, mustTouch: Store<BBox>) {
super(
features.features.mapD(
(features) => {
@ -29,10 +30,10 @@ export default class BBoxFeatureSource extends StaticFeatureSource {
}
}
export class BBoxFeatureSourceForLayer extends BBoxFeatureSource implements FeatureSourceForLayer {
export class BBoxFeatureSourceForLayer<T extends Feature = Feature> extends BBoxFeatureSource<T> implements FeatureSourceForLayer {
readonly layer: FilteredLayer
constructor(features: FeatureSourceForLayer, mustTouch: Store<BBox>) {
constructor(features: FeatureSourceForLayer<T>, mustTouch: Store<BBox>) {
super(features, mustTouch)
this.layer = features.layer
}