Skip to content

Commit

Permalink
Support merging points into an area (#435)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Feb 6, 2013
1 parent d7639ac commit bd8c9d6
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 4 deletions.
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
<script src="js/id/actions/delete_way.js"></script>
<script src='js/id/actions/disconnect.js'></script>
<script src='js/id/actions/join.js'></script>
<script src='js/id/actions/merge.js'></script>
<script src='js/id/actions/move_node.js'></script>
<script src='js/id/actions/move_way.js'></script>
<script src='js/id/actions/circularize.js'></script>
Expand Down
35 changes: 35 additions & 0 deletions js/id/actions/merge.js
Original file line number Diff line number Diff line change
@@ -0,0 +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); }));
}

var action = function(graph) {
var geometries = groupEntitiesByGeometry(graph),
area = geometries['area'][0],
points = geometries['point'];

points.forEach(function (point) {
area = area.mergeTags(point.tags);

graph.parentRelations(point).forEach(function (parent) {
graph = graph.replace(parent.replaceMember(point, area));
});

graph = graph.remove(point);
});

graph = graph.replace(area);

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 action;
};
18 changes: 14 additions & 4 deletions js/id/operations/merge.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
iD.operations.Merge = function(selection, context) {
var action = iD.actions.Join(selection);
var join = iD.actions.Join(selection),
merge = iD.actions.Merge(selection);

var operation = function() {
var annotation = t('operations.merge.annotation', {n: selection.length}),
difference = context.perform(action, annotation);
action;

if (join.enabled(context.graph())) {
action = join;
} else {
action = merge;
}

var difference = context.perform(action, annotation);
context.enter(iD.modes.Select(context, difference.extantIDs()));
};

operation.available = function() {
return selection.length > 1;
return selection.length >= 2;
};

operation.enabled = function() {
return action.enabled(context.graph());
return join.enabled(context.graph()) ||
merge.enabled(context.graph());
};

operation.id = "merge";
Expand Down
2 changes: 2 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
<script src="../js/id/actions/delete_way.js"></script>
<script src='../js/id/actions/disconnect.js'></script>
<script src='../js/id/actions/join.js'></script>
<script src='../js/id/actions/merge.js'></script>
<script src='../js/id/actions/move_node.js'></script>
<script src='../js/id/actions/move_way.js'></script>
<script src='../js/id/actions/noop.js'></script>
Expand Down Expand Up @@ -157,6 +158,7 @@
<script src="spec/actions/delete_way.js"></script>
<script src='spec/actions/disconnect.js'></script>
<script src="spec/actions/join.js"></script>
<script src='spec/actions/merge.js'></script>
<script src="spec/actions/move_node.js"></script>
<script src="spec/actions/move_way.js"></script>
<script src="spec/actions/noop.js"></script>
Expand Down
1 change: 1 addition & 0 deletions test/index_packaged.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<script src="spec/actions/delete_way.js"></script>
<script src='spec/actions/disconnect.js'></script>
<script src="spec/actions/join.js"></script>
<script src='spec/actions/merge.js'></script>
<script src="spec/actions/move_node.js"></script>
<script src="spec/actions/move_way.js"></script>
<script src="spec/actions/noop.js"></script>
Expand Down
20 changes: 20 additions & 0 deletions test/spec/actions/merge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
describe("iD.actions.Merge", function () {
it("merges multiple points to an area", 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', tags: {area: 'yes'}}),
'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', area: 'yes'});
expect(graph.entity('r').members).to.eql([{id: 'w', role: 'r', type: 'way'}]);
});
});

0 comments on commit bd8c9d6

Please sign in to comment.