Merge pull request #3010 from ahocevar/style-geometry

Allow styles to override feature geometries
This commit is contained in:
Andreas Hocevar
2014-12-18 10:45:50 +01:00
8 changed files with 384 additions and 4 deletions

View File

@@ -477,7 +477,7 @@ ol.render.canvas.Immediate.prototype.drawCircleGeometry =
* @api
*/
ol.render.canvas.Immediate.prototype.drawFeature = function(feature, style) {
var geometry = feature.getGeometry();
var geometry = style.getGeometryFunction()(feature);
if (!goog.isDefAndNotNull(geometry) ||
!ol.extent.intersects(this.extent_, geometry.getExtent())) {
return;

View File

@@ -123,7 +123,7 @@ ol.renderer.vector.renderFeature = function(
*/
ol.renderer.vector.renderFeature_ = function(
replayGroup, feature, style, squaredTolerance) {
var geometry = feature.getGeometry();
var geometry = style.getGeometryFunction()(feature);
if (!goog.isDefAndNotNull(geometry)) {
return;
}

View File

@@ -118,7 +118,7 @@ ol.render.webgl.Immediate.prototype.drawCircleGeometry =
* @api
*/
ol.render.webgl.Immediate.prototype.drawFeature = function(feature, style) {
var geometry = feature.getGeometry();
var geometry = style.getGeometryFunction()(feature);
if (!goog.isDefAndNotNull(geometry) ||
!ol.extent.intersects(this.extent_, geometry.getExtent())) {
return;

View File

@@ -1,7 +1,9 @@
goog.provide('ol.style.Style');
goog.provide('ol.style.defaultGeometryFunction');
goog.require('goog.asserts');
goog.require('goog.functions');
goog.require('ol.geom.Geometry');
goog.require('ol.geom.GeometryType');
goog.require('ol.style.Circle');
goog.require('ol.style.Fill');
@@ -24,6 +26,22 @@ ol.style.Style = function(opt_options) {
var options = goog.isDef(opt_options) ? opt_options : {};
/**
* @private
* @type {string|ol.geom.Geometry|ol.style.GeometryFunction}
*/
this.geometry_ = null;
/**
* @private
* @type {!ol.style.GeometryFunction}
*/
this.geometryFunction_ = ol.style.defaultGeometryFunction;
if (goog.isDef(options.geometry)) {
this.setGeometry(options.geometry);
}
/**
* @private
* @type {ol.style.Fill}
@@ -57,6 +75,27 @@ ol.style.Style = function(opt_options) {
};
/**
* @return {string|ol.geom.Geometry|ol.style.GeometryFunction}
* Feature property or geometry or function that returns the geometry that will
* be rendered with this style.
* @api
*/
ol.style.Style.prototype.getGeometry = function() {
return this.geometry_;
};
/**
* @return {!ol.style.GeometryFunction} Function that is called with a feature
* and returns the geometry to render instead of the feature's geometry.
* @api
*/
ol.style.Style.prototype.getGeometryFunction = function() {
return this.geometryFunction_;
};
/**
* @return {ol.style.Fill} Fill style.
* @api
@@ -102,6 +141,37 @@ ol.style.Style.prototype.getZIndex = function() {
};
/**
* Set a geometry that is rendered instead of the feature's geometry.
*
* @param {string|ol.geom.Geometry|ol.style.GeometryFunction} geometry
* Feature property or geometry or function returning a geometry to render
* for this style.
* @api
*/
ol.style.Style.prototype.setGeometry = function(geometry) {
if (goog.isFunction(geometry)) {
this.geometryFunction_ = geometry;
} else if (goog.isString(geometry)) {
this.geometryFunction_ = function(feature) {
var result = feature.get(geometry);
if (goog.isDefAndNotNull(result)) {
goog.asserts.assertInstanceof(result, ol.geom.Geometry);
}
return result;
};
} else if (goog.isNull(geometry)) {
this.geometryFunction_ = ol.style.defaultGeometryFunction;
} else if (goog.isDef(geometry)) {
goog.asserts.assertInstanceof(geometry, ol.geom.Geometry);
this.geometryFunction_ = function() {
return geometry;
};
}
this.geometry_ = geometry;
};
/**
* Set the zIndex.
*
@@ -264,3 +334,24 @@ ol.style.createDefaultEditingStyles = function() {
return styles;
};
/**
* A function that takes an {@link ol.Feature} as argument and returns an
* {@link ol.geom.Geometry} that will be rendered and styled for the feature.
*
* @typedef {function(ol.Feature): (ol.geom.Geometry|undefined)}
* @api
*/
ol.style.GeometryFunction;
/**
* Function that is called with a feature and returns its default geometry.
* @param {ol.Feature} feature Feature to get the geometry for.
* @return {ol.geom.Geometry|undefined} Geometry to render.
*/
ol.style.defaultGeometryFunction = function(feature) {
goog.asserts.assert(!goog.isNull(feature));
return feature.getGeometry();
};