[OpenLayers-Users] Style, Rule and Comparison
Gwennaël matot
gwennael.matot.ext at makina-corpus.com
Fri Jun 20 09:26:46 EDT 2008
Hi list !
I'd like some clarification on the use of the "Comparison" class.
I have many feature defined with GeoJSON. I want to change style of
feature according to an attribute "nb_voyage".
Here a part of my test :
/* DATA */
var waypoints = {
type: "FeatureCollection",
features: [{
type: "Feature",
geometry: { type: "LineString", coordinates: [ [
0.04863281249999929, 40.00107421875 ] , [ 0.3452636718749993,
38.81455078125 ] ] },
crs: { type: "OGC", properties: { urn:
"urn:ogc:def:crs:OGC:1.3:CRS84" } },
properties: { nb_voyage: 3, from: "Point 1", to: "Point 10" }
},{
type: "Feature",
geometry: { type: "LineString", coordinates: [ [
-0.3139160156250007, 39.495703125 ] , [ 0.3452636718749993,
38.81455078125 ] ] },
crs: { type: "OGC", properties: { urn:
"urn:ogc:def:crs:OGC:1.3:CRS84" } },
properties: { nb_voyage: 18, from: "Point 5", to: "Point 8" }
}]
};
/* BUILD STYLE */
var style = new OpenLayers.Style({strokeOpacity: 1, strokeLinecap:
"round", strokeWidth: 2});
var ruleLow = new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.LESS_THAN,
property: "nb_voyage",
value: 15,
symbolizer: {strokeColor: "#FF0000" }
});
var ruleHigh = new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.GREATER_THAN_OR_EQUAL_TO,
property: "nb_voyage",
value: 15,
symbolizer: {strokeColor: "#00FF00" }
});
var elseRule = new OpenLayers.Rule({
elseFilter: true,
symbolizer: {strokeColor: "#0000FF" }
});
style.addRules([ruleLow, ruleHigh, elseRule]);
/* ADD DATA TO LAYER */
var geojson_format = new OpenLayers.Format.GeoJSON();
var layer = new OpenLayers.Layer.Vector("mylayer", {
styleMap: new OpenLayers.StyleMap({
default: style
})
});
layer.addFeatures(geojson_format.read(waypoints));
map.addLayer(layer);
But the elseFilter rule is always applied !
After many trackback I saw in Comparison.evaluate(context) function that
"context" is the feature.
The comparison is done on the root properties of the feature
(feature.nb_voyage), but my attribute is in "attributes"
(feature.attributes.nb_voyage).
Can i change this way ?
Thanks for your help
Gwennaël Matot
PS : the features are like to :
feature: {
CLASS_NAME: "OpenLayers.Feature.Vector",
attributes: {
from: "Point 1"
to: "Point 2"
nb_voyage: 5
},
data: {
from: "Point 1"
to: "Point 2"
nb_voyage: 5
},
geometry: ...,
renderIntent: "default",
style: null,
...
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/openlayers-users/attachments/20080620/e520c87b/attachment.html
More information about the Users
mailing list