Keep the oldest ID alive when merging polygons

This commit is contained in:
Thomas Petillon
2020-04-11 16:26:17 +02:00
parent 87ca2b09cc
commit 23b3bc27b6
2 changed files with 20 additions and 12 deletions

View File

@@ -1,6 +1,6 @@
import { geoPolygonContainsPolygon } from '../geo';
import { osmJoinWays, osmRelation } from '../osm';
import { utilArrayGroupBy, utilArrayIntersection, utilObjectOmit } from '../util';
import { utilArrayGroupBy, utilArrayIntersection, utilObjectOmit, utilOldestID } from '../util';
export function actionMergePolygon(ids, newRelationId) {
@@ -85,13 +85,21 @@ export function actionMergePolygon(ids, newRelationId) {
outer = !outer;
}
// Move all tags to one relation
var relation = entities.multipolygon[0] ||
osmRelation({ id: newRelationId, tags: { type: 'multipolygon' }});
// Move all tags to one relation.
// Keep the oldest multipolygon alive if it exists.
var relation;
if (entities.multipolygon.length > 0) {
var oldestID = utilOldestID(entities.multipolygon.map((entity) => entity.id));
relation = entities.multipolygon.find((entity) => entity.id === oldestID);
} else {
relation = osmRelation({ id: newRelationId, tags: { type: 'multipolygon' }});
}
entities.multipolygon.slice(1).forEach(function(m) {
relation = relation.mergeTags(m.tags);
graph = graph.remove(m);
entities.multipolygon.forEach(function(m) {
if (m.id !== relation.id) {
relation = relation.mergeTags(m.tags);
graph = graph.remove(m);
}
});
entities.closedWay.forEach(function(way) {

View File

@@ -68,15 +68,15 @@ describe('iD.actionMergePolygon', function () {
expect(r.members.length).to.equal(3);
});
it('creates a multipolygon from two multipolygon relations', function() {
graph = iD.actionMergePolygon(['w0', 'w1'], 'r')(graph);
graph = iD.actionMergePolygon(['w2', 'w5'], 'r2')(graph);
graph = iD.actionMergePolygon(['r', 'r2'])(graph);
it('creates a multipolygon from two multipolygon relations and keeps the oldest alive', function() {
graph = iD.actionMergePolygon(['w0', 'w1'], 'r2')(graph);
graph = iD.actionMergePolygon(['w2', 'w5'], 'r1')(graph);
graph = iD.actionMergePolygon(['r2', 'r1'])(graph);
// Delete other relation
expect(graph.hasEntity('r2')).to.equal(undefined);
var r = graph.entity('r');
var r = graph.entity('r1');
expect(find(r, 'w0').role).to.equal('outer');
expect(find(r, 'w1').role).to.equal('inner');
expect(find(r, 'w2').role).to.equal('outer');