diff --git a/js/id/actions/merge_polygon.js b/js/id/actions/merge_polygon.js index d4fded992..85579e79b 100644 --- a/js/id/actions/merge_polygon.js +++ b/js/id/actions/merge_polygon.js @@ -26,15 +26,7 @@ iD.actions.MergePolygon = function(ids, newRelationId) { // ids - an array of ids of entities that are part of that polygon // locs - an array of the locations forming the polygon var polygons = entities.multipolygon.reduce(function(polygons, m) { - m.multipolygon(graph).forEach(function(group) { - group.forEach(function(ring) { - polygons.push({ - ids: ring.ids, - locs: ring - }); - }); - }); - return polygons; + return polygons.concat(m.joinMemberWays(null, graph)); }, []).concat(entities.closedWay.map(function(d) { return { ids: [d.id], diff --git a/js/id/core/relation.js b/js/id/core/relation.js index 90de844e8..c86bf1186 100644 --- a/js/id/core/relation.js +++ b/js/id/core/relation.js @@ -168,59 +168,6 @@ _.extend(iD.Relation.prototype, { .filter(function(m) { return m.type === 'way' && resolver.entity(m.id); }) .map(function(m) { return { role: m.role || 'outer', id: m.id, nodes: resolver.childNodes(resolver.entity(m.id)) }; }); - function join(ways) { - var joined = [], way, current, first, last, i, how, what; - - while (ways.length) { - way = ways.pop(); - current = way.nodes.slice(); - current.ids = [way.id]; - joined.push(current); - - while (ways.length && _.first(current) !== _.last(current)) { - first = _.first(current); - last = _.last(current); - - for (i = 0; i < ways.length; i++) { - what = ways[i].nodes; - - if (last === _.first(what)) { - how = current.push; - what = what.slice(1); - break; - } else if (last === _.last(what)) { - how = current.push; - what = what.slice(0, -1).reverse(); - break; - } else if (first == _.last(what)) { - how = current.unshift; - what = what.slice(0, -1); - break; - } else if (first == _.first(what)) { - how = current.unshift; - what = what.slice(1).reverse(); - break; - } else { - what = how = null; - } - } - - if (!what) - break; // Invalid geometry (unclosed ring) - - current.ids.push(ways[i].id); - ways.splice(i, 1); - how.apply(current, what); - } - } - - return joined.map(function(nodes) { - var locs = _.pluck(nodes, 'loc'); - locs.ids = nodes.ids; - return locs; - }); - } - function findOuter(inner) { var o, outer; @@ -237,8 +184,8 @@ _.extend(iD.Relation.prototype, { } } - var outers = join(members.filter(function(m) { return m.role === 'outer'; })), - inners = join(members.filter(function(m) { return m.role === 'inner'; })), + var outers = _.pluck(this.joinMemberWays(members.filter(function(m) { return m.role === 'outer'; })), 'locs'), + inners = _.pluck(this.joinMemberWays(members.filter(function(m) { return m.role === 'inner'; })), 'locs'), result = outers.map(function(o) { return [o]; }); for (var i = 0; i < inners.length; i++) { @@ -250,5 +197,68 @@ _.extend(iD.Relation.prototype, { } return result; + }, + + joinMemberWays: function(ways, resolver) { + var joined = [], way, current, first, last, i, how, what; + + ways = ways || this.members.filter(function(m) { + return m.type === 'way'; + }).map(function(m) { + return { + id: m.id, + nodes: resolver.childNodes(resolver.entity(m.id)) + }; + }); + + while (ways.length) { + way = ways.pop(); + current = way.nodes.slice(); + current.ids = [way.id]; + joined.push(current); + + while (ways.length && _.first(current) !== _.last(current)) { + first = _.first(current); + last = _.last(current); + + for (i = 0; i < ways.length; i++) { + what = ways[i].nodes; + + if (last === _.first(what)) { + how = current.push; + what = what.slice(1); + break; + } else if (last === _.last(what)) { + how = current.push; + what = what.slice(0, -1).reverse(); + break; + } else if (first == _.last(what)) { + how = current.unshift; + what = what.slice(0, -1); + break; + } else if (first == _.first(what)) { + how = current.unshift; + what = what.slice(1).reverse(); + break; + } else { + what = how = null; + } + } + + if (!what) + break; // Invalid geometry (unclosed ring) + + current.ids.push(ways[i].id); + ways.splice(i, 1); + how.apply(current, what); + } + } + return joined.map(function(nodes) { + return { + ids: nodes.ids, + locs: _.pluck(nodes, 'loc') + }; + }); } + }); diff --git a/test/index.html b/test/index.html index c2f9662a1..973180028 100644 --- a/test/index.html +++ b/test/index.html @@ -193,6 +193,7 @@ +