forked from MapComplete/MapComplete
Add flag to generate new items only once
This commit is contained in:
parent
9c9be0918b
commit
9f91d30147
2 changed files with 47 additions and 46 deletions
|
@ -13,7 +13,9 @@ export default class ChangeApplicator implements FeatureSource {
|
||||||
public readonly features: UIEventSource<{ feature: any; freshness: Date }[]>;
|
public readonly features: UIEventSource<{ feature: any; freshness: Date }[]>;
|
||||||
public readonly name: string;
|
public readonly name: string;
|
||||||
|
|
||||||
constructor(source: FeatureSource, changes: Changes) {
|
constructor(source: FeatureSource, changes: Changes, mode?: {
|
||||||
|
generateNewGeometries: boolean
|
||||||
|
}) {
|
||||||
|
|
||||||
this.name = "ChangesApplied(" + source.name + ")"
|
this.name = "ChangesApplied(" + source.name + ")"
|
||||||
this.features = source.features
|
this.features = source.features
|
||||||
|
@ -21,10 +23,10 @@ export default class ChangeApplicator implements FeatureSource {
|
||||||
const self = this;
|
const self = this;
|
||||||
let runningUpdate = false;
|
let runningUpdate = false;
|
||||||
source.features.addCallbackAndRunD(features => {
|
source.features.addCallbackAndRunD(features => {
|
||||||
if(runningUpdate){
|
if (runningUpdate) {
|
||||||
return; // No need to ping again
|
return; // No need to ping again
|
||||||
}
|
}
|
||||||
ChangeApplicator.ApplyChanges(features, changes.pendingChanges.data)
|
ChangeApplicator.ApplyChanges(features, changes.pendingChanges.data, mode)
|
||||||
seenChanges.clear()
|
seenChanges.clear()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -33,7 +35,7 @@ export default class ChangeApplicator implements FeatureSource {
|
||||||
changes = changes.filter(ch => !seenChanges.has(ch))
|
changes = changes.filter(ch => !seenChanges.has(ch))
|
||||||
changes.forEach(c => seenChanges.add(c))
|
changes.forEach(c => seenChanges.add(c))
|
||||||
console.log("Called back", changes)
|
console.log("Called back", changes)
|
||||||
ChangeApplicator.ApplyChanges(self.features.data, changes)
|
ChangeApplicator.ApplyChanges(self.features.data, changes, mode)
|
||||||
source.features.ping()
|
source.features.ping()
|
||||||
runningUpdate = false;
|
runningUpdate = false;
|
||||||
})
|
})
|
||||||
|
@ -45,9 +47,9 @@ export default class ChangeApplicator implements FeatureSource {
|
||||||
/**
|
/**
|
||||||
* Returns true if the geometry is changed and the source should be pinged
|
* Returns true if the geometry is changed and the source should be pinged
|
||||||
*/
|
*/
|
||||||
private static ApplyChanges(features: {feature: any, freshness: Date}[], cs: ChangeDescription[]): boolean {
|
private static ApplyChanges(features: { feature: any; freshness: Date }[], cs: ChangeDescription[], mode: { generateNewGeometries: boolean }): boolean {
|
||||||
if (cs.length === 0 || features === undefined ) {
|
if (cs.length === 0 || features === undefined) {
|
||||||
return ;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("Applying changes ", this.name, cs)
|
console.log("Applying changes ", this.name, cs)
|
||||||
|
@ -75,45 +77,47 @@ export default class ChangeApplicator implements FeatureSource {
|
||||||
|
|
||||||
// First, create the new features - they have a negative ID
|
// First, create the new features - they have a negative ID
|
||||||
// We don't set the properties yet though
|
// We don't set the properties yet though
|
||||||
changesPerId.forEach(cs => {
|
if (mode?.generateNewGeometries) {
|
||||||
cs.forEach(change => {
|
changesPerId.forEach(cs => {
|
||||||
if (change.id >= 0) {
|
cs
|
||||||
return; // Nothing to do here, already created
|
.forEach(change => {
|
||||||
}
|
if (change.id >= 0) {
|
||||||
|
return; // Nothing to do here, already created
|
||||||
|
}
|
||||||
|
|
||||||
if(change.changes === undefined){
|
if (change.changes === undefined) {
|
||||||
// An update to the object - not the actual created
|
// An update to the object - not the actual created
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
switch (change.type) {
|
switch (change.type) {
|
||||||
case "node":
|
case "node":
|
||||||
const n = new OsmNode(change.id)
|
const n = new OsmNode(change.id)
|
||||||
n.lat = change.changes["lat"]
|
n.lat = change.changes["lat"]
|
||||||
n.lon = change.changes["lon"]
|
n.lon = change.changes["lon"]
|
||||||
const geojson = n.asGeoJson()
|
const geojson = n.asGeoJson()
|
||||||
add(geojson)
|
add(geojson)
|
||||||
break;
|
break;
|
||||||
case "way":
|
case "way":
|
||||||
const w = new OsmWay(change.id)
|
const w = new OsmWay(change.id)
|
||||||
w.nodes = change.changes["nodes"]
|
w.nodes = change.changes["nodes"]
|
||||||
add(w.asGeoJson())
|
add(w.asGeoJson())
|
||||||
break;
|
break;
|
||||||
case "relation":
|
case "relation":
|
||||||
const r = new OsmRelation(change.id)
|
const r = new OsmRelation(change.id)
|
||||||
r.members = change.changes["members"]
|
r.members = change.changes["members"]
|
||||||
add(r.asGeoJson())
|
add(r.asGeoJson())
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e)
|
console.error(e)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
}
|
||||||
|
|
||||||
|
|
||||||
for (const feature of features) {
|
for (const feature of features) {
|
||||||
const f = feature.feature;
|
const f = feature.feature;
|
||||||
|
@ -133,9 +137,6 @@ export default class ChangeApplicator implements FeatureSource {
|
||||||
// Apply tag changes and ping the consumers
|
// Apply tag changes and ping the consumers
|
||||||
const k = kv.k
|
const k = kv.k
|
||||||
let v = kv.v
|
let v = kv.v
|
||||||
if (v === "") {
|
|
||||||
v = undefined;
|
|
||||||
}
|
|
||||||
f.properties[k] = v;
|
f.properties[k] = v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -43,7 +43,7 @@ export default class FeaturePipeline implements FeatureSource {
|
||||||
new FeatureDuplicatorPerLayer(flayers,
|
new FeatureDuplicatorPerLayer(flayers,
|
||||||
new RegisteringFeatureSource(
|
new RegisteringFeatureSource(
|
||||||
new ChangeApplicator(
|
new ChangeApplicator(
|
||||||
updater, changes
|
updater, changes, {generateNewGeometries: true}
|
||||||
))
|
))
|
||||||
)), layout));
|
)), layout));
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue