Support merging point + line

This commit is contained in:
John Firebaugh
2013-03-06 17:25:14 -08:00
parent 15c37b86b9
commit cb0e02d56f
2 changed files with 28 additions and 8 deletions
+9 -8
View File
@@ -1,34 +1,35 @@
iD.actions.Merge = function(ids) {
function groupEntitiesByGeometry(graph) {
var entities = ids.map(function(id) { return graph.entity(id); });
return _.extend({point: [], area: []}, _.groupBy(entities, function(entity) { return entity.geometry(graph); }));
return _.extend({point: [], area: [], line: [], relation: []},
_.groupBy(entities, function(entity) { return entity.geometry(graph); }));
}
var action = function(graph) {
var geometries = groupEntitiesByGeometry(graph),
area = geometries.area[0],
target = geometries.area[0] || geometries.line[0],
points = geometries.point;
points.forEach(function(point) {
area = area.mergeTags(point.tags);
target = target.mergeTags(point.tags);
graph.parentRelations(point).forEach(function(parent) {
graph = graph.replace(parent.replaceMember(point, area));
graph = graph.replace(parent.replaceMember(point, target));
});
graph = graph.remove(point);
});
graph = graph.replace(area);
graph = graph.replace(target);
return graph;
};
action.enabled = function(graph) {
var geometries = groupEntitiesByGeometry(graph);
return geometries.area.length === 1 &&
geometries.point.length > 0 &&
(geometries.area.length + geometries.point.length) === ids.length;
return geometries.point.length > 0 &&
(geometries.area.length + geometries.line.length) === 1 &&
geometries.relation.length === 0;
};
return action;
+19
View File
@@ -1,4 +1,23 @@
describe("iD.actions.Merge", function () {
it("merges multiple points to a line", function () {
var graph = iD.Graph({
'a': iD.Node({id: 'a', tags: {a: 'a'}}),
'b': iD.Node({id: 'b', tags: {b: 'b'}}),
'w': iD.Way({id: 'w'}),
'r': iD.Relation({id: 'r', members: [{id: 'a', role: 'r', type: 'node'}]})
}),
action = iD.actions.Merge(['a', 'b', 'w']);
expect(action.enabled(graph)).to.be.true;
graph = action(graph);
expect(graph.entity('a')).to.be.undefined;
expect(graph.entity('b')).to.be.undefined;
expect(graph.entity('w').tags).to.eql({a: 'a', b: 'b'});
expect(graph.entity('r').members).to.eql([{id: 'w', role: 'r', type: 'way'}]);
});
it("merges multiple points to an area", function () {
var graph = iD.Graph({
'a': iD.Node({id: 'a', tags: {a: 'a'}}),