diff --git a/js/id/graph/entity.js b/js/id/graph/entity.js index 9c26d66e0..ae3520fab 100644 --- a/js/id/graph/entity.js +++ b/js/id/graph/entity.js @@ -68,24 +68,8 @@ iD.Entity.prototype = { return this._updated && this.osmId() > 0; }, - extent: function(resolver) { - if (this.type === 'node') { - return [this.loc, this.loc]; - } else if (this.type == 'way') { - return resolver.transient(this, 'extent', function() { - var extent = [[-Infinity, Infinity], [Infinity, -Infinity]]; - for (var i = 0, l = this.nodes.length; i < l; i++) { - var node = resolver.entity(this.nodes[i]); - if (node.loc[0] > extent[0][0]) extent[0][0] = node.loc[0]; - if (node.loc[0] < extent[1][0]) extent[1][0] = node.loc[0]; - if (node.loc[1] < extent[0][1]) extent[0][1] = node.loc[1]; - if (node.loc[1] > extent[1][1]) extent[1][1] = node.loc[1]; - } - return extent; - }); - } else { - return [[NaN, NaN], [NaN, NaN]]; - } + extent: function() { + throw Error("not implemented"); }, intersects: function(extent, resolver) { diff --git a/js/id/graph/node.js b/js/id/graph/node.js index 1a71674d5..3a29cd8e6 100644 --- a/js/id/graph/node.js +++ b/js/id/graph/node.js @@ -1,3 +1,7 @@ iD.Node = iD.Entity.extend({ - type: "node" + type: "node", + + extent: function() { + return [this.loc, this.loc]; + } }); diff --git a/js/id/graph/relation.js b/js/id/graph/relation.js index a4a00dc03..8f65254e8 100644 --- a/js/id/graph/relation.js +++ b/js/id/graph/relation.js @@ -1,4 +1,8 @@ iD.Relation = iD.Entity.extend({ type: "relation", - members: [] + members: [], + + extent: function() { + return [[NaN, NaN], [NaN, NaN]]; + } }); diff --git a/js/id/graph/way.js b/js/id/graph/way.js index 9c39ba67e..a62afb8e1 100644 --- a/js/id/graph/way.js +++ b/js/id/graph/way.js @@ -2,6 +2,20 @@ iD.Way = iD.Entity.extend({ type: "way", nodes: [], + extent: function(resolver) { + return resolver.transient(this, 'extent', function() { + var extent = [[-Infinity, Infinity], [Infinity, -Infinity]]; + for (var i = 0, l = this.nodes.length; i < l; i++) { + var node = resolver.entity(this.nodes[i]); + if (node.loc[0] > extent[0][0]) extent[0][0] = node.loc[0]; + if (node.loc[0] < extent[1][0]) extent[1][0] = node.loc[0]; + if (node.loc[1] < extent[0][1]) extent[0][1] = node.loc[1]; + if (node.loc[1] > extent[1][1]) extent[1][1] = node.loc[1]; + } + return extent; + }); + }, + isOneWay: function() { return this.tags.oneway === 'yes'; }, diff --git a/test/spec/graph/node.js b/test/spec/graph/node.js index b364dc0e6..c7ce0c406 100644 --- a/test/spec/graph/node.js +++ b/test/spec/graph/node.js @@ -21,6 +21,12 @@ describe('iD.Node', function () { expect(iD.Node({tags: {foo: 'bar'}}).tags).to.eql({foo: 'bar'}); }); + describe("#extent", function() { + it("returns a point extent", function() { + expect(iD.Node({loc: [5, 10]}).extent()).to.eql([[5, 10], [5, 10]]); + }); + }); + describe("#intersects", function () { it("returns true for a node within the given extent", function () { expect(iD.Node({loc: [0, 0]}).intersects([[-180, 90], [180, -90]])).to.equal(true); diff --git a/test/spec/graph/relation.js b/test/spec/graph/relation.js index 00e9addd8..dfeeb4e5c 100644 --- a/test/spec/graph/relation.js +++ b/test/spec/graph/relation.js @@ -34,4 +34,8 @@ describe('iD.Relation', function () { it("sets tags as specified", function () { expect(iD.Relation({tags: {foo: 'bar'}}).tags).to.eql({foo: 'bar'}); }); + + describe("#extent", function () { + it("returns the minimal extent containing the extents of all members"); + }); }); diff --git a/test/spec/graph/way.js b/test/spec/graph/way.js index bb96a322d..4f64fac67 100644 --- a/test/spec/graph/way.js +++ b/test/spec/graph/way.js @@ -35,6 +35,16 @@ describe('iD.Way', function() { expect(iD.Way({tags: {foo: 'bar'}}).tags).to.eql({foo: 'bar'}); }); + describe("#extent", function () { + it("returns the minimal extent containing all member nodes", function () { + var node1 = iD.Node({loc: [0, 0]}), + node2 = iD.Node({loc: [5, 10]}), + way = iD.Way({nodes: [node1.id, node2.id]}), + graph = iD.Graph([node1, node2, way]); + expect(way.extent(graph)).to.eql([[5, 0], [0, 10]]); + }); + }); + describe("#intersects", function () { it("returns true for a way with a node within the given extent", function () { var node = iD.Node({loc: [0, 0]}),