diff --git a/modules/actions/merge_polygon.js b/modules/actions/merge_polygon.js index 8d5829401..0f29cd5b5 100644 --- a/modules/actions/merge_polygon.js +++ b/modules/actions/merge_polygon.js @@ -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) { diff --git a/test/spec/actions/merge_polygon.js b/test/spec/actions/merge_polygon.js index d3f7e0f4f..89814da98 100644 --- a/test/spec/actions/merge_polygon.js +++ b/test/spec/actions/merge_polygon.js @@ -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');