mapcomplete/Logic/FeatureSource/Sources/TouchesBboxFeatureSource.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

41 lines
1.5 KiB
TypeScript
Raw Normal View History

import {FeatureSource, FeatureSourceForLayer} from "../FeatureSource"
2023-03-28 05:13:48 +02:00
import StaticFeatureSource from "./StaticFeatureSource"
import {BBox} from "../../BBox"
2023-03-28 05:13:48 +02:00
import FilteredLayer from "../../../Models/FilteredLayer"
import {Store} from "../../UIEventSource"
import {Feature} from "geojson";
2023-03-28 05:13:48 +02:00
/**
* Results in a feature source which has all the elements that touch the given features
*/
export default class BBoxFeatureSource<T extends Feature = Feature> extends StaticFeatureSource<T> {
constructor(features: FeatureSource<T>, mustTouch: Store<BBox>) {
2023-03-28 05:13:48 +02:00
super(
features.features.mapD(
(features) => {
if (mustTouch.data === undefined) {
return features
}
const box = mustTouch.data
return features.filter((feature) => {
if (feature.geometry.type === "Point") {
return box.contains(<[number, number]>feature.geometry.coordinates)
}
return box.overlapsWith(BBox.get(feature))
})
},
[mustTouch]
2023-03-28 05:13:48 +02:00
)
)
}
}
export class BBoxFeatureSourceForLayer<T extends Feature = Feature> extends BBoxFeatureSource<T> implements FeatureSourceForLayer {
readonly layer: FilteredLayer
constructor(features: FeatureSourceForLayer<T>, mustTouch: Store<BBox>) {
2023-03-28 05:13:48 +02:00
super(features, mustTouch)
this.layer = features.layer
}
}