Permalink
Browse files

Moving the feature management to the layer

The source will be responsible for fetching vector data.
  • Loading branch information...
1 parent a968a5c commit 12bee3178e94841a6aadf2ff0ebebba5b48ec3ce @tschaub tschaub committed Mar 5, 2013
View
@@ -15,11 +15,46 @@ goog.require('ol.style.Rule');
goog.require('ol.style.Style');
-var source = new ol.source.Vector({
- projection: ol.projection.getFromCode('EPSG:3857')
+var style = new ol.style.Style({rules: [
+ new ol.style.Rule({
+ filter: new ol.filter.Filter(function(feature) {
+ return feature.get('where') == 'outer';
+ }),
+ symbolizers: [
+ new ol.style.Line({
+ strokeStyle: new ol.Expression('color'),
+ strokeWidth: 4,
+ opacity: 1
+ })
+ ]
+ }),
+ new ol.style.Rule({
+ filter: new ol.filter.Filter(function(feature) {
+ return feature.get('where') == 'inner';
+ }),
+ symbolizers: [
+ new ol.style.Line({
+ strokeStyle: '#013',
+ strokeWidth: 4,
+ opacity: 1
+ }),
+ new ol.style.Line({
+ strokeStyle: new ol.Expression('color'),
+ strokeWidth: 2,
+ opacity: 1
+ })
+ ]
+ })
+]});
+
+var vector = new ol.layer.Vector({
+ style: style,
+ source: new ol.source.Vector({
+ projection: ol.projection.getFromCode('EPSG:3857')
+ })
});
-source.addFeatures([
+vector.addFeatures([
new ol.Feature({
g: new ol.geom.LineString([[-10000000, -10000000], [10000000, 10000000]]),
'color': '#BADA55',
@@ -52,44 +87,6 @@ source.addFeatures([
})
]);
-var style = new ol.style.Style({
- rules: [
- new ol.style.Rule({
- filter: new ol.filter.Filter(function(feature) {
- return feature.get('where') == 'outer';
- }),
- symbolizers: [
- new ol.style.Line({
- strokeStyle: new ol.Expression('color'),
- strokeWidth: 4,
- opacity: 1
- })
- ]
- }),
- new ol.style.Rule({
- filter: new ol.filter.Filter(function(feature) {
- return feature.get('where') == 'inner';
- }),
- symbolizers: [
- new ol.style.Line({
- strokeStyle: '#013',
- strokeWidth: 4,
- opacity: 1
- }),
- new ol.style.Line({
- strokeStyle: new ol.Expression('color'),
- strokeWidth: 2,
- opacity: 1
- })
- ]
- })
- ]
-});
-
-var vector = new ol.layer.Vector({
- source: source,
- style: style
-});
var map = new ol.Map({
layers: new ol.Collection([vector]),
View
@@ -17,11 +17,13 @@ var raster = new ol.layer.TileLayer({
source: new ol.source.MapQuestOpenAerial()
});
-var source = new ol.source.Vector({
- projection: ol.projection.getFromCode('EPSG:3857')
+var vector = new ol.layer.Vector({
+ source: new ol.source.Vector({
+ projection: ol.projection.getFromCode('EPSG:3857')
+ })
});
-source.addFeatures([
+vector.addFeatures([
new ol.Feature({
g: new ol.geom.LineString([[-10000000, -10000000], [10000000, 10000000]])
}),
@@ -31,9 +33,6 @@ source.addFeatures([
new ol.Feature({g: new ol.geom.Point([-10000000, 5000000])})
]);
-var vector = new ol.layer.Vector({
- source: source
-});
var map = new ol.Map({
layers: new ol.Collection([raster, vector]),
View
@@ -3,12 +3,172 @@ goog.provide('ol.layer.Vector');
goog.require('ol.Feature');
goog.require('ol.layer.Layer');
goog.require('ol.source.Vector');
+goog.require('ol.structs.RTree');
goog.require('ol.style.Style');
/**
* @constructor
+ */
+ol.layer.FeatureCache = function() {
+
+ /**
+ * @type {Object.<string, ol.Feature>}
+ * @private
+ */
+ this.idLookup_;
+
+ /**
+ * @type {Object.<string, ol.Feature>}
+ * @private
+ */
+ this.geometryTypeIndex_;
+
+ /**
+ * @type {Object.<ol.geom.GeometryType, ol.structs.RTree>}
+ * @private
+ */
+ this.boundsByGeometryType_;
+
+ this.clear();
+
+};
+
+
+/**
+ * Clear the cache.
+ */
+ol.layer.FeatureCache.prototype.clear = function() {
+ this.idLookup_ = {};
+
+ var geometryTypeIndex = {},
+ boundsByGeometryType = {},
+ geometryType;
+ for (var key in ol.geom.GeometryType) {
+ geometryType = ol.geom.GeometryType[key];
+ geometryTypeIndex[geometryType] = {};
+ boundsByGeometryType[geometryType] = new ol.structs.RTree();
+ }
+ this.geometryTypeIndex_ = geometryTypeIndex;
+ this.boundsByGeometryType_ = boundsByGeometryType;
+};
+
+
+/**
+ * Add a feature to the cache.
+ * @param {ol.Feature} feature Feature to be cached.
+ */
+ol.layer.FeatureCache.prototype.add = function(feature) {
+ var id = goog.getUid(feature).toString(),
+ geometry = feature.getGeometry();
+
+ this.idLookup_[id] = feature;
+
+ // index by geometry type and bounding box
+ if (!goog.isNull(geometry)) {
+ var geometryType = geometry.getType();
+ this.geometryTypeIndex_[geometryType][id] = feature;
+ this.boundsByGeometryType_[geometryType].put(geometry.getBounds(),
+ feature);
+ }
+};
+
+
+/**
+ * @param {ol.filter.Filter=} opt_filter Optional filter.
+ * @return {Object.<string, ol.Feature>} Object of features, keyed by id.
+ * @private
+ */
+ol.layer.FeatureCache.prototype.getFeaturesObject_ = function(opt_filter) {
+ var i, features;
+ if (!goog.isDef(opt_filter)) {
+ features = this.idLookup_;
+ } else {
+ 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));
+ }
+ } else if (opt_filter instanceof ol.filter.Logical &&
+ opt_filter.operator === ol.filter.LogicalOperator.AND) {
+ var filters = opt_filter.getFilters();
+ if (filters.length === 2) {
+ var filter, geometryFilter, extentFilter;
+ for (i = 0; i <= 1; ++i) {
+ filter = filters[i];
+ if (filter instanceof ol.filter.Geometry) {
+ geometryFilter = filter;
+ } else if (filter instanceof ol.filter.Extent) {
+ extentFilter = filter;
+ }
+ }
+ if (extentFilter && geometryFilter) {
+ features = this.boundsByGeometryType_[geometryFilter.getType()]
+ .find(extentFilter.getExtent());
+ }
+ }
+ }
+ if (!goog.isDef(features)) {
+ // TODO: support fast lane for other filter types
+ var candidates = this.idLookup_,
+ feature;
+ features = {};
+ for (i in candidates) {
+ feature = candidates[i];
+ if (opt_filter.applies(feature) === true) {
+ features[i] = feature;
+ }
+ }
+ }
+ }
+ return features;
+};
+
+
+/**
+ * @param {ol.filter.Filter=} opt_filter Optional filter.
+ * @return {Array.<ol.Feature>} Array of features.
+ */
+ol.layer.FeatureCache.prototype.getFeatures = function(opt_filter) {
+ return goog.object.getValues(this.getFeaturesObject_(opt_filter));
+};
+
+
+/**
+ * @param {ol.filter.Geometry} filter Geometry type filter.
+ * @return {Array.<ol.Feature>} Array of features.
+ * @private
+ */
+ol.layer.FeatureCache.prototype.getFeaturesByGeometryType_ = function(filter) {
+ return goog.object.getValues(this.geometryTypeIndex_[filter.getType()]);
+};
+
+
+/**
+ * Get features by ids.
+ * @param {Array.<string>} ids Array of (internal) identifiers.
+ * @return {Array.<ol.Feature>} Array of features.
+ * @private
+ */
+ol.layer.FeatureCache.prototype.getFeaturesByIds_ = function(ids) {
+ var len = ids.length,
+ features = new Array(len),
+ i;
+ for (i = 0; i < len; ++i) {
+ features[i] = this.idLookup_[ids[i]];
+ }
+ return features;
+};
+
+
+
+/**
+ * @constructor
* @extends {ol.layer.Layer}
* @param {ol.layer.LayerOptions} layerOptions Layer options.
*/
@@ -21,11 +181,27 @@ ol.layer.Vector = function(layerOptions) {
*/
this.style_ = goog.isDef(layerOptions.style) ? layerOptions.style : null;
+ /**
+ * @type {ol.layer.FeatureCache}
+ * @private
+ */
+ this.featureCache_ = new ol.layer.FeatureCache();
+
};
goog.inherits(ol.layer.Vector, ol.layer.Layer);
/**
+ * @param {Array.<ol.Feature>} features Array of features.
+ */
+ol.layer.Vector.prototype.addFeatures = function(features) {
+ for (var i = 0, ii = features.length; i < ii; ++i) {
+ this.featureCache_.add(features[i]);
+ }
+};
+
+
+/**
* @return {ol.source.Vector} Source.
*/
ol.layer.Vector.prototype.getVectorSource = function() {
@@ -34,6 +210,15 @@ ol.layer.Vector.prototype.getVectorSource = function() {
/**
+ * @param {ol.filter.Filter=} opt_filter Optional filter.
+ * @return {Array.<ol.Feature>} Array of features.
+ */
+ol.layer.Vector.prototype.getFeatures = function(opt_filter) {
+ return this.featureCache_.getFeatures(opt_filter);
+};
+
+
+/**
* @param {Array.<ol.Feature>} features Features.
* @return {Array.<Array>} symbolizers for features.
*/
@@ -72,3 +257,10 @@ ol.layer.Vector.prototype.groupFeaturesBySymbolizerLiteral =
}
return featuresBySymbolizer;
};
+
+
+goog.require('ol.filter.Extent');
+goog.require('ol.filter.Geometry');
+goog.require('ol.filter.Logical');
+goog.require('ol.filter.LogicalOperator');
+goog.require('ol.geom.GeometryType');
@@ -167,11 +167,10 @@ ol.renderer.canvas.VectorLayer.prototype.renderFrame =
resolution = view2DState.resolution,
extent = frameState.extent,
layer = this.getVectorLayer(),
- source = layer.getVectorSource(),
tileGrid = this.tileGrid_;
if (goog.isNull(tileGrid)) {
- // lazy tile source creation to match the view projection
+ // lazy tile grid creation to match the view projection
tileGrid = ol.tilegrid.createForProjection(
view2DState.projection,
22, // should be no harm in going big here - ideally, it would be ∞
@@ -282,7 +281,7 @@ ol.renderer.canvas.VectorLayer.prototype.renderFrame =
for (i = 0; i < numFilters; ++i) {
geomFilter = filters[i];
type = geomFilter.getType();
- features = source.getFeatures(new ol.filter.Logical(
+ features = layer.getFeatures(new ol.filter.Logical(
[geomFilter, extentFilter], ol.filter.LogicalOperator.AND));
if (features.length) {
groups = layer.groupFeaturesBySymbolizerLiteral(features);
Oops, something went wrong.

0 comments on commit 12bee31

Please sign in to comment.