source: src/olheatmap/js/test_1.php@ 8971

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

olheatmap - aangepast en toegevoegd, werkt met lokale db, dus zal online nog niet werken

File size: 5.0 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 <?php
85 $result=mysql_query("SELECT longitude, latitude FROM ".$table." LIMIT 100");
86 while($row=mysql_fetch_array($result))
87 {
88 echo "lonLat = new OpenLayers.LonLat(".$row['longitude'].", ".$row['latitude'].").transform(map.displayProjection,map.getProjectionObject());";
89 echo "color = 'red';
90 size = '100';
91 size = scaleRadius(size);
92 vectorStyle = getVectorStyle(size, color);
93 point = new OpenLayers.Geometry.Point(lonLat.lon, lonLat.lat);
94 pointVector= new OpenLayers.Feature.Vector(point, null, vectorStyle);
95 points.push(pointVector);";
96 }
97 ?>
98
99
100 layerVectors.addFeatures(points);
101}
102
103
104function generateHeatmap() {
105 var width = layerVectors.renderer.canvas.canvas.width;
106 var height = layerVectors.renderer.canvas.canvas.height;
107
108 var context = layerVectors.renderer.canvas.canvas.getContext('2d');
109 var myImageData = context.getImageData(0, 0, width, height);
110 var pix = myImageData.data;
111
112 for (var i = 0, n = pix.length; i < n; i += 4) {
113 // i+3 is alpha (the fourth element)
114 if (pix[i+3] <= 64) {
115 pix[i+2] = 255; // blue
116 pix[i+1] = (255/64)*pix[i+3]; // green
117 pix[i ] = 0; // red
118 } else if (pix[i+3] <= 128) {
119 pix[i+1] = 255;
120 pix[i+2] = (255/64)*(128-pix[i+3]);
121 pix[i ] = 0;
122 } else if (pix[i+3] <= 192) {
123 pix[i+1] = 255;
124 pix[i+2] = 0;
125 pix[i ] = (255/64)*(pix[i+3]-128);
126 } else if (pix[i+3] <= 255) {
127 pix[i+1] = (255/64)*(255-pix[i+3]);
128 pix[i+2] = 0;
129 pix[i ] = 255;
130 }
131 }
132 context.putImageData(myImageData, 0, 0);
133}
134
135
136function mapMoveEndEvent(event) {
137 if (event.type == 'moveend') {
138 if (lastZoom != map.getZoom()) {
139 lastZoom = map.getZoom();
140 onChangeParams(true);
141 }
142 }
143}
144
145
146function onChangeParams(_drawMarkers) {
147 vectorStyleCache = null;
148 if (_drawMarkers)
149 drawMarkers();
150}
151
152
153function init() {
154 map = new OpenLayers.Map("map", {
155 eventListeners: {
156 "moveend": mapMoveEndEvent
157 },
158 controls: [
159 new OpenLayers.Control.Navigation(),
160 new OpenLayers.Control.PanZoomBar(),
161 new OpenLayers.Control.ScaleLine(),
162 new OpenLayers.Control.LayerSwitcher()
163 ],
164 maxExtent: new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34),
165 maxResolution: 156543.0399,
166 numZoomLevels: 19,
167 units: 'm',
168 projection: new OpenLayers.Projection("EPSG:900913"),
169 displayProjection: new OpenLayers.Projection("EPSG:4326")
170 });
171
172 layerMapnik = new OpenLayers.Layer.OSM.Mapnik("Mapnik");
173 map.addLayer(layerMapnik);
174
175 layerVectors = new OpenLayers.Layer.Vector("Features", {renderers: ["Canvas"]});
176 map.addLayer(layerVectors);
177
178 layerVectors2 = new OpenLayers.Layer.Vector("Features2", {renderers: ["Canvas"]});
179 map.addLayer(layerVectors2);
180
181
182 var lon = 4.4585017;
183 var lat = 52.1406583;
184 var zoom = 16;
185
186 var lonLat = new OpenLayers.LonLat(lon, lat).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());
187 map.setCenter (lonLat, zoom);
188
189 lastZoom = zoom;
190
191}
Note: See TracBrowser for help on using the repository browser.