use city_koha to support different points in same city
[google-map-tiles.git] / koha.pl
1 #!/usr/bin/perl
2 # Deliver click values to volcano.htm...
3 # Author. John D. Coryat 02/2010...
4 # Copyright 2010 USNaviguide LLC. All rights reserved...
5
6 use CGI qw/:standard *table/;
7 use strict ;
8 use DBI ;
9 use USNaviguide_Google_Tiles ;
10
11 my $dbh         = DBI->connect ( "dbi:Pg:dbname=koha" , "dpavlin" , "" , { AutoCommit => 1 } ) ;
12 my $point       = param('POINT') ;
13 my $zoom        = param('ZOOM') ;
14
15 my $maxpix      = 15 ;                                  # Maximum pixels between click and point
16
17 my $limit_books = 100; # max. for one click
18
19 print qq{Content-type: text/xml\r\n\r\n};
20 print qq{<?xml version="1.0" encoding="UTF-8"?>\n} ;
21 print qq!<map>\n! ;
22
23 if ( !$point )
24 {
25  print qq!<info error="No point passed..."/>\n! ;
26  print qq!</map>\n! ;
27  $dbh->disconnect ;
28  exit ;
29 }
30
31 if ( $point =~ /(.*),(.*)/ )
32 {
33  my $lat        = $1 ;
34  my $lng        = $2 ;
35
36  my $value = &Google_Tile_Factors($zoom) ;                                      # Calculate Tile Factors
37
38  my ($latpix,$lngpix) = &Google_Coord_to_Pix( $value, $lat, $lng ) ;    # Convert coordinate to pixel location
39  
40  my $sql = qq{
41 select
42         city_koha
43         ,country
44         ,count
45         ,point
46         ,point'($lat,$lng)' <-> point as distance
47 from geo_count
48 order by distance
49 limit 1
50  };
51
52  if ( my ($city,$country,$count,$volpnt,$i) = $dbh->selectrow_array($sql) )     # Got one
53  {
54   $volpnt =~ /\((.*),(.*)\)/ ; 
55   my $vlat = $1 ;
56   my $vlng = $2 ;
57
58   my ($vlatpix,$vlngpix) = &Google_Coord_to_Pix( $value, $vlat, $vlng ) ;       # Convert coordinate to pixel location
59
60   if ( sqrt(($vlatpix - $latpix)**2  + ($vlngpix - $lngpix)**2) > $maxpix )
61   {
62    # Click not within maxpix of point...
63    print qq!<info error="No publisher is within range of your click, please try again."/>\n! ;
64   } else                                                                # Good point found
65   {
66
67         my $sth = $dbh->prepare(qq{
68 select
69         author, title, max(bi.biblionumber), count(title)
70 from geo_city c
71 join geo_biblioitems bi on bi.city = c.city_koha
72 join biblio b on b.biblionumber = bi.biblionumber
73 where c.city_koha = ? and country = ?
74 group by author, title
75 order by min(timestamp)
76 limit $limit_books
77         });
78         $sth->execute( $city, $country );
79
80         my $rows = $sth->rows;
81         if ( $rows == $limit_books ) {
82                 $rows = "more than $rows";
83                 $rows = $count if $count > $rows;
84         };
85         my $books = 'books';
86         $books = 'book' if $rows == 1;
87
88         my $descript = "<b>$city</b> <em>$country</em> <small>$rows $books</small>\n<ol>";
89
90         while ( my $row = $sth->fetchrow_hashref ) {
91                 $descript .= sprintf qq|<li><a target="koha" href="http://koha.ffzg.hr/cgi-bin/koha/opac-detail.pl?biblionumber=%d">%s</a> %s\n|,
92                         $row->{biblionumber}, $row->{title}, $row->{author}
93                 ;
94         }
95
96         $descript .= "\n</ol>\n";
97
98    print qq!<info error=""  name="${city}_${country}" lat="$vlat" lng="$vlng">\n! ;
99    print qq! <description><\![CDATA[$descript]]></description>\n! ;
100    print qq!</info>\n! ;
101   }
102  }
103 } else
104 {
105  print qq!<info error="No valid point ($point) passed. Should be: (lat,lng) format."/>\n! ;
106 }
107 $dbh->disconnect ;
108
109 print "</map>\n\n" ;
110