1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
/**
* Class: OpenLayers.Strategy.AttributeCluster
* Strategy for vector feature clustering based on feature attributes.
*
* Inherits from:
* - <OpenLayers.Strategy.Cluster>
*/
OpenLayers.Strategy.AttributeCluster = OpenLayers.Class(OpenLayers.Strategy.Cluster, {
/**
* the attribute to use for comparison
*/
attribute: null,
/**
* Method: shouldCluster
* Determine whether to include a feature in a given cluster.
*
* Parameters:
* cluster - {<OpenLayers.Feature.Vector>} A cluster.
* feature - {<OpenLayers.Feature.Vector>} A feature.
*
* Returns:
* {Boolean} The feature should be included in the cluster.
*/
shouldCluster: function(cluster, feature) {
var cc_attrval = cluster.cluster[0].attributes[this.attribute];
var fc_attrval = feature.attributes[this.attribute];
var superProto = OpenLayers.Strategy.Cluster.prototype;
return cc_attrval === fc_attrval &&
superProto.shouldCluster.apply(this, arguments);
},
CLASS_NAME: "OpenLayers.Strategy.AttributeCluster"
});
/**
* Class: OpenLayers.Strategy.RuleCluster
* Strategy for vector feature clustering according to a given rule.
*
* Inherits from:
* - <OpenLayers.Strategy.Cluster>
*/
OpenLayers.Strategy.RuleCluster = OpenLayers.Class(OpenLayers.Strategy.Cluster, {
/**
* the rule to use for comparison
*/
rule: null,
/**
* Method: shouldCluster
* Determine whether to include a feature in a given cluster.
*
* Parameters:
* cluster - {<OpenLayers.Feature.Vector>} A cluster.
* feature - {<OpenLayers.Feature.Vector>} A feature.
*
* Returns:
* {Boolean} The feature should be included in the cluster.
*/
shouldCluster: function(cluster, feature) {
var superProto = OpenLayers.Strategy.Cluster.prototype;
return this.rule.evaluate(cluster.cluster[0]) &&
this.rule.evaluate(feature) &&
superProto.shouldCluster.apply(this, arguments);
},
CLASS_NAME: "OpenLayers.Strategy.RuleCluster"
});
// global variables
var map, vectorlayer, features, stylemap, select;
// wrap the instanciation code in an anonymous function that gets executed
// immeadeately
(function(){
// The function that gets called on feature selection: shows information
// about the feature/cluser in a div on the page
var showInformation = function(evt){
var feature = evt.feature;
var info = 'Last hovered feature:<br>';
if (feature.cluster) {
info += ' Cluster of ' + feature.attributes.count + ' features:';
var clazzes = {
'1': 0,
'2': 0,
'3': 0,
'4': 0
};
for (var i = 0; i < feature.attributes.count; i++) {
var feat = feature.cluster[i];
clazzes[feat.attributes.clazz]++;
}
for (var j=1; j<=4; j++) {
var plural_s = (clazzes[j] !== 1) ? 's' : '';
info += '<br> • clazz ' + j + ': ' + clazzes[j] + ' feature' + plural_s;
}
} else {
info += ' Single feature of clazz = ' + feature.attributes.clazz;
}
document.getElementById('info').innerHTML = info;
};
// The function that gets called on feature selection. Shows information
// about the number of "points" on the map.
var updateGeneralInformation = function() {
var info = 'Currently ' + vectorlayer.features.length + ' points are shown on the map.';
document.getElementById('generalinfo').innerHTML = info;
};
// instanciate the map
map = new OpenLayers.Map("map");
// background WMS
var ol_wms = new OpenLayers.Layer.WMS("OpenLayers WMS", "http://vmap0.tiles.osgeo.org/wms/vmap0", {
layers: "basic"
});
// context to style the vectorlayer
var context = {
getColor: function(feature){
var color = '#aaaaaa';
if (feature.attributes.clazz && feature.attributes.clazz === 4) {
color = '#ee0000';
} else if(feature.cluster) {
var onlyFour = true;
for (var i = 0; i < feature.cluster.length; i++) {
if (onlyFour && feature.cluster[i].attributes.clazz !== 4) {
onlyFour = false;
}
}
if (onlyFour === true) {
color = '#ee0000';
}
}
return color;
}
};
// style the vectorlayer
stylemap = new OpenLayers.StyleMap({
'default': new OpenLayers.Style({
pointRadius: 5,
fillColor: "${getColor}",
fillOpacity: 0.7,
strokeColor: "#666666",
strokeWidth: 1,
strokeOpacity: 1,
graphicZIndex: 1
}, {
context: context
}),
'select' : new OpenLayers.Style({
pointRadius: 5,
fillColor: "#ffff00",
fillOpacity: 1,
strokeColor: "#666666",
strokeWidth: 1,
strokeOpacity: 1,
graphicZIndex: 2
})
});
// the vectorlayer
vectorlayer = new OpenLayers.Layer.Vector('Vectorlayer', {styleMap: stylemap, strategies: []});
// the select control
select = new OpenLayers.Control.SelectFeature(
vectorlayer, {hover: true}
);
map.addControl(select);
select.activate();
vectorlayer.events.on({"featureselected": showInformation});
map.addLayers([ol_wms, vectorlayer]);
map.addControl(new OpenLayers.Control.LayerSwitcher());
map.zoomToMaxExtent();
features = [];
// adding lots of features:
for (var i = 0; i < 700; i++) {
var r1 = Math.random();
var r2 = Math.random();
var r3 = Math.random();
var r4 = Math.random();
var px = r1 * 180 * ((r2 < 0.5) ? -1 : 1);
var py = r3 * 90 * ((r4 < 0.5) ? -1 : 1);
var p = new OpenLayers.Geometry.Point(px, py);
var clazz = (i % 10 === 0) ? 4 : Math.ceil(r4 * 3);
var f = new OpenLayers.Feature.Vector(p, {clazz: clazz});
features.push(f);
}
vectorlayer.addFeatures(features);
updateGeneralInformation();
// the behaviour and methods for the radioboxes
var changeStrategy = function() {
var strategies = [];
// this is the checkbox
switch(this.value) {
case 'cluster':
// standard clustering
strategies.push(new OpenLayers.Strategy.Cluster());
break;
case 'attribute-cluster':
// use the custom class: only cluster features of the same clazz
strategies.push(new OpenLayers.Strategy.AttributeCluster({
attribute:'clazz'
}));
break;
case 'rule-cluster':
// use the custom class: only cluster features that have a
// clazz smaller than 4
strategies.push(new OpenLayers.Strategy.RuleCluster({
rule: new OpenLayers.Rule({
filter: new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.LESS_THAN,
property: "clazz",
value: 4
})
})
}));
break;
}
// remove layer and control
map.removeLayer(vectorlayer);
map.removeControl(select);
// rebuild layer
vectorlayer = new OpenLayers.Layer.Vector('Vectorlayer', {styleMap: stylemap, strategies: strategies});
map.addLayer( vectorlayer );
vectorlayer.addFeatures(features);
// rebuild select control
select = new OpenLayers.Control.SelectFeature(
vectorlayer, {hover: true}
);
map.addControl(select);
select.activate();
vectorlayer.events.on({"featureselected": showInformation});
// update meta information
updateGeneralInformation();
};
// bind the behviour to the radios
var inputs = document.getElementsByTagName('input');
for( var cnt = 0; cnt < inputs.length; cnt++) {
var input = inputs[cnt];
if (input.name === 'strategy') {
input.onclick = changeStrategy;
}
}
})();
|