Merge pull request #1390 from twpayne/vector-api-misc-fixes

[vector-api] Miscellaneous vector fixes
This commit is contained in:
Tom Payne
2013-12-16 04:09:21 -08:00
5 changed files with 54 additions and 16 deletions

View File

@@ -1,2 +1 @@
@exportProperty ol.format.Format.prototype.readProjection
@exportProperty ol.format.Format.prototype.readStyleFunction

View File

@@ -67,16 +67,6 @@ ol.format.Format.prototype.readGeometry = goog.abstractMethod;
ol.format.Format.prototype.readProjection = goog.abstractMethod;
/**
* @param {Document|Node|Object|string} source Source.
* @return {function(ol.Feature, number): Array.<ol.style.Style>} Style
* function.
*/
ol.format.Format.prototype.readStyleFunction = function(source) {
return goog.functions.NULL;
};
/**
* @param {ol.Feature} feature Feature.
* @return {Node|Object|string} Result.

View File

@@ -173,6 +173,14 @@ ol.geom.GeometryCollection.prototype.getType = function() {
};
/**
* @return {boolean} Is empty.
*/
ol.geom.GeometryCollection.prototype.isEmpty = function() {
return goog.array.isEmpty(this.geometries_);
};
/**
* @param {Array.<ol.geom.Geometry>} geometries Geometries.
*/

View File

@@ -6,10 +6,12 @@ goog.provide('ol.source.Vector');
goog.provide('ol.source.VectorEvent');
goog.provide('ol.source.VectorEventType');
goog.require('goog.array');
goog.require('goog.asserts');
goog.require('goog.events');
goog.require('goog.events.Event');
goog.require('goog.events.EventType');
goog.require('goog.object');
goog.require('ol.source.Source');
goog.require('ol.structs.RBush');
@@ -48,6 +50,12 @@ ol.source.Vector = function(opt_options) {
*/
this.rBush_ = new ol.structs.RBush();
/**
* @private
* @type {Object.<string, ol.Feature>}
*/
this.nullGeometryFeatures_ = {};
/**
* @private
* @type {Object.<string, goog.events.Key>}
@@ -74,8 +82,13 @@ ol.source.Vector.prototype.addFeature = function(feature) {
goog.asserts.assert(!(featureKey in this.featureChangeKeys_));
this.featureChangeKeys_[featureKey] = goog.events.listen(feature,
goog.events.EventType.CHANGE, this.handleFeatureChange_, false, this);
var extent = feature.getGeometry().getExtent();
this.rBush_.insert(extent, feature);
var geometry = feature.getGeometry();
if (goog.isNull(geometry)) {
this.nullGeometryFeatures_[goog.getUid(feature).toString()] = feature;
} else {
var extent = geometry.getExtent();
this.rBush_.insert(extent, feature);
}
this.dispatchEvent(
new ol.source.VectorEvent(ol.source.VectorEventType.ADDFEATURE, feature));
this.dispatchChangeEvent();
@@ -140,7 +153,12 @@ ol.source.Vector.prototype.forEachFeatureInExtent =
* @return {Array.<ol.Feature>} Features.
*/
ol.source.Vector.prototype.getAllFeatures = function() {
return this.rBush_.getAll();
var features = this.rBush_.getAll();
if (!goog.object.isEmpty(this.nullGeometryFeatures_)) {
goog.array.extend(
features, goog.object.getValues(this.nullGeometryFeatures_));
}
return features;
};
@@ -212,6 +230,14 @@ ol.source.Vector.prototype.getClosestFeatureToCoordinate =
};
/**
* @return {ol.Extent} Extent.
*/
ol.source.Vector.prototype.getExtent = function() {
return this.rBush_.getExtent();
};
/**
* @param {goog.events.Event} event Event.
* @private
@@ -227,7 +253,8 @@ ol.source.Vector.prototype.handleFeatureChange_ = function(event) {
* @return {boolean} Is empty.
*/
ol.source.Vector.prototype.isEmpty = function() {
return this.rBush_.isEmpty();
return this.rBush_.isEmpty() &&
goog.object.isEmpty(this.nullGeometryFeatures_);
};
@@ -235,7 +262,12 @@ ol.source.Vector.prototype.isEmpty = function() {
* @param {ol.Feature} feature Feature.
*/
ol.source.Vector.prototype.removeFeature = function(feature) {
this.rBush_.remove(feature);
var featureKey = goog.getUid(feature).toString();
if (featureKey in this.nullGeometryFeatures_) {
delete this.nullGeometryFeatures_[featureKey];
} else {
this.rBush_.remove(feature);
}
this.removeFeatureInternal_(feature);
this.dispatchChangeEvent();
};

View File

@@ -522,6 +522,15 @@ ol.structs.RBush.prototype.getAllInExtent = function(extent) {
};
/**
* @param {ol.Extent=} opt_extent Extent.
* @return {ol.Extent} Extent.
*/
ol.structs.RBush.prototype.getExtent = function(opt_extent) {
return ol.extent.returnOrUpdate(this.root_.extent, opt_extent);
};
/**
* @param {T} value Value.
* @private