forked from MapComplete/MapComplete
Add polygon merging
This commit is contained in:
parent
ee38cdb9d7
commit
ee3e000cd1
11 changed files with 460 additions and 305 deletions
|
@ -2,43 +2,49 @@ import { Store, UIEventSource } from "../../UIEventSource"
|
|||
import { FeatureSource, IndexedFeatureSource } from "../FeatureSource"
|
||||
import { Feature } from "geojson"
|
||||
import { Utils } from "../../../Utils"
|
||||
import DynamicTileSource from "../TiledFeatureSource/DynamicTileSource"
|
||||
|
||||
/**
|
||||
*
|
||||
* The featureSourceMerger receives complete geometries from various sources.
|
||||
* If multiple sources contain the same object (as determined by 'id'), only one copy of them is retained
|
||||
*/
|
||||
export default class FeatureSourceMerger implements IndexedFeatureSource {
|
||||
export default class FeatureSourceMerger<Src extends FeatureSource = FeatureSource> implements IndexedFeatureSource {
|
||||
public features: UIEventSource<Feature[]> = new UIEventSource([])
|
||||
public readonly featuresById: Store<Map<string, Feature>>
|
||||
private readonly _featuresById: UIEventSource<Map<string, Feature>>
|
||||
private readonly _sources: FeatureSource[] = []
|
||||
protected readonly _featuresById: UIEventSource<Map<string, Feature>>
|
||||
private readonly _sources: Src[] = []
|
||||
/**
|
||||
* Merges features from different featureSources.
|
||||
* In case that multiple features have the same id, the latest `_version_number` will be used. Otherwise, we will take the last one
|
||||
*/
|
||||
constructor(...sources: FeatureSource[]) {
|
||||
constructor(...sources: Src[]) {
|
||||
this._featuresById = new UIEventSource<Map<string, Feature>>(new Map<string, Feature>())
|
||||
this.featuresById = this._featuresById
|
||||
const self = this
|
||||
sources = Utils.NoNull(sources)
|
||||
for (let source of sources) {
|
||||
source.features.addCallback(() => {
|
||||
self.addData(sources.map((s) => s.features.data))
|
||||
self.addDataFromSources(sources)
|
||||
})
|
||||
}
|
||||
this.addData(sources.map((s) => s.features.data))
|
||||
this.addDataFromSources(sources)
|
||||
this._sources = sources
|
||||
}
|
||||
|
||||
public addSource(source: FeatureSource) {
|
||||
public addSource(source: Src) {
|
||||
if (!source) {
|
||||
return
|
||||
}
|
||||
this._sources.push(source)
|
||||
source.features.addCallbackAndRun(() => {
|
||||
this.addData(this._sources.map((s) => s.features.data))
|
||||
this.addDataFromSources(this._sources)
|
||||
})
|
||||
}
|
||||
|
||||
protected addDataFromSources(sources: Src[]){
|
||||
this.addData(sources.map(s => s.features.data))
|
||||
}
|
||||
|
||||
protected addData(sources: Feature[][]) {
|
||||
sources = Utils.NoNull(sources)
|
||||
let somethingChanged = false
|
||||
|
@ -56,7 +62,7 @@ export default class FeatureSourceMerger implements IndexedFeatureSource {
|
|||
const id = f.properties.id
|
||||
unseen.delete(id)
|
||||
if (!all.has(id)) {
|
||||
// This is a new feature
|
||||
// This is a new, previously unseen feature
|
||||
somethingChanged = true
|
||||
all.set(id, f)
|
||||
continue
|
||||
|
@ -81,10 +87,8 @@ export default class FeatureSourceMerger implements IndexedFeatureSource {
|
|||
return
|
||||
}
|
||||
|
||||
const newList = []
|
||||
all.forEach((value) => {
|
||||
newList.push(value)
|
||||
})
|
||||
const newList = Array.from(all.values())
|
||||
|
||||
this.features.setData(newList)
|
||||
this._featuresById.setData(all)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue