Skip to content

Commit

Permalink
Merge pull request #462 from oterral/geomfunc
Browse files Browse the repository at this point in the history
Use all the styles defined and display geometries coming from style
  • Loading branch information
gberaudo authored May 23, 2017
2 parents be90410 + 55ccc64 commit 51f1fa1
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 11 deletions.
15 changes: 12 additions & 3 deletions examples/vectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var iconFeature = new ol.Feature({
});

var textFeature = new ol.Feature({
geometry: new ol.geom.Point([1000000, 3000000, 50000])
geometry: new ol.geom.Point([1000000, 3000000, 500000])
});

var cervinFeature = new ol.Feature({
Expand Down Expand Up @@ -34,7 +34,7 @@ var iconStyle = new ol.style.Style({
})
});

var textStyle = new ol.style.Style({
var textStyle = [new ol.style.Style({
text: new ol.style.Text({
text: 'Only text',
textAlign: 'center',
Expand All @@ -47,7 +47,16 @@ var textStyle = new ol.style.Style({
color: 'rgba(0, 0, 155, 0.3)'
})
})
});
}), new ol.style.Style({
geometry: new ol.geom.Circle([1000000, 3000000, 10000], 2e6),
stroke: new ol.style.Stroke({
color: 'blue',
width: 2
}),
fill:new ol.style.Fill({
color: 'rgba(0, 0, 255, 0.2)'
})
})]

iconFeature.setStyle(iconStyle);

Expand Down
60 changes: 52 additions & 8 deletions src/featureconverter.js
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ olcs.FeatureConverter.prototype.olStyleToCesium = function(feature, style, outli
* @param {!ol.Feature} feature
* @param {ol.StyleFunction|undefined} fallbackStyleFunction
* @param {number} resolution
* @return {ol.style.Style} null if no style is available
* @return {Array.<!ol.style.Style>} null if no style is available
* @api
*/
olcs.FeatureConverter.prototype.computePlainStyle = function(layer, feature, fallbackStyleFunction, resolution) {
Expand Down Expand Up @@ -866,7 +866,7 @@ olcs.FeatureConverter.prototype.computePlainStyle = function(layer, feature, fal
// then this function must return a custom material
// More simply, could blend the colors like described in
// http://en.wikipedia.org/wiki/Alpha_compositing
return Array.isArray(style) ? style[0] : style;
return Array.isArray(style) ? style : [style];
};


Expand All @@ -883,6 +883,14 @@ olcs.FeatureConverter.prototype.computePlainStyle = function(layer, feature, fal
olcs.FeatureConverter.prototype.olFeatureToCesium = function(layer, feature, style, context, opt_geom) {
let geom = opt_geom || feature.getGeometry();
const proj = context.projection;

if (!opt_geom && style) {
const geomFuncRes = style.getGeometryFunction()(feature);
if (geomFuncRes instanceof ol.geom.Geometry) {
geom = geomFuncRes;
}
}

if (!geom) {
// OpenLayers features may not have a geometry
// See http://geojson.org/geojson-spec.html#feature-objects
Expand Down Expand Up @@ -1001,13 +1009,31 @@ olcs.FeatureConverter.prototype.olVectorLayerToCesium = function(olLayer, olView
} else {
layerStyle = olLayer.getStyleFunction();
}
const style = this.computePlainStyle(olLayer, feature, layerStyle,
const styles = this.computePlainStyle(olLayer, feature, layerStyle,
resolution);
if (!style) {
if (!styles.length) {
// only 'render' features with a style
continue;
}
const primitives = this.olFeatureToCesium(olLayer, feature, style, context);

/**
* @type {Cesium.Primitive|null}
*/
let primitives = null;
for (let i = 0; i < styles.length; i++) {
const prims = this.olFeatureToCesium(olLayer, feature, styles[i], context);
if (prims) {
if (!primitives) {
primitives = prims;
} else {
let i = 0, prim;
while ((prim = prims.get(i))) {
primitives.add(prim);
i++;
}
}
}
}
if (!primitives) {
continue;
}
Expand Down Expand Up @@ -1050,13 +1076,31 @@ olcs.FeatureConverter.prototype.convert = function(layer, view, feature, context
} else {
layerStyle = layer.getStyleFunction();
}
const style = this.computePlainStyle(layer, feature, layerStyle, resolution);

if (!style) {
const styles = this.computePlainStyle(layer, feature, layerStyle, resolution);

if (!styles.length) {
// only 'render' features with a style
return null;
}

context.projection = proj;
return this.olFeatureToCesium(layer, feature, style, context);

/**
* @type {Cesium.Primitive|null}
*/
let primitives = null;
for (let i = 0; i < styles.length; i++) {
const prims = this.olFeatureToCesium(layer, feature, styles[i], context);
if (!primitives) {
primitives = prims;
} else {
let i = 0, prim;
while ((prim = prims.get(i))) {
primitives.add(prim);
i++;
}
}
}
return primitives;
};

0 comments on commit 51f1fa1

Please sign in to comment.