Much more optimized bubble tree
[contractdashboard.git] / displayHeatmap.php
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
<?php
 
 
include('lib/common.inc.php');
 
$year = 2006;
$ZeroX = 112.5;
$MaxX = 157;
$ZeroY = -9;
$MaxY = -45;
 
$XRange = $MaxX - $ZeroX;
$YRange = abs($MaxY) - abs($ZeroY);
$xdivs = 250;
$xdivlength = $XRange / $xdivs;
$ydivs = 250;
$ydivheight = $YRange / $ydivs;
 
$borderLeft = 100;
$borderTop = 35;
$width = 497;
$height = 457;
//echo "http://dev.openstreetmap.org/~pafciu17/?module=map&bbox=".$ZeroX.",".$ZeroY.",".$MaxX.",".$MaxY."&width=".$width."&height=".$height;
//$handle = ImageCreate ($width, $height) or die ("Cannot Create image");
$handle = imagecreatefrompng('images/australia.png');
$white = imagecolorallocate($handle, 0, 0, 0);
imagecolortransparent($handle, $white);
 
$query = 'SELECT "supplierPostcode", sum("value") as value, max(lat) as lat, max(lon) as lon FROM contractnotice inner join postcodes on "supplierPostcode" = postcode::text where "childCN" = 0 AND "supplierCountry" = \'Australia\' GROUP BY "supplierPostcode"';
$query = $conn->prepare($query);
   $query->execute();
   if (!$query) {
      databaseError($conn->errorInfo());
   }
 
 
$left = "FFFF50";
$right= "EF0050";
$leftR = hexdec(substr($left,0,2));
$leftG = hexdec(substr($left,2,2));
$leftB = hexdec(substr($left,4,2));
$rightR = hexdec(substr($right,0,2));
$rightG = hexdec(substr($right,2,2));
$rightB = hexdec(substr($right,4,2));
for($i=0;$i<250;$i++) {
    $colorset[$i] = imagecolorallocatealpha($handle, $leftR + ($i*(($rightR-$leftR)/250)), $leftG + ($i*(($rightG-$leftG)/250)), $leftB + ($i*(($rightB-$leftB)/250)),117 - ($i/250)*40);
}
 
  foreach ($query->fetchAll() as $row) {
   $xpage = round((($XRange - ($MaxX - $row['lon'])) / $xdivlength));
        if ($row['lat'] < -40.25) $row['lat']-= 0.75;
   $ypage = round(($YRange - (abs($MaxY) - abs($row['lat']))) / $ydivheight);
   @$pagevalues[$xpage][$ypage] += $row['value'];
   //echo $ypage." ".$xpage."<br>";
}
 
$max = 0;
$min = 0;
for ($i = 0;$i < $xdivs; $i++ ) {
   for ($j = 0;$j < $ydivs; $j++ ) {
   if (@$pagevalues[$i][$j])
   {
      if (@$pagevalues[$i][$j] > $max) $max = @$pagevalues[$i][$j];
      if ($min == 0 || @$pagevalues[$i][$j] < $min) $min = @$pagevalues[$i][$j];
   } else {
      @$pagevalues[$i][$j] = ((@$pagevalues[$i][$j] + (@$pagevalues[$i+1][$j] + @$pagevalues[$i][$j+1]
                           + @$pagevalues[$i-1][$j] + @$pagevalues[$i][$j-1])/4) /2)-9;
      @$pagevalues[$i-1][$j] = ((@$pagevalues[$i-1][$j] + (@$pagevalues[$i][$j] + @$pagevalues[$i-1][$j+1]
                           + @$pagevalues[$i-2][$j] + @$pagevalues[$i-1][$j-1])/4) /2)-9;
      @$pagevalues[$i][$j-1] = ((@$pagevalues[$i][$j-1] + (@$pagevalues[$i+1][$j-1] + @$pagevalues[$i][$j]
                           + @$pagevalues[$i-1][$j-1] + @$pagevalues[$i][$j-2])/4) /2)-9;
   } 
   }
}
//echo $max." ".$min;
//echo "<table width='100%'>";
for ($i = 0;$i < $xdivs; $i++ ) {
//   echo "<tr>";
   for ($j = 0; $j < $ydivs; $j++ ) {
//   echo ("<td>". log10(@$pagevalues[$i][$j])/log10($max) ."</td>");
   if ((@$pagevalues[$i][$j]) > $min) {
      $x = $i*($width/$xdivs);
      $x1 = $borderLeft + $x -($width/$xdivs);
      $x2 = $borderLeft + $x +($width/$xdivs);
      $y = $j*($height/$ydivs);
      $y1 = $borderTop + $y - ($height/$ydivs);
      $y2 = $borderTop + $y + ($height/$ydivs);
      imagefilledrectangle ($handle, $x1, $y1, $x2, $y2, $colorset[(int)((log10(@$pagevalues[$i][$j])/log10($max))*249)]);
   }
   }
//echo "</tr>";
}
//echo "</table>"
 
header ("Content-type: image/png");
ImagePng ($handle); 
 
 
?>