Newer
Older
Digital_Repository / Repositories / Maps / coordinates.php
nstanger on 8 May 2006 2 KB - Added various test files.
  1. <?php
  2.  
  3. $start_time = time();
  4.  
  5. include('geoipcity.inc');
  6.  
  7. $gi = geoip_open('/usr/local/share/GeoIP/GeoLiteCity.dat',GEOIP_STANDARD);
  8.  
  9. $proj_pipe = '|/sw/bin/proj +proj=robin +ellps=sphere +lon_0=10e -';
  10. $x_offset = 16986796.16;
  11. $y_offset = 8615499.05;
  12. $max_x = $x_offset * 2;
  13. $max_y = $y_offset * 2;
  14.  
  15. $connect = mysql_pconnect ('localhost','eprintstatspriv','AuldGrizzel');
  16. $db = mysql_select_db('eprintstats',$connect) or die("Could not connect");
  17.  
  18. $num_IPs = 3875;
  19. $num_hits = 0;
  20.  
  21. $query =
  22. "SELECT ip, COUNT(*) AS count
  23. FROM view
  24. WHERE (view_type = 'download')
  25. GROUP BY ip
  26. ORDER BY count DESC" .
  27. ($num_IPs > 0 ? " LIMIT $num_IPs" : '');
  28. $result = mysql_query($query,$connect);
  29.  
  30. if (mysql_num_rows($result) > 0)
  31. {
  32. if ($num_IPs < 1) $num_IPs = mysql_num_rows($result);
  33. while ($row = mysql_fetch_assoc($result))
  34. {
  35. $ip = $row["ip"];
  36. $count = $row["count"];
  37. $location = GeoIP_record_by_addr($gi, $ip);
  38. if (isset($location->latitude))
  39. {
  40. $lat = $location->latitude;
  41. $long = $location->longitude;
  42. $city = $location->city;
  43. if ($city == '') $city = 'Unknown';
  44.  
  45. $cities[$city]['lat'] = $lat;
  46. $cities[$city]['long'] = $long;
  47. if (!isset($cities[$city])) $cities[$city]['count'] = 0;
  48. $cities[$city]['count'] += $count;
  49. $num_hits += $count;
  50. list($x, $y) = explode("\t", exec("echo '$long\t$lat'" . $proj_pipe));
  51. $cities[$city]['x'] = round(($x + $x_offset) / $max_x * 900, 0);
  52. $cities[$city]['y'] = round(($y_offset - $y) / $max_y * 457, 0);
  53. }
  54. }
  55.  
  56. $mapimage = imagecreatefrompng('map.png');
  57. if ($mapimage) /* See if it failed */
  58. {
  59. foreach ($cities as $city => $loc)
  60. {
  61. $col_ellipse = imagecolorallocate($mapimage, 255, 0, 0);
  62. imagefilledrectangle($mapimage, $loc['x'] - 1, $loc['y'] - 1, $loc['x'] + 1, $loc['y'] + 1, $col_ellipse);
  63. }
  64. }
  65. else
  66. {
  67. $mapimageÊ = imagecreatetruecolor(150, 30); /* Create a blank image */
  68. $bgc = imagecolorallocate($mapimage, 255, 255, 255);
  69. $tcÊ = imagecolorallocate($mapimage, 0, 0, 0);
  70. imagefilledrectangle($mapimage, 0, 0, 150, 30, $bgc);
  71. /* Output an errmsg */
  72. imagestring($mapimage, 1, 5, 5, "Error loading $imgname", $tc);
  73. }
  74.  
  75. $tcÊ = imagecolorallocate($mapimage, 0, 0, 0);
  76. imagestring($mapimage, 1, 3, 3, "$num_hits downloads from " . (count($cities)) . ' cities', $tc);
  77. imagestring($mapimage, 1, 3, 15, "($num_IPs IP addresses)", $tc);
  78. imagestring($mapimage, 1, 3, 447, 'Elapsed time ' . (time() - $start_time) . ' seconds', $tc);
  79. header("Content-type: image/jpeg");
  80. imagejpeg($mapimage);
  81. }
  82.  
  83. ?>