source: src/olheatmap/js/Heatmap.php@ 9407

Last change on this file since 9407 was 8983, checked in by dennisw, 14 years ago

index.html - linkt nu naar goede script
Heatmap.php - renamed

File size: 5.1 KB
Line 
1/* OpenLayersHeatmap
2 * Copyright (C) 2010 Felipe Barriga Richards
3 *
4 * This Program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
7 * any later version.
8 *
9 * This Program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with RTMPDump; see the file COPYING. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 */
21
22/*
23 * Edited by Dennis Wagenaar
24 */
25
26
27<?php
28function connectdb()
29{
30 mysql_connect("location", "username", "password")
31 or die("Error connecting to mysql: " . mysql_error());
32 mysql_select_db('database')
33 or die("Error connecting to database: " . mysql_error());
34}
35connectdb();
36$table = 'table';
37?>
38
39
40var layerMapnik = null;
41var lastZoom = null;
42var vectorStyleCache = null;
43
44function addToVectorStyleCache(vectorStyle, hash) {
45 if (vectorStyleCache == null)
46 vectorStyleCache = new Array();
47 vectorStyleCache[hash] = vectorStyle;
48}
49
50
51function getVectorStyle(size, color) {
52 var vectorStyle = null;
53 var hash = size + '_' + color;
54 if ( (vectorStyleCache != null) && (vectorStyleCache[hash] != null) ) {
55 vectorStyle = vectorStyleCache[hash];
56 } else {
57 vectorStyle = OpenLayers.Util.extend({}, OpenLayers.Feature.Vector.style['default']);
58 vectorStyle.graphicWidth = size;
59 vectorStyle.graphicHeight = size;
60 vectorStyle.graphicXOffset = -size/2;
61 vectorStyle.graphicYOffset = -size/2;
62 vectorStyle.externalGraphic = 'images/marker_' + color + '.png';
63
64 addToVectorStyleCache(vectorStyle, hash);
65 }
66 return vectorStyle;
67}
68
69
70function scaleRadius(radius) {
71 var zoom = map.getZoom();
72 var scaleFactor = 1 / (1.1 * Math.pow(2, 16 - zoom));
73 radius *= scaleFactor;
74
75 return radius;
76}
77
78
79function drawMarkers() {
80
81 var points = new Array();
82 var lonLat, vectorStyle, point, pointVector, color, size;
83
84 layerVectors.destroyFeatures();
85
86 <?php
87 $result=mysql_query("SELECT longitude, latitude FROM ".$table." ");
88 while($row=mysql_fetch_array($result))
89 {
90 echo "lonLat = new OpenLayers.LonLat(".$row['longitude'].", ".$row['latitude'].").transform(map.displayProjection,map.getProjectionObject());";
91 echo "color = 'green';
92 size = '25';
93 size = scaleRadius(size);
94 vectorStyle = getVectorStyle(size, color);
95 point = new OpenLayers.Geometry.Point(lonLat.lon, lonLat.lat);
96 pointVector= new OpenLayers.Feature.Vector(point, null, vectorStyle);
97 points.push(pointVector);";
98 }
99 ?>
100
101
102 layerVectors.addFeatures(points);
103}
104
105
106function generateHeatmap() {
107 var width = layerVectors.renderer.canvas.canvas.width;
108 var height = layerVectors.renderer.canvas.canvas.height;
109
110 var context = layerVectors.renderer.canvas.canvas.getContext('2d');
111 var myImageData = context.getImageData(0, 0, width, height);
112 var pix = myImageData.data;
113
114 for (var i = 0, n = pix.length; i < n; i += 4) {
115 // i+3 is alpha (the fourth element)
116 if (pix[i+3] <= 64) {
117 pix[i+2] = 255; // blue
118 pix[i+1] = (255/64)*pix[i+3]; // green
119 pix[i ] = 0; // red
120 } else if (pix[i+3] <= 128) {
121 pix[i+1] = 255;
122 pix[i+2] = (255/64)*(128-pix[i+3]);
123 pix[i ] = 0;
124 } else if (pix[i+3] <= 192) {
125 pix[i+1] = 255;
126 pix[i+2] = 0;
127 pix[i ] = (255/64)*(pix[i+3]-128);
128 } else if (pix[i+3] <= 255) {
129 pix[i+1] = (255/64)*(255-pix[i+3]);
130 pix[i+2] = 0;
131 pix[i ] = 255;
132 }
133 }
134 context.putImageData(myImageData, 0, 0);
135}
136
137
138function mapMoveEndEvent(event) {
139 if (event.type == 'moveend') {
140 if (lastZoom != map.getZoom()) {
141 lastZoom = map.getZoom();
142 onChangeParams(true);
143 }
144 Concurrent.Thread.create(function() {
145 Concurrent.Thread.sleep(50);
146 layerVectors.renderer.redraw();
147 })
148 }
149}
150
151
152function onChangeParams(_drawMarkers) {
153 vectorStyleCache = null;
154 if (_drawMarkers)
155 drawMarkers();
156}
157
158function clearMarkers() {
159 markers = new Array();
160 vectorialMarkers = new Array();
161 layerVectors.destroyFeatures();
162}
163
164
165
166function init() {
167 map = new OpenLayers.Map("heatmap", {
168 eventListeners: {
169 "moveend": mapMoveEndEvent
170 },
171 controls: [
172 new OpenLayers.Control.Navigation(),
173 new OpenLayers.Control.PanZoomBar(),
174 new OpenLayers.Control.ScaleLine()
175 ],
176 maxExtent: new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34),
177 maxResolution: 156543.0399,
178 numZoomLevels: 19,
179 units: 'm',
180 projection: new OpenLayers.Projection("EPSG:900913"),
181 displayProjection: new OpenLayers.Projection("EPSG:4326")
182 });
183
184 layerMapnik = new OpenLayers.Layer.OSM.Mapnik("Mapnik");
185 map.addLayer(layerMapnik);
186
187 layerVectors = new OpenLayers.Layer.Vector("Features", {renderers: ["Canvas"]});
188 map.addLayer(layerVectors);
189
190 var lon = 4.4585017;
191 var lat = 52.1406583;
192 var zoom = 16;
193
194 var lonLat = new OpenLayers.LonLat(lon, lat).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());
195 map.setCenter (lonLat, zoom);
196
197 lastZoom = zoom;
198
199}
Note: See TracBrowser for help on using the repository browser.