fix count from query if needed
[google-map-tiles.git] / tiles.pl
index 0f2b60c..31f8fec 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 ;
@@ -44,10 +49,10 @@ 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...
 
@@ -64,10 +69,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 ;
 
@@ -124,7 +131,9 @@ 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...
 
@@ -150,7 +159,7 @@ while ( ($zoom,$tilex,$tiley) = $sth->fetchrow_array )
 
  # 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 ) ;
 
@@ -176,6 +185,9 @@ while ( ($zoom,$tilex,$tiley) = $sth->fetchrow_array )
   $imicon = $imicon1 ;
  }
 
+ my $custom_icon = "$name/icons/$zoom.png";
+ $imicon = GD::Image->newFromPng( $custom_icon ) if -e $custom_icon;
+
  $sti = $dbh->prepare("select latpix,lngpix from gvp_world_tiles where zoom = $zoom and tilex = $tilex and tiley = $tiley") ;
 
  $sti->execute ;
@@ -187,10 +199,10 @@ while ( ($zoom,$tilex,$tiley) = $sth->fetchrow_array )
   $im->copy($imicon,$ix,$iy,0,0,$xiconpix,$yiconpix) ;
  }
 
- 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 ;