prevent creation of ways with duplicate segments or repeated nodes

This commit is contained in:
Ansis Brammanis
2013-03-11 16:00:24 -04:00
parent ab561ff198
commit 6129fbf4f5
3 changed files with 27 additions and 0 deletions

View File

@@ -103,13 +103,26 @@ iD.behavior.DragNode = function(context) {
if (cancelled) return;
off();
function adjacent(d) {
return _.any(context.graph().parentWays(entity).map(function(w) {
return w.areAdjacent(d.id, entity.id);
}));
}
var d = datum();
if (d.type === 'way') {
var choice = iD.geo.chooseIndex(d, d3.mouse(context.surface().node()), context);
context.replace(
iD.actions.MoveNode(entity.id, choice.loc),
iD.actions.AddVertex(d.id, entity.id, choice.index),
connectAnnotation(d));
} else if (d.type === 'node' && adjacent(d)) {
context.replace(
iD.actions.DeleteNode(entity.id),
t('operations.delete.annotation.vertex'));
} else if (d.type === 'node' && d.id !== entity.id) {
context.replace(
iD.actions.Connect([entity.id, d.id]),

View File

@@ -139,6 +139,10 @@ iD.behavior.DrawWay = function(context, wayId, index, mode, baseGraph) {
// Connect the way to an existing node and continue drawing.
drawWay.addNode = function(node) {
// Avoid creating duplicate segments
if (way.areAdjacent(node.id, way.nodes[way.nodes.length - 1])) return;
context.perform(
ReplaceTemporaryNode(node),
annotation);

View File

@@ -57,6 +57,16 @@ _.extend(iD.Way.prototype, {
return _.uniq(this.nodes).length < (this.isArea() ? 3 : 2);
},
areAdjacent: function(n1, n2) {
for (var i = 0; i < this.nodes.length; i++) {
if (this.nodes[i] === n1) {
if (this.nodes[i - 1] === n2) return true;
if (this.nodes[i + 1] === n2) return true;
}
}
return false;
},
geometry: function() {
return this.isArea() ? 'area' : 'line';
},