Newer
Older
Digital_Repository / Repositories / Maps / coordinates.php
nstanger on 8 May 2006 2 KB - Added various test files.
<?php

$start_time = time();

include('geoipcity.inc');

$gi = geoip_open('/usr/local/share/GeoIP/GeoLiteCity.dat',GEOIP_STANDARD);

$proj_pipe = '|/sw/bin/proj +proj=robin +ellps=sphere +lon_0=10e -';
$x_offset = 16986796.16;
$y_offset = 8615499.05;
$max_x = $x_offset * 2;
$max_y = $y_offset * 2;

$connect = mysql_pconnect ('localhost','eprintstatspriv','AuldGrizzel');
$db = mysql_select_db('eprintstats',$connect) or die("Could not connect");

$num_IPs = 3875;
$num_hits = 0;

$query =
	"SELECT ip, COUNT(*) AS count
	 FROM view
	 WHERE (view_type = 'download')
	 GROUP BY ip
	 ORDER BY count DESC" .
	 ($num_IPs > 0 ? " LIMIT $num_IPs" : '');
$result = mysql_query($query,$connect);

if (mysql_num_rows($result) > 0)
{
	if ($num_IPs < 1) $num_IPs = mysql_num_rows($result);
	
	while ($row = mysql_fetch_assoc($result))
	{
		$ip = $row["ip"];
		$count = $row["count"];
		
		$location = GeoIP_record_by_addr($gi, $ip);
		
		if (isset($location->latitude))
		{
			$lat = $location->latitude;
			$long = $location->longitude;
			$city = $location->city;
			
			if ($city == '') $city = 'Unknown';

			$cities[$city]['lat'] = $lat;
			$cities[$city]['long'] = $long;
			if (!isset($cities[$city])) $cities[$city]['count'] = 0;
			$cities[$city]['count'] += $count;
			$num_hits += $count;
			
			list($x, $y) = explode("\t", exec("echo '$long\t$lat'" . $proj_pipe));
			$cities[$city]['x'] = round(($x + $x_offset) / $max_x * 900, 0);
			$cities[$city]['y'] = round(($y_offset - $y) / $max_y * 457, 0);
		}
	}

	$mapimage = imagecreatefrompng('map.png');
	if ($mapimage) /* See if it failed */
	{
		foreach ($cities as $city => $loc)
		{
			$col_ellipse = imagecolorallocate($mapimage, 255, 0, 0);
			imagefilledrectangle($mapimage, $loc['x'] - 1, $loc['y'] - 1, $loc['x'] + 1, $loc['y'] + 1, $col_ellipse);
		}
	}
	else
	{
		$mapimageÊ = imagecreatetruecolor(150, 30); /* Create a blank image */
		$bgc = imagecolorallocate($mapimage, 255, 255, 255);
		$tcÊ = imagecolorallocate($mapimage, 0, 0, 0);
		imagefilledrectangle($mapimage, 0, 0, 150, 30, $bgc);
		/* Output an errmsg */
		imagestring($mapimage, 1, 5, 5, "Error loading $imgname", $tc);
	}

	$tcÊ = imagecolorallocate($mapimage, 0, 0, 0);
	imagestring($mapimage, 1, 3, 3, "$num_hits downloads from " . (count($cities)) . ' cities', $tc);
	imagestring($mapimage, 1, 3, 15, "($num_IPs IP addresses)", $tc);
	imagestring($mapimage, 1, 3, 447, 'Elapsed time ' . (time() - $start_time) . ' seconds', $tc);
	
	header("Content-type: image/jpeg");
	
	imagejpeg($mapimage);
}

?>