extract data directly from database for huge speedup
[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 $lat         = 0 ;
16 my $lng         = 0 ;
17 my $latpix      = 0 ;
18 my $lngpix      = 0 ;
19
20 my $volpnt      = '' ;
21 my $vlat        = 0 ;
22 my $vlng        = 0 ;
23 my $vlatpix     = 0 ;
24 my $vlngpix     = 0 ;
25
26 my $value       = '' ;
27 my $maxpix      = 15 ;                                  # Maximum pixels between click and point
28 my $name        = '' ;
29 my $descript    = '' ;
30 my $x           = '' ;
31 my $i           = 0 ;
32
33 print qq{Content-type: text/xml\r\n\r\n};
34 print qq{<?xml version="1.0" encoding="UTF-8"?>\n} ;
35 print qq!<map>\n! ;
36
37 if ( !$point )
38 {
39  print qq!<info error="No point passed..."/>\n! ;
40  print qq!</map>\n! ;
41  $dbh->disconnect ;
42  exit ;
43 }
44
45 if ( $point =~ /(.*),(.*)/ )
46 {
47  $lat   = $1 ;
48  $lng   = $2 ;
49
50  $value = &Google_Tile_Factors($zoom) ;                                 # Calculate Tile Factors
51
52  ($latpix,$lngpix) = &Google_Coord_to_Pix( $value, $lat, $lng ) ;       # Convert coordinate to pixel location
53  
54  $x = "select city,count,point,point'($lat,$lng)' <-> point as distance from geo_count order by distance limit 1" ;
55
56  if ( ($name,$descript,$volpnt,$i) = $dbh->selectrow_array($x) )        # Got one
57  {
58   $volpnt =~ /\((.*),(.*)\)/ ; 
59   $vlat = $1 ;
60   $vlng = $2 ;
61
62   ($vlatpix,$vlngpix) = &Google_Coord_to_Pix( $value, $vlat, $vlng ) ;  # Convert coordinate to pixel location
63
64   if ( sqrt(($vlatpix - $latpix)**2  + ($vlngpix - $lngpix)**2) > $maxpix )
65   {
66    # Click not within maxpix of point...
67    print qq!<info error="No publisher is within range of your click, please try again."/>\n! ;
68   } else                                                                # Good point found
69   {
70
71         my $sth = $dbh->prepare(qq{
72 select
73         author, title, bi.biblionumber
74 from geo_city c
75 join geo_biblioitems bi on bi.city = c.city_koha
76 join biblio b on b.biblionumber = bi.biblionumber
77 where c.city = ?
78 order by timestamp
79 limit 100
80         });
81         $sth->execute( $name );
82
83         $descript = "<b>$name</b> $descript items\n<ol>";
84
85         while ( my $row = $sth->fetchrow_hashref ) {
86                 $descript .= sprintf qq|<li><a target="koha" href="http://koha.ffzg.hr/cgi-bin/koha/opac-detail.pl?biblionumber=%d">%s</a> %s\n|,
87                         $row->{biblionumber}, $row->{title}, $row->{author}
88                 ;
89         }
90
91         $descript .= "\n</ol>\n";
92
93    print qq!<info error = "" name = "$name" lat="$vlat" lng="$vlng">\n! ;
94    print qq! <description><\![CDATA[$descript]]></description>\n! ;
95    print qq!</info>\n! ;
96   }
97  }
98 } else
99 {
100  print qq!<info error="No valid point ($point) passed. Should be: (lat,lng) format."/>\n! ;
101 }
102 $dbh->disconnect ;
103
104 print "</map>\n\n" ;
105