Changes to replaceNode and add tests, also stricter isClosed

This commit is contained in:
Bryan Housel
2017-01-09 18:16:30 -05:00
parent f510038791
commit 7b86afc9de
2 changed files with 78 additions and 41 deletions
+18 -12
View File
@@ -122,7 +122,7 @@ _.extend(osmWay.prototype, {
isClosed: function() {
return this.nodes.length > 0 && this.first() === this.last();
return this.nodes.length > 1 && this.first() === this.last();
},
@@ -286,8 +286,10 @@ _.extend(osmWay.prototype, {
// Replaces each occurrence of node id needle with replacement.
// Consecutive duplicates are eliminated including existing ones.
// Circularity is preserved.
replaceNode: function(needle, replacement) {
var nodes = this.nodes.slice();
var nodes = this.nodes.slice(),
isClosed = this.isClosed();
for (var i = 0; i < nodes.length; i++) {
if (nodes[i] === needle) {
@@ -296,7 +298,12 @@ _.extend(osmWay.prototype, {
}
nodes = nodes.filter(noRepeatNodes);
// checkCircular(this, nodes);
// If the way was closed before, append a connector node to keep it closed..
if (isClosed && (nodes.length === 1 || nodes[0] !== nodes[nodes.length - 1])) {
nodes.push(nodes[0]);
}
return this.update({nodes: nodes});
},
@@ -305,12 +312,18 @@ _.extend(osmWay.prototype, {
// Consecutive duplicates are eliminated including existing ones.
// Circularity is preserved.
removeNode: function(id) {
var nodes = this.nodes.slice();
var nodes = this.nodes.slice(),
isClosed = this.isClosed();
nodes = nodes.filter(function(node, i, arr) {
return node !== id && noRepeatNodes(node, i, arr);
});
// checkCircular(this, nodes);
// If the way was closed before, append a connector node to keep it closed..
if (isClosed && (nodes.length === 1 || nodes[0] !== nodes[nodes.length - 1])) {
nodes.push(nodes[0]);
}
return this.update({nodes: nodes});
},
@@ -385,10 +398,3 @@ _.extend(osmWay.prototype, {
function noRepeatNodes(node, i, arr) {
return i === 0 || node !== arr[i - 1];
}
// If the nodelist was circular before, keep it circular.
function keepCircular(nodes) {
if ((nodes.length === 1 || nodes[0] !== nodes[nodes.length - 1])) {
nodes.push(nodes[0]);
}
}