Support merging points into an area (#435)

This commit is contained in:
John Firebaugh
2013-02-06 13:31:06 -08:00
parent d7639acb22
commit bd8c9d6a00
6 changed files with 73 additions and 4 deletions
+1
View File
@@ -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>
+35
View File
@@ -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;
};
+14 -4
View File
@@ -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";
+2
View File
@@ -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>
@@ -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>
+1
View File
@@ -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>
+20
View File
@@ -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'}]);
});
});