Skip to content

Commit

Permalink
use grid index instead of rtree for querying features
Browse files Browse the repository at this point in the history
Why?
Both indexes have comparable querying performance and Grid is
transferrable. Insertion is faster with Grid than rbush but that's not
the main benefit.

Point queries are really fast for both. Each is sometimes faster than
the other. Therre is no clear winner.

Large, full-screen bbox queries are 10% slower with Grid than rbush but
difference is not significant (0.2% of FeatureTree.query).

comparisons were done with mapbox-streets-v6
  • Loading branch information
ansis committed Mar 2, 2016
1 parent c00a7f3 commit 6e3f8c7
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 21 deletions.
21 changes: 1 addition & 20 deletions js/data/feature_tree.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

var rbush = require('rbush');
var Point = require('point-geometry');
var util = require('../util/util');
var loadGeometry = require('./load_geometry');
Expand All @@ -25,7 +24,6 @@ function FeatureTree(coord, overscaling, collisionTile, vtLayers) {
this.x = coord.x;
this.y = coord.y;
this.z = coord.z - Math.log(overscaling) / Math.LN2;
this.rtree = rbush(9);
this.toBeInserted = [];
this.grid = new Grid(16, EXTENT, 0);
this.featureIndexArray = new FeatureIndexArray();
Expand All @@ -40,19 +38,8 @@ FeatureTree.prototype.insert = function(bbox, extent, featureIndex, sourceLayerI
bbox[1] *= scale;
bbox[2] *= scale;
bbox[3] *= scale;
bbox.key = this.featureIndexArray.length;
this.grid.insert(this.featureIndexArray.length, bbox[0], bbox[1], bbox[2], bbox[3]);
this.featureIndexArray.emplaceBack(featureIndex, sourceLayerIndex, bucketIndex);
this.toBeInserted.push(bbox);
};

// bulk insert into tree
FeatureTree.prototype._load = function() {
this.rtree.load(this.toBeInserted);
for (var i = 0; i < this.toBeInserted.length; i++) {
var bbox = this.toBeInserted[i];
this.grid.insert(i, bbox[0], bbox[1], bbox[2], bbox[3]);
}
this.toBeInserted = [];
};

FeatureTree.prototype.setCollisionTile = function(collisionTile) {
Expand All @@ -66,7 +53,6 @@ function translateDistance(translate) {
// Finds features in this tile at a particular position.
FeatureTree.prototype.query = function(args, styleLayersByID) {
if (!this.vtLayers) return [];
if (this.toBeInserted.length) this._load();

var params = args.params || {},
pixelsToTileUnits = EXTENT / args.tileSize / args.scale,
Expand Down Expand Up @@ -108,15 +94,10 @@ FeatureTree.prototype.query = function(args, styleLayersByID) {
maxY = Math.max(maxY, p.y);
}

var bounds = [minX - additionalRadius, minY - additionalRadius, maxX + additionalRadius, maxY + additionalRadius];
var treeMatching = this.rtree.search(bounds);

var matching = this.grid.query(minX - additionalRadius, minY - additionalRadius, maxX + additionalRadius, maxY + additionalRadius);
var match = this.featureIndexArray.at(0);
filterMatching.call(this, matching, match);

if (matching.length !== treeMatching.length) throw Error("asdf");

var matchingSymbols = this.collisionTile.queryRenderedSymbols(minX, minY, maxX, maxY, args.scale);
var match2 = this.collisionTile.collisionBoxArray.at(0);
filterMatching.call(this, matchingSymbols, match2);
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"pbf": "^1.3.2",
"pngjs": "^2.2.0",
"point-geometry": "^0.0.0",
"rbush": "^1.4.0",
"request": "^2.39.0",
"resolve-url": "^0.2.1",
"supercluster": "^2.0.0",
Expand Down

0 comments on commit 6e3f8c7

Please sign in to comment.