use Directory instead of Location to allow vhost overlay
[google-map-tiles.git] / tiles.pl
index 0f2b60c..4a11d75 100755 (executable)
--- a/tiles.pl
+++ b/tiles.pl
@@ -7,6 +7,11 @@ use DBI ;
 use strict ;
 use GD ;
 use USNaviguide_Google_Tiles ;
+use File::Path;
+
+my $name = shift @ARGV || die "usage: $0 database\n";
+
+warn "WORKING on $name\n";
 
 my $zoom       = 0 ;
 my $lat                = 0 ;
@@ -14,8 +19,6 @@ my $lng               = 0 ;
 my $latpix     = 0 ;
 my $lngpix     = 0 ;
 my %tiles      = ( ) ;
-my $tilex      = 0 ;
-my $tiley      = 0 ;
 my $nacount    = 0 ;
 my %tile       = ( ) ;
 my $top                = 0 ;
@@ -40,14 +43,10 @@ my $imicon1 = '' ;
 my $imicon2    = '' ;
 my $maxzoom    = 10 ;
 my $minzoom    = 2 ;
-my $xiconoff   = 12 ;                                  # X Icon offset in pixels (width or lng)
-my $yiconoff   = 12 ;                                  # Y Icon offset in pixels (height or lat)
-my $xiconpix   = 24 ;                                  # Icon width in pixels
-my $yiconpix   = 24 ;                                  # Icon width in pixels
-my $path       = 'tiles/' ;
+my $path       = "$name/tiles" ;
 my $icon1      = 'images/gvp_icon_1.png' ;             # Zooms up to 7
 my $icon2      = 'images/gvp_icon_2.png' ;             # Zooms after 7
-my $dbh        = DBI->connect ( "dbi:Pg:dbname=volcano" , "" , "" , { AutoCommit => 1 } ) ;
+my $dbh        = DBI->connect ( "dbi:Pg:dbname=$name" , "" , "" , { AutoCommit => 1 } ) ;
 
 # Make sure icon file exists...
 
@@ -57,6 +56,23 @@ if ( !(-e $icon1) or !(-e $icon2))                                   # Icon file missing - bad thing
  exit ;
 }
 
+sub get_icon {
+       my $zoom = shift;
+       $imicon = GD::Image->newFromPng( $zoom > 7 ? $icon2 : $icon1 ) ;
+        # Calculate which icon to use based on zoom...
+
+       my $custom_icon = "$name/icons/$zoom.png";
+       $imicon = GD::Image->newFromPng( $custom_icon ) if -e $custom_icon;
+
+       my $xiconpix = $imicon->width;
+       my $yiconpix = $imicon->height;
+
+       # FIXME make click position configurable
+       my $xiconoff = $xiconpix / 2;
+       my $yiconoff = $yiconpix / 2;
+
+       return ( $xiconpix, $yiconpix, $xiconoff, $yiconoff );
+}
 
 # Relations: 
 # Y,Top,N,S,Lat,Height
@@ -64,10 +80,12 @@ if ( !(-e $icon1) or !(-e $icon2))                                  # Icon file missing - bad thing
 
 # (35,-89),(34,-90)
 
-$dbh->do("drop table gvp_world_tiles") ;
+eval { $dbh->do("drop table gvp_world_tiles") };
 $dbh->do("create table gvp_world_tiles (zoom int2,tilex int4,tiley int4,latpix int4,lngpix int4)") ;
 
-$sth = $dbh->prepare("select (volpnt)[0] as lat, (volpnt)[1] as lng from gvp_world") ;
+my $sql = "select (volpnt)[0] as lat, (volpnt)[1] as lng from gvp_world" ;
+$sql    = "select (point)[0]  as lat, (point) [1] as lng from geo_count" if $name =~ m/koha/;
+$sth = $dbh->prepare( $sql );
 
 $sth->execute ;
 
@@ -77,14 +95,16 @@ while ( ($lat,$lng) = $sth->fetchrow_array )
 
  # Figure out what tiles are needed...
 
- for ( $zoom = $minzoom; $zoom <= $maxzoom; $zoom++ )
+ for ( my $zoom = $minzoom; $zoom <= $maxzoom; $zoom++ )
  {
   $value = &Google_Tile_Factors($zoom) ;               # Calculate Tile Factors
 
   ($latpix,$lngpix) = &Google_Coord_to_Pix( $value, $lat, $lng ) ;
   %tiles = ( ) ;
 
-  ($tiley,$tilex) = &Google_Pix_to_Tile( $value, $latpix + $yiconoff, $lngpix + $xiconoff ) ;
+  my ( $xiconpix, $yiconpix, $xiconoff, $yiconoff ) = get_icon $zoom;
+
+  my ($tiley,$tilex) = &Google_Pix_to_Tile( $value, $latpix + $yiconoff, $lngpix + $xiconoff ) ;
   $tiles{"$tiley $tilex"} = [$tilex, $tiley] ;
 
   ($tiley,$tilex) = &Google_Pix_to_Tile( $value, $latpix + $yiconoff, $lngpix - $xiconoff ) ;
@@ -124,13 +144,12 @@ print "Total Points: $count\n" ;
 
 for ( $zoom = $minzoom; $zoom <= $maxzoom; $zoom++ )
 {
- system("rm tiles/$zoom/*") ;
+ warn "clean $path/$zoom\n";
+ rmtree "$path/$zoom";
+ mkpath "$path/$zoom";
 }
 # Open up map icon files as images...
 
-$imicon1 = GD::Image->newFromPng( $icon1 ) ;
-$imicon2 = GD::Image->newFromPng( $icon2 ) ;
-
 # Create index...
 
 $dbh->do("create index gvp_world_tiles_main on gvp_world_tiles (zoom,tilex,tiley)") ;
@@ -144,13 +163,13 @@ $sth->execute ;
 
 $count = 0 ;
 
-while ( ($zoom,$tilex,$tiley) = $sth->fetchrow_array )
+while ( my ($zoom,$tilex,$tiley) = $sth->fetchrow_array )
 {
  $count++ ;
 
  # Calculate tile fields...
 
- $file = $path . $zoom . '/v_' . $tilex . '_' . $tiley . '.png' ;
+ $file = $path . '/' . $zoom . '/v_' . $tilex . '_' . $tiley . '.png' ;
 
  ($top,$left) = &Google_Tile_to_Pix( $value, $tiley, $tilex ) ;
 
@@ -166,15 +185,7 @@ while ( ($zoom,$tilex,$tiley) = $sth->fetchrow_array )
 
  $im->setThickness(1) ;
 
- # Calculate which icon to use based on zoom...
-
- if ( $zoom > 7 )
- {
-  $imicon = $imicon2 ;
- } else
- {
-  $imicon = $imicon1 ;
- }
+ my ( $xiconpix, $yiconpix, $xiconoff, $yiconoff ) = get_icon $zoom;
 
  $sti = $dbh->prepare("select latpix,lngpix from gvp_world_tiles where zoom = $zoom and tilex = $tilex and tiley = $tiley") ;
 
@@ -184,13 +195,14 @@ while ( ($zoom,$tilex,$tiley) = $sth->fetchrow_array )
  {
   $ix = $lngpix - $left - $xiconoff ;                  # Remove half image size
   $iy = $latpix - $top - $yiconoff ;                   # Remove half image size
-  $im->copy($imicon,$ix,$iy,0,0,$xiconpix,$yiconpix) ;
+#  $im->copy($imicon,$ix,$iy,0,0,$xiconpix,$yiconpix) ;
+  $im->copyMerge($imicon,$ix,$iy,0,0,$xiconpix,$yiconpix,50) ;
  }
 
- open PNG, ">$file" ;
- print PNG $im->png ;
- close PNG ;
- chmod(0444, $file) ;
+ open(my $PNG, '>', $file) || die "$file: $!";
+ print $PNG $im->png ;
+ close $PNG ;
+# chmod(0444, $file) ;
  if ( int($count/100)*100 == $count )
  {
   print "Processed $count tiles...\n" ;
@@ -198,7 +210,10 @@ while ( ($zoom,$tilex,$tiley) = $sth->fetchrow_array )
 }
 print "Processed $count total tiles...\n" ;
 
-$dbh->do("drop table gvp_world_tiles") ;
+#$dbh->do("drop table gvp_world_tiles") ;
+
+# allow web server to select data
+$dbh->do("grant select on gvp_world to public") ;
 
 $dbh->disconnect ;