From c50a290cd19fa54eefb4516d621b2eb65ec302b4 Mon Sep 17 00:00:00 2001 From: Bryan Housel Date: Fri, 3 Jul 2015 11:14:54 -0400 Subject: [PATCH] Add iD.geo.Extent#rectangle --- js/id/core/tree.js | 13 ++----------- js/id/geo/extent.js | 6 +++++- js/id/services/nominatim.js | 2 +- test/spec/geo/extent.js | 15 ++++++++++++++- 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/js/id/core/tree.js b/js/id/core/tree.js index 6bc40c33d..05bbc2230 100644 --- a/js/id/core/tree.js +++ b/js/id/core/tree.js @@ -2,17 +2,8 @@ iD.Tree = function(head) { var rtree = rbush(), rectangles = {}; - function extentRectangle(extent) { - return [ - extent[0][0], - extent[0][1], - extent[1][0], - extent[1][1] - ]; - } - function entityRectangle(entity) { - var rect = extentRectangle(entity.extent(head)); + var rect = entity.extent(head).rectangle(); rect.id = entity.id; rectangles[entity.id] = rect; return rect; @@ -90,7 +81,7 @@ iD.Tree = function(head) { rtree.load(_.map(insertions, entityRectangle)); } - return rtree.search(extentRectangle(extent)).map(function(rect) { + return rtree.search(extent.rectangle()).map(function(rect) { return head.entity(rect.id); }); }; diff --git a/js/id/geo/extent.js b/js/id/geo/extent.js index 912bcf08a..685c22282 100644 --- a/js/id/geo/extent.js +++ b/js/id/geo/extent.js @@ -45,6 +45,10 @@ _.extend(iD.geo.Extent.prototype, { (this[0][1] + this[1][1]) / 2]; }, + rectangle: function() { + return [this[0][0], this[0][1], this[1][0], this[1][1]]; + }, + polygon: function() { return [ [this[0][0], this[0][1]], @@ -100,7 +104,7 @@ _.extend(iD.geo.Extent.prototype, { }, toParam: function() { - return [this[0][0], this[0][1], this[1][0], this[1][1]].join(','); + return this.rectangle().join(','); } }); diff --git a/js/id/services/nominatim.js b/js/id/services/nominatim.js index 8431ce6aa..e57921331 100644 --- a/js/id/services/nominatim.js +++ b/js/id/services/nominatim.js @@ -24,7 +24,7 @@ iD.services.nominatim = function() { var extent = iD.geo.Extent(location).padByMeters(1000); - cache.insert([extent[0][0], extent[0][1], extent[1][0], extent[1][1], result.address.country_code]); + cache.insert(extent.rectangle().concat(result.address.country_code)); callback(null, result.address.country_code); }); diff --git a/test/spec/geo/extent.js b/test/spec/geo/extent.js index cd2e84cb0..b58dbefec 100644 --- a/test/spec/geo/extent.js +++ b/test/spec/geo/extent.js @@ -57,7 +57,20 @@ describe("iD.geo.Extent", function () { describe("#center", function () { it("returns the center point", function () { - expect(iD.geo.Extent([0, 0], [5, 10]).center()).to.eql([2.5, 5]); + expect(iD.geo.Extent([0, 0], [5, 10]).center()).to.eql([2.5, 5]); + }); + }); + + describe("#rectangle", function () { + it("returns the extent as a rectangle", function () { + expect(iD.geo.Extent([0, 0], [5, 10]).rectangle()).to.eql([0, 0, 5, 10]); + }); + }); + + describe("#polygon", function () { + it("returns the extent as a polygon", function () { + expect(iD.geo.Extent([0, 0], [5, 10]).polygon()) + .to.eql([[0, 0], [0, 10], [5, 10], [5, 0], [0, 0]]); }); });