<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
<div class="moz-text-flowed"
style="font-family: -moz-fixed; font-size: 12px;" lang="x-western">Hi
list !
<br>
<br>
I'd like some clarification on the use of the "Comparison" class.
<br>
I have many feature defined with GeoJSON. I want to change style of
feature according to an attribute "nb_voyage".
<br>
Here a part of my test :
<br>
<br>
/* DATA */
<br>
var waypoints = {
<br>
type: "FeatureCollection",
<br>
features: [{
<br>
type: "Feature",
<br>
geometry: { type: "LineString", coordinates: [ [
0.04863281249999929, 40.00107421875 ] , [ 0.3452636718749993,
38.81455078125 ] ] },
<br>
crs: { type: "OGC", properties: { urn:
"urn:ogc:def:crs:OGC:1.3:CRS84" } },
<br>
properties: { nb_voyage: 3, from: "Point 1", to: "Point 10"
}
<br>
},{
<br>
type: "Feature",
<br>
geometry: { type: "LineString", coordinates: [ [
-0.3139160156250007, 39.495703125 ] , [ 0.3452636718749993,
38.81455078125 ] ] },
<br>
crs: { type: "OGC", properties: { urn:
"urn:ogc:def:crs:OGC:1.3:CRS84" } },
<br>
properties: { nb_voyage: 18, from: "Point 5", to: "Point 8"
}
<br>
}]
<br>
};
<br>
<br>
/* BUILD STYLE */
<br>
var style = new OpenLayers.Style({strokeOpacity: 1, strokeLinecap:
"round", strokeWidth: 2});
<br>
var ruleLow = new OpenLayers.Filter.Comparison({
<br>
type: OpenLayers.Filter.Comparison.LESS_THAN,
<br>
property: "nb_voyage",
<br>
value: 15,
<br>
symbolizer: {strokeColor: "#FF0000" }
<br>
});
<br>
var ruleHigh = new OpenLayers.Filter.Comparison({
<br>
type: OpenLayers.Filter.Comparison.GREATER_THAN_OR_EQUAL_TO,
<br>
property: "nb_voyage",
<br>
value: 15,
<br>
symbolizer: {strokeColor: "#00FF00" }
<br>
});
<br>
var elseRule = new OpenLayers.Rule({
<br>
elseFilter: true,
<br>
symbolizer: {strokeColor: "#0000FF" }
<br>
});
<br>
style.addRules([ruleLow, ruleHigh, elseRule]);
<br>
/* ADD DATA TO LAYER */
<br>
var geojson_format = new OpenLayers.Format.GeoJSON();
<br>
var layer = new OpenLayers.Layer.Vector("mylayer", {
<br>
styleMap: new OpenLayers.StyleMap({
<br>
default: style
<br>
})
<br>
});
<br>
layer.addFeatures(geojson_format.read(waypoints));
<br>
map.addLayer(layer);
<br>
<br>
But the elseFilter rule is always applied !
<br>
<br>
After many trackback I saw in Comparison.evaluate(context) function
that "context" is the feature.
<br>
The comparison is done on the root properties of the feature
(feature.nb_voyage), but my attribute is in "attributes"
(feature.attributes.nb_voyage).
<br>
Can i change this way ?
<br>
<br>
Thanks for your help
<br>
Gwennaël Matot
<br>
<br>
<br>
PS : the features are like to :
<br>
feature: {
<br>
CLASS_NAME: "OpenLayers.Feature.Vector",
<br>
attributes: {
<br>
from: "Point 1"
<br>
to: "Point 2"
<br>
nb_voyage: 5
<br>
},
<br>
data: {
<br>
from: "Point 1"
<br>
to: "Point 2"
<br>
nb_voyage: 5
<br>
},
<br>
geometry: ...,
<br>
renderIntent: "default",
<br>
style: null,
<br>
...
<br>
}
<br>
<br>
</div>
</body>
</html>