Merge pull request #2870 from tsauerwein/rbush-bulk-insert
Use bulk-insertion for addFeatures
This commit is contained in:
@@ -128,6 +128,28 @@ ol.source.Vector.prototype.addFeature = function(feature) {
|
|||||||
*/
|
*/
|
||||||
ol.source.Vector.prototype.addFeatureInternal = function(feature) {
|
ol.source.Vector.prototype.addFeatureInternal = function(feature) {
|
||||||
var featureKey = goog.getUid(feature).toString();
|
var featureKey = goog.getUid(feature).toString();
|
||||||
|
this.setupChangeEvents_(featureKey, feature);
|
||||||
|
|
||||||
|
var geometry = feature.getGeometry();
|
||||||
|
if (goog.isDefAndNotNull(geometry)) {
|
||||||
|
var extent = geometry.getExtent();
|
||||||
|
this.rBush_.insert(extent, feature);
|
||||||
|
} else {
|
||||||
|
this.nullGeometryFeatures_[featureKey] = feature;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.addToIndex_(featureKey, feature);
|
||||||
|
this.dispatchEvent(
|
||||||
|
new ol.source.VectorEvent(ol.source.VectorEventType.ADDFEATURE, feature));
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} featureKey
|
||||||
|
* @param {ol.Feature} feature
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
ol.source.Vector.prototype.setupChangeEvents_ = function(featureKey, feature) {
|
||||||
goog.asserts.assert(!(featureKey in this.featureChangeKeys_));
|
goog.asserts.assert(!(featureKey in this.featureChangeKeys_));
|
||||||
this.featureChangeKeys_[featureKey] = [
|
this.featureChangeKeys_[featureKey] = [
|
||||||
goog.events.listen(feature,
|
goog.events.listen(feature,
|
||||||
@@ -137,13 +159,15 @@ ol.source.Vector.prototype.addFeatureInternal = function(feature) {
|
|||||||
ol.ObjectEventType.PROPERTYCHANGE,
|
ol.ObjectEventType.PROPERTYCHANGE,
|
||||||
this.handleFeatureChange_, false, this)
|
this.handleFeatureChange_, false, this)
|
||||||
];
|
];
|
||||||
var geometry = feature.getGeometry();
|
};
|
||||||
if (goog.isDefAndNotNull(geometry)) {
|
|
||||||
var extent = geometry.getExtent();
|
|
||||||
this.rBush_.insert(extent, feature);
|
/**
|
||||||
} else {
|
* @param {string} featureKey
|
||||||
this.nullGeometryFeatures_[featureKey] = feature;
|
* @param {ol.Feature} feature
|
||||||
}
|
* @private
|
||||||
|
*/
|
||||||
|
ol.source.Vector.prototype.addToIndex_ = function(featureKey, feature) {
|
||||||
var id = feature.getId();
|
var id = feature.getId();
|
||||||
if (goog.isDef(id)) {
|
if (goog.isDef(id)) {
|
||||||
this.idIndex_[id.toString()] = feature;
|
this.idIndex_[id.toString()] = feature;
|
||||||
@@ -152,8 +176,6 @@ ol.source.Vector.prototype.addFeatureInternal = function(feature) {
|
|||||||
'Feature already added to the source');
|
'Feature already added to the source');
|
||||||
this.undefIdIndex_[featureKey] = feature;
|
this.undefIdIndex_[featureKey] = feature;
|
||||||
}
|
}
|
||||||
this.dispatchEvent(
|
|
||||||
new ol.source.VectorEvent(ol.source.VectorEventType.ADDFEATURE, feature));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -174,10 +196,31 @@ ol.source.Vector.prototype.addFeatures = function(features) {
|
|||||||
* @protected
|
* @protected
|
||||||
*/
|
*/
|
||||||
ol.source.Vector.prototype.addFeaturesInternal = function(features) {
|
ol.source.Vector.prototype.addFeaturesInternal = function(features) {
|
||||||
// FIXME use R-Bush bulk load when available
|
var featureKey, i, length, feature;
|
||||||
var i, ii;
|
var extents = [];
|
||||||
for (i = 0, ii = features.length; i < ii; ++i) {
|
var validFeatures = [];
|
||||||
this.addFeatureInternal(features[i]);
|
for (i = 0, length = features.length; i < length; i++) {
|
||||||
|
feature = features[i];
|
||||||
|
featureKey = goog.getUid(feature).toString();
|
||||||
|
this.setupChangeEvents_(featureKey, feature);
|
||||||
|
|
||||||
|
var geometry = feature.getGeometry();
|
||||||
|
if (goog.isDefAndNotNull(geometry)) {
|
||||||
|
var extent = geometry.getExtent();
|
||||||
|
extents.push(extent);
|
||||||
|
validFeatures.push(feature);
|
||||||
|
} else {
|
||||||
|
this.nullGeometryFeatures_[featureKey] = feature;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.rBush_.load(extents, validFeatures);
|
||||||
|
|
||||||
|
for (i = 0, length = features.length; i < length; i++) {
|
||||||
|
feature = features[i];
|
||||||
|
featureKey = goog.getUid(feature).toString();
|
||||||
|
this.addToIndex_(featureKey, feature);
|
||||||
|
this.dispatchEvent(new ol.source.VectorEvent(
|
||||||
|
ol.source.VectorEventType.ADDFEATURE, feature));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -65,6 +65,36 @@ ol.structs.RBush.prototype.insert = function(extent, value) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bulk-insert values into the RBush.
|
||||||
|
* @param {Array.<ol.Extent>} extents Extents.
|
||||||
|
* @param {Array.<T>} values Values.
|
||||||
|
*/
|
||||||
|
ol.structs.RBush.prototype.load = function(extents, values) {
|
||||||
|
if (goog.DEBUG && this.readers_) {
|
||||||
|
throw new Error('Can not insert values while reading');
|
||||||
|
}
|
||||||
|
goog.asserts.assert(extents.length === values.length);
|
||||||
|
|
||||||
|
var items = [];
|
||||||
|
for (var i = 0, l = values.length; i < l; i++) {
|
||||||
|
var extent = extents[i];
|
||||||
|
var value = values[i];
|
||||||
|
|
||||||
|
var item = [
|
||||||
|
extent[0],
|
||||||
|
extent[1],
|
||||||
|
extent[2],
|
||||||
|
extent[3],
|
||||||
|
value
|
||||||
|
];
|
||||||
|
items.push(item);
|
||||||
|
goog.object.add(this.items_, goog.getUid(value).toString(), item);
|
||||||
|
}
|
||||||
|
this.rbush_.load(items);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove a value from the RBush.
|
* Remove a value from the RBush.
|
||||||
* @param {T} value Value.
|
* @param {T} value Value.
|
||||||
|
|||||||
Reference in New Issue
Block a user