Additional index dimension for RTree; use RTree again

The RTree can easily maintain an additional index dimension,
by passing a type with each added item. Now instead of
maintaining an RTree for each geometry type, we have a single
RTree with a type filter. With this change, using the RTree
finally speeds up rendering as expected.
This commit is contained in:
ahocevar
2013-03-07 14:36:09 +01:00
parent f961930823
commit f0039ee460
3 changed files with 34 additions and 27 deletions

View File

@@ -29,10 +29,10 @@ ol.layer.FeatureCache = function() {
this.geometryTypeIndex_;
/**
* @type {Object.<ol.geom.GeometryType, ol.structs.RTree>}
* @type {ol.structs.RTree}
* @private
*/
this.boundsByGeometryType_;
this.rTree_;
this.clear();
@@ -44,17 +44,12 @@ ol.layer.FeatureCache = function() {
*/
ol.layer.FeatureCache.prototype.clear = function() {
this.idLookup_ = {};
var geometryTypeIndex = {},
boundsByGeometryType = {},
geometryType;
var geometryTypeIndex = {};
for (var key in ol.geom.GeometryType) {
geometryType = ol.geom.GeometryType[key];
geometryTypeIndex[geometryType] = {};
boundsByGeometryType[geometryType] = new ol.structs.RTree();
geometryTypeIndex[ol.geom.GeometryType[key]] = {};
}
this.geometryTypeIndex_ = geometryTypeIndex;
this.boundsByGeometryType_ = boundsByGeometryType;
this.rTree_ = new ol.structs.RTree();
};
@@ -72,8 +67,8 @@ ol.layer.FeatureCache.prototype.add = function(feature) {
if (!goog.isNull(geometry)) {
var geometryType = geometry.getType();
this.geometryTypeIndex_[geometryType][id] = feature;
this.boundsByGeometryType_[geometryType].put(geometry.getBounds(),
feature);
this.rTree_.put(geometry.getBounds(),
feature, geometryType);
}
};
@@ -91,12 +86,7 @@ ol.layer.FeatureCache.prototype.getFeaturesObject_ = function(opt_filter) {
if (opt_filter instanceof ol.filter.Geometry) {
features = this.geometryTypeIndex_[opt_filter.getType()];
} else if (opt_filter instanceof ol.filter.Extent) {
var boundsByGeometryType = this.boundsByGeometryType_,
extent = opt_filter.getExtent();
features = {};
for (i in boundsByGeometryType) {
goog.object.extend(features, boundsByGeometryType[i].find(extent));
}
features = this.rTree_.find(opt_filter.getExtent());
} else if (opt_filter instanceof ol.filter.Logical &&
opt_filter.operator === ol.filter.LogicalOperator.AND) {
var filters = opt_filter.getFilters();
@@ -111,8 +101,8 @@ ol.layer.FeatureCache.prototype.getFeaturesObject_ = function(opt_filter) {
}
}
if (extentFilter && geometryFilter) {
features = this.boundsByGeometryType_[geometryFilter.getType()]
.find(extentFilter.getExtent());
features = this.rTree_.find(
extentFilter.getExtent(), geometryFilter.getType());
}
}
}

View File

@@ -327,7 +327,8 @@ ol.renderer.canvas.VectorLayer.prototype.renderFrame =
for (i = 0; i < numFilters; ++i) {
geomFilter = filters[i];
type = geomFilter.getType();
features = layer.getFeatures(geomFilter);
features = layer.getFeatures(new ol.filter.Logical(
[geomFilter, extentFilter], ol.filter.LogicalOperator.AND));
if (features.length) {
groups = layer.groupFeaturesBySymbolizerLiteral(features);
numGroups = groups.length;

View File

@@ -39,6 +39,11 @@ ol.RTreeNode_ = function(minX, minY, maxX, maxY, parent, level) {
*/
this.level = level;
/**
* @type {Object.<string, boolean>}
*/
this.types = {};
/**
* @type {Array.<ol.RTreeNode_>}
*/
@@ -52,9 +57,11 @@ goog.inherits(ol.RTreeNode_, ol.Rectangle);
* Find all objects intersected by a rectangle.
* @param {ol.Rectangle} bounds Bounding box.
* @param {Object.<string, Object>} results Target object for results.
* @param {string=} opt_type Type for another indexing dimension.
*/
ol.RTreeNode_.prototype.find = function(bounds, results) {
if (this.intersects(bounds)) {
ol.RTreeNode_.prototype.find = function(bounds, results, opt_type) {
if (this.intersects(bounds) &&
(!goog.isDef(opt_type) || this.types[opt_type] === true)) {
var numChildren = this.children.length;
if (numChildren === 0) {
if (goog.isDef(this.object)) {
@@ -62,7 +69,7 @@ ol.RTreeNode_.prototype.find = function(bounds, results) {
}
} else {
for (var i = 0; i < numChildren; ++i) {
this.children[i].find(bounds, results);
this.children[i].find(bounds, results, opt_type);
}
}
}
@@ -123,9 +130,11 @@ ol.RTreeNode_.prototype.divide = function() {
if (i % half === 0) {
node = new ol.RTreeNode_(child.minX, child.minY, child.maxX, child.maxY,
this, this.level + 1);
goog.object.extend(this.types, node.types);
this.children.push(node);
}
child.parent = /** @type {ol.RTreeNode_} */ node;
goog.object.extend(node.types, child.types);
node.children.push(child);
node.extend(child);
}
@@ -151,11 +160,12 @@ ol.structs.RTree = function() {
/**
* @param {ol.Rectangle} bounds Bounding box.
* @param {string=} opt_type Type for another indexing dimension.
* @return {Object.<string, Object>} Results for the passed bounding box.
*/
ol.structs.RTree.prototype.find = function(bounds) {
ol.structs.RTree.prototype.find = function(bounds, opt_type) {
var results = /** @type {Object.<string, Object>} */ {};
this.root_.find(bounds, results);
this.root_.find(bounds, results, opt_type);
return results;
};
@@ -163,8 +173,9 @@ ol.structs.RTree.prototype.find = function(bounds) {
/**
* @param {ol.Rectangle} bounds Bounding box.
* @param {Object} object Object to store with the passed bounds.
* @param {string=} opt_type Type for another indexing dimension.
*/
ol.structs.RTree.prototype.put = function(bounds, object) {
ol.structs.RTree.prototype.put = function(bounds, object, opt_type) {
var found = this.root_.get(bounds);
if (found) {
var node = new ol.RTreeNode_(
@@ -176,6 +187,11 @@ ol.structs.RTree.prototype.put = function(bounds, object) {
found.children.push(node);
found.update(bounds);
if (goog.isDef(opt_type)) {
node.types[opt_type] = true;
found.types[opt_type] = true;
}
if (found.children.length >= ol.structs.RTree.MAX_OBJECTS &&
found.level < ol.structs.RTree.MAX_SUB_DIVISIONS) {
found.divide();