Newer
Older
Digital_Repository / Repositories / Maps / google_earth_generate_kml.pl
nstanger on 26 Oct 2006 3 KB - Changed icon and fixed colours.
  1. #!/usr/bin/env perl
  2. use strict;
  3. # use CGI;
  4. use DBI;
  5. use Geo::IP;
  6.  
  7. # my ($page);
  8.  
  9. # Database connection.
  10. my ($dsn) = "DBI:mysql:database=eprintstats;host=localhost";
  11. my ($user_name) = "eprintstatspriv";
  12. my ($password) = "AuldGrizzel";
  13. my ( $connect, $query, %types, %unmapped, $stat, $row, $num_rows, $vtype );
  14.  
  15. # Geolocation database.
  16. my ($gi);
  17. my ($gidb) = '/usr/local/share/GeoIP/GeoLiteCity.dat';
  18.  
  19. # Miscellaneous variable.
  20. my ( %cities, %IPs );
  21. my ($num_entries) = -1;
  22. my ($num_hits) = 0;
  23. my ( $ip, $count, $location );
  24. my ( $lat, $long, $city, $key ) = ( 0, 0, '', '' );
  25.  
  26. # $page = new CGI;
  27. # print $page->header( -type => "text/xml", -Pragma => 'no-cache' );
  28. $num_entries = -1;#$page->param('top');
  29.  
  30. $gi = Geo::IP->open( $gidb, GEOIP_STANDARD )
  31. or die "Unable to open GeoIP database $gidb\n";
  32.  
  33. $connect = DBI->connect( $dsn, $user_name, $password, { RaiseError => 1 } );
  34.  
  35. $types{'download'} = $types{'abstract'} = 0;
  36. $unmapped{'download'} = $unmapped{'abstract'} = 0;
  37. $query = "SELECT ip, view_type, COUNT(*) AS count
  38. FROM view
  39. GROUP BY ip, view_type
  40. ORDER BY count DESC" . ( ( $num_entries > 0 ) ? " LIMIT $num_entries" : '' );
  41.  
  42. $stat = $connect->prepare($query);
  43. $stat->execute();
  44. $num_rows = $stat->rows;
  45.  
  46. if ( $num_rows > 0 )
  47. {
  48. $num_entries = $num_rows if ( $num_entries < 1 );
  49.  
  50. while ( $row = $stat->fetchrow_hashref() )
  51. {
  52. $ip = $row->{'ip'};
  53. $count = $row->{'count'};
  54. $vtype = $row->{'view_type'};
  55.  
  56. $IPs{$ip} = 1;
  57.  
  58. $location = $gi->record_by_addr($ip);
  59.  
  60. if ( defined($location) )
  61. {
  62. $lat = $location->latitude;
  63. $long = $location->longitude;
  64. $city = (
  65. ( $location->city eq '' )
  66. ? 'Unknown'
  67. : $location->city
  68. );
  69. $key = $city . " ($lat, $long)";
  70.  
  71. if ( !defined( $cities{$key} ) )
  72. {
  73. $cities{$key}{'name'} = $city;
  74. $cities{$key}{'lat'} = $lat;
  75. $cities{$key}{'long'} = $long;
  76. $cities{$key}{'abstract'} = 0;
  77. $cities{$key}{'download'} = 0;
  78. }
  79. $cities{$key}{$vtype} += $count;
  80. $types{$vtype} += $count;
  81. }
  82. else
  83. {
  84. $unmapped{$vtype} += $count;
  85. }
  86. }
  87.  
  88. # Need to wait until we have all the counts before writing the data out.
  89. print '<?xml version="1.0"?>' . "\n";
  90. print '<kml xmlns="http://earth.google.com/kml/2.0">' . "\n";
  91. print "<Document>\n<description>lotsa points</description>\n<name>Hits</name>\n";
  92.  
  93. foreach $city ( keys %cities )
  94. {
  95. my ($maxcolor) = $cities{$city}{'download'} + $cities{$city}{'abstract'};
  96. my ($red) = round($cities{$city}{'download'} / $maxcolor * 255);
  97. my ($blue) = round($cities{$city}{'abstract'} / $maxcolor * 255);
  98. print '<Placemark>
  99. <description><![CDATA[<span style="color:red;">'
  100. . $cities{$city}{'download'}
  101. . ' downloads</span>, <span style="color:blue;">'
  102. . $cities{$city}{'abstract'}
  103. . ' abstracts</span>]]>
  104. </description>
  105. <name>'
  106. . $cities{$city}{'name'}
  107. . '</name>
  108. <Style>
  109. <IconStyle>
  110. <color>'
  111. . sprintf( 'c0%02x00%02x', $blue, $red )
  112. . '</color>
  113. <Icon>
  114. <href>root://icons/palette-4.png</href>
  115. <x>96</x>
  116. <y>128</y>
  117. <w>32</w>
  118. <h>32</h>
  119. </Icon>
  120. </IconStyle>
  121. </Style>
  122. <LookAt>
  123. <longitude>'
  124. . $cities{$city}{'long'}
  125. . '</longitude>
  126. <latitude>'
  127. . $cities{$city}{'lat'}
  128. . '</latitude>
  129. <range>540.68</range>
  130. <tilt>0</tilt>
  131. <heading>3</heading>
  132. </LookAt>
  133. <Point>
  134. <coordinates>'
  135. . $cities{$city}{'long'} . ','
  136. . $cities{$city}{'lat'}
  137. . ',0</coordinates>
  138. </Point>
  139. </Placemark>' . "\n";
  140. }
  141.  
  142. print "</Document>\n</kml>\n";
  143. }
  144.  
  145. $stat->finish();
  146. $connect->disconnect();
  147.  
  148. sub round
  149. {
  150. my ($n) = shift;
  151. return int( $n + 0.5 * ( $n <=> 0 ) );
  152. }