diff --git a/package.json b/package.json index 8e673837c3..d19dd2dfb2 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "nomnom": "1.8.1", "pbf": "2.0.1", "pixelworks": "1.1.0", - "rbush": "1.4.3", + "rbush": "2.0.1", "temp": "0.8.3", "vector-tile": "1.2.1", "walk": "2.3.9" @@ -86,7 +86,10 @@ } }, "ext": [ - "rbush", + { + "module": "rbush", + "browserify": true + }, { "module": "pbf", "browserify": true diff --git a/src/ol/structs/rbush.js b/src/ol/structs/rbush.js index 2d77e5f3de..df435ad6ef 100644 --- a/src/ol/structs/rbush.js +++ b/src/ol/structs/rbush.js @@ -26,7 +26,7 @@ ol.structs.RBush = function(opt_maxEntries) { * A mapping between the objects added to this rbush wrapper * and the objects that are actually added to the internal rbush. * @private - * @type {Object.} + * @type {Object.} */ this.items_ = {}; @@ -49,13 +49,15 @@ ol.structs.RBush.prototype.insert = function(extent, value) { if (goog.DEBUG && this.readers_) { throw new Error('Can not insert value while reading'); } - var item = [ - extent[0], - extent[1], - extent[2], - extent[3], - value - ]; + /** @type {ol.RBushEntry} */ + var item = { + minX: extent[0], + minY: extent[1], + maxX: extent[2], + maxY: extent[3], + value: value + }; + this.rbush_.insert(item); // remember the object that was added to the internal rbush goog.asserts.assert(!(goog.getUid(value) in this.items_), @@ -82,13 +84,14 @@ ol.structs.RBush.prototype.load = function(extents, values) { var extent = extents[i]; var value = values[i]; - var item = [ - extent[0], - extent[1], - extent[2], - extent[3], - value - ]; + /** @type {ol.RBushEntry} */ + var item = { + minX: extent[0], + minY: extent[1], + maxX: extent[2], + maxY: extent[3], + value: value + }; items[i] = item; goog.asserts.assert(!(goog.getUid(value) in this.items_), 'uid (%s) of value (%s) already exists', goog.getUid(value), value); @@ -130,7 +133,8 @@ ol.structs.RBush.prototype.update = function(extent, value) { 'uid (%s) of value (%s) does not exist', uid, value); var item = this.items_[uid]; - if (!ol.extent.equals(item.slice(0, 4), extent)) { + var bbox = [item.minX, item.minY, item.maxX, item.maxY]; + if (!ol.extent.equals(bbox, extent)) { if (goog.DEBUG && this.readers_) { throw new Error('Can not update extent while reading'); } @@ -147,7 +151,7 @@ ol.structs.RBush.prototype.update = function(extent, value) { ol.structs.RBush.prototype.getAll = function() { var items = this.rbush_.all(); return items.map(function(item) { - return item[4]; + return item.value; }); }; @@ -158,9 +162,16 @@ ol.structs.RBush.prototype.getAll = function() { * @return {Array.} All in extent. */ ol.structs.RBush.prototype.getInExtent = function(extent) { - var items = this.rbush_.search(extent); + /** @type {ol.RBushEntry} */ + var bbox = { + minX: extent[0], + minY: extent[1], + maxX: extent[2], + maxY: extent[3] + }; + var items = this.rbush_.search(bbox); return items.map(function(item) { - return item[4]; + return item.value; }); }; @@ -253,5 +264,6 @@ ol.structs.RBush.prototype.clear = function() { */ ol.structs.RBush.prototype.getExtent = function(opt_extent) { // FIXME add getExtent() to rbush - return this.rbush_.data.bbox; + var data = this.rbush_.data; + return [data.minX, data.minY, data.maxX, data.maxY]; }; diff --git a/src/ol/typedefs.js b/src/ol/typedefs.js index ffe8677a77..c9a59f1eab 100644 --- a/src/ol/typedefs.js +++ b/src/ol/typedefs.js @@ -686,3 +686,10 @@ ol.XmlParser; * @typedef {function(Node, *, Array.<*>)} */ ol.XmlSerializer; + + +/** + * @typedef {{minX: number, minY: number, maxX: number, maxY: number, + * value: (Object|undefined)}} + */ +ol.RBushEntry; diff --git a/test/spec/ol/structs/rbush.test.js b/test/spec/ol/structs/rbush.test.js index 44117fb360..d3d6c029f8 100644 --- a/test/spec/ol/structs/rbush.test.js +++ b/test/spec/ol/structs/rbush.test.js @@ -362,6 +362,16 @@ describe('ol.structs.RBush', function() { }); + describe('#getExtent', function() { + + it('gets the extent', function() { + var obj = {}; + rBush.insert([0, 0, 1, 1], obj); + expect(rBush.getExtent()).to.eql([0, 0, 1, 1]); + }); + + }); + }); goog.require('ol.structs.RBush');