heatmap-openlayers-renderer.js
12.4 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/* Copyright 2013 Xavier Mamano, http://github.com/jorix/OL-Ragbag
* Published under MIT license. All rights reserved.
*
* Based on lib/OpenLayers/Renderer/Canvas.js form:
* https://github.com/openlayers/openlayers/blob/master
* with license:
* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
* full list of contributors). Published under the 2-clause BSD license.
* See license.txt in the OpenLayers distribution or repository for the
* full text of the license. */
/**
* @requires OpenLayers/Renderer.js
*/
/**
* Class: OpenLayers.Renderer.Heatmap
* A renderer based on https://github.com/pa7/heatmap.js
*
* Inherits:
* - <OpenLayers.Renderer>
*/
OpenLayers.Renderer.Heatmap = OpenLayers.Class(OpenLayers.Renderer, {
/**
* APIProperty: weight
* {String||Function} Each feature weight, if string refers to the name of a
* numeric attribute of the features, if function receives the feature
* as argument and should return a number, if not every feature has a
* weight equal to 0.
*/
weight: null,
/**
* APIProperty: maxStart
* {Fload} Set to ensure maximum value not less than the indicated value.
* Default is 0.
*/
maxStart: 0,
/**
* APIProperty: updatableWeight
* {Fload} Set to true if the features can change the weight after added to
* the layer. In this case it is necessary call drawFeature from the
* layer to update the rendered feature.
*/
updatableWeight: false,
/**
* APIProperty: heatmapConfig
* {Object} See documentation at
* https://github.com/pa7/heatmap.js/blob/master/README.md.
*
* Note that the following heatmap.js options are ignored: element,
* visible, width, height.
*/
heatmapConfig: null,
/**
* Property: heatmap
* {heatmap.js} The heatmap.js object.
*/
heatmap: null,
/**
* Property: extentHM
* Heatmap extent, increases <extend> with the radius so this prevents the
* attenuation in areas near the edges.
* {<OpenLayers.Bounds>}
*/
extentHM: null,
/**
* Property: features
* {Object} Internal object of feature/style pairs for use in redrawing the layer.
*/
features: null,
/**
* Property: pendingRedraw
* {Boolean} The renderer needs a redraw call to render features added while
* the renderer was locked.
*/
pendingRedraw: false,
/**
* Property: pendingMax
* {Boolean} The renderer needs calculate max to render features drawed
* while the renderer was not locked.
*/
pendingMax: false,
/**
* Constructor: OpenLayers.Renderer.Heatmap
*
* Parameters:
* containerID - {<String>}
* options - {Object} Optional properties to be set on the renderer.
*/
initialize: function(containerID, options) {
OpenLayers.Renderer.prototype.initialize.apply(this, arguments);
var heatmapConfig = OpenLayers.Util.applyDefaults({
visible: true,
element: this.container
},
this.heatmapConfig || {}
);
var _weight = this.weight;
if (!_weight) {
this.weight = function() {
return 0;
};
} else if (typeof _weight === 'string') {
this.weight = function(feature) {
return feature.attributes[_weight] || 0;
};
}
var hm = h337.create(heatmapConfig);
this.heatmap = hm;
this.root = hm.get("canvas");
this.maxStart = this.maxStart || 0;
this.features = {};
this.pendingMax = false;
this.max = this.maxStart;
},
/**
* Method: setExtent
* Set the visible part of the layer.
*
* Parameters:
* extent - {<OpenLayers.Bounds>}
* resolutionChanged - {Boolean}
*
* Returns:
* {Boolean} true to notify the layer that the new extent does not exceed
* the coordinate range, and the features will not need to be redrawn.
* False otherwise.
*/
setExtent: function() {
OpenLayers.Renderer.prototype.setExtent.apply(this, arguments);
var mapRadius = this.getResolution() * this.heatmap.get("radius"),
extent = this.extent;
this.extentHM = new OpenLayers.Bounds(
extent.left - mapRadius,
extent.bottom - mapRadius,
extent.right + mapRadius,
extent.top + mapRadius
);
// always redraw features
return false;
},
/**
* Method: eraseGeometry
* Erase a geometry from the renderer. Because the Canvas renderer has
* 'memory' of the features that it has drawn, we have to remove the
* feature so it doesn't redraw.
*
* Parameters:
* geometry - {<OpenLayers.Geometry>}
* featureId - {String}
*/
eraseGeometry: function(geometry, featureId) {
this.eraseFeatures(this.features[featureId][0]);
},
/**
* APIMethod: supported
*
* Returns:
* {Boolean} Whether or not the browser supports the renderer class
*/
supported: function() {
return !!window.h337 && OpenLayers.CANVAS_SUPPORTED;
},
/**
* Method: setSize
* Sets the size of the drawing surface.
*
* Once the size is updated, redraw the canvas.
*
* Parameters:
* size - {<OpenLayers.Size>}
*/
setSize: function(size) {
this.size = size.clone();
var root = this.root;
root.style.width = size.w + "px";
root.style.height = size.h + "px";
root.width = size.w;
root.height = size.h;
this.resolution = null;
},
/**
* Method: drawFeature
* Draw the feature. Stores the feature in the features list,
* then redraws the layer.
*
* Parameters:
* feature - {<OpenLayers.Feature.Vector>}
* style - {<Object>}
*
* Returns:
* {Boolean} The feature has been drawn completely. If the feature has no
* geometry, undefined will be returned. If the feature is not rendered
* for other reasons, false will be returned.
*/
drawFeature: function(feature, style) {
var rendered;
if (feature.geometry) {
style = this.applyDefaultSymbolizer(style || feature.style);
// don't render if display none or feature outside extent
var weight,
bounds = feature.geometry.getBounds();
var worldBounds;
if (this.map.baseLayer && this.map.baseLayer.wrapDateLine) {
worldBounds = this.map.getMaxExtent();
}
rendered = (style.display !== "none");
if (rendered && bounds) {
rendered = bounds.intersectsBounds(
this.extentHM,
{worldBounds: worldBounds}
);
// Get max weight of all features
weight = this.weight(feature);
if (this.max < weight) {
this.max = weight;
}
} else {
rendered = false;
}
if (rendered) {
// keep track of what we have rendered for redraw
this.features[feature.id] = [feature, weight];
}
else {
// remove from features tracked for redraw
delete(this.features[feature.id]);
}
this.pendingRedraw = true;
}
if (this.pendingRedraw && !this.locked) {
if (this.updatableWeight && this.pendingMax) {
this.calculateMax();
}
this.redraw();
this.pendingRedraw = false;
this.pendingMax = true;
}
return rendered;
},
/**
* Method: getLocalXY
* Transform geographic coordinates into pixel xy
*
* Parameters:
* lacation - {<OpenLayers.LonLat>}
*/
getLocalXY: function(lacation) {
var resolution = this.getResolution();
var extent = this.extent;
var x = ((lacation.lon - this.featureDx) / resolution + (-extent.left / resolution));
var y = ((extent.top / resolution) - lacation.lat / resolution);
return [Math.round(x), Math.round(y)];
},
/**
* Method: clear
* Clear all vectors from the renderer.
*/
clear: function() {
var hm = this.heatmap;
hm.set("height", this.root.height);
hm.set("width", this.root.width);
hm.clear();
this.features = {};
this.pendingMax = false;
this.max = this.maxStart;
},
/**
* Method: getFeatureIdFromEvent
* Returns a feature id from an event on the renderer.
*
* Parameters:
* evt - {<OpenLayers.Event>}
*
* Returns:
* {<OpenLayers.Feature.Vector} A feature or undefined. This method returns
* a feature instead of a feature id to avoid an unnecessary lookup on
* the layer.
*/
getFeatureIdFromEvent: function(evt) {
// TODO: select?
var featureId, feature;
return feature;
},
/**
* Method: eraseFeatures
* This is called by the layer to erase features; removes the feature from
* the list, then redraws the layer.
*
* Parameters:
* features - {Array(<OpenLayers.Feature.Vector>)}
*/
eraseFeatures: function(features) {
if(!(OpenLayers.Util.isArray(features))) {
features = [features];
}
for(var i=0; i<features.length; ++i) {
delete this.features[features[i].id];
}
if (!this.locked) {
this.calculateMax();
this.redraw();
}
},
/**
* Method: calculateMax
*/
calculateMax: function() {
var max = this.maxStart;
if (this.map) {
var layer = this.map.getLayer(this.container.id);
if (layer) {
var features = layer.features,
weight;
for (var i = 0, len = features.length; i < len; i++) {
weight = this.weight(features[i]);
if (max < weight) {
max = weight;
}
}
}
}
this.pendingMax = false;
this.max = max;
},
/**
* Method: redraw
* The real 'meat' of the function: any time things have changed,
* redraw() can be called to loop over all the data and (you guessed
* it) redraw it. Unlike Elements-based Renderers, we can't interact
* with things once they're drawn, to remove them, for example, so
* instead we have to just clear everything and draw from scratch.
*/
redraw: function() {
if (!this.locked) {
var height = this.root.height;
var width = this.root.width;
var hm = this.heatmap;
hm.set("height", height);
hm.set("width", width);
hm.resize();
var labelMap = [];
var feature, bounds, style;
var worldBounds = (this.map.baseLayer &&
this.map.baseLayer.wrapDateLine) &&
this.map.getMaxExtent();
var data = [],
item = 0,
weight,
getWeight = this.weight,
features = [];
// What is the layer?,
// we need to use all features although not in the map window.
for (var id in this.features) {
if (!this.features.hasOwnProperty(id)) { continue; }
item = this.features[id];
feature = item[0];
bounds = feature.geometry.getBounds();
this.calculateFeatureDx(bounds, worldBounds);
var pt = this.getLocalXY(bounds.getCenterLonLat());
var p0 = pt[0];
var p1 = pt[1];
if(!isNaN(p0) && !isNaN(p1)) {
weight = item[1];
data.push({x: p0, y: p1, count: weight});
}
}
this.heatmap.store.setDataSet({
data: data,
max: this.max
});
}
},
CLASS_NAME: "OpenLayers.Renderer.Heatmap"
});