2 # Create Map overlay of worldwide volcanoes...
3 # Author. John D. Coryat 02/2010...
4 # Copyright 1997-2010 USNaviguide LLC. All rights reserved.
9 use USNaviguide_Google_Tiles ;
12 my $name = shift @ARGV || die "usage: $0 database\n";
14 warn "WORKING on $name\n";
48 my $xiconoff = 12 ; # X Icon offset in pixels (width or lng)
49 my $yiconoff = 12 ; # Y Icon offset in pixels (height or lat)
50 my $xiconpix = 24 ; # Icon width in pixels
51 my $yiconpix = 24 ; # Icon width in pixels
52 my $path = "$name/tiles" ;
53 my $icon1 = 'images/gvp_icon_1.png' ; # Zooms up to 7
54 my $icon2 = 'images/gvp_icon_2.png' ; # Zooms after 7
55 my $dbh = DBI->connect ( "dbi:Pg:dbname=$name" , "" , "" , { AutoCommit => 1 } ) ;
57 # Make sure icon file exists...
59 if ( !(-e $icon1) or !(-e $icon2)) # Icon file missing - bad thing
61 print "Map Icon(s) missing\n" ;
67 # Y,Top,N,S,Lat,Height
68 # X,Left,E,W,Lng,Width
72 eval { $dbh->do("drop table gvp_world_tiles") };
73 $dbh->do("create table gvp_world_tiles (zoom int2,tilex int4,tiley int4,latpix int4,lngpix int4)") ;
75 my $sql = "select (volpnt)[0] as lat, (volpnt)[1] as lng from gvp_world" ;
76 $sql = "select (point)[0] as lat, (point) [1] as lng from geo_count" if $name =~ m/koha/;
77 $sth = $dbh->prepare( $sql );
81 while ( ($lat,$lng) = $sth->fetchrow_array )
85 # Figure out what tiles are needed...
87 for ( $zoom = $minzoom; $zoom <= $maxzoom; $zoom++ )
89 $value = &Google_Tile_Factors($zoom) ; # Calculate Tile Factors
91 ($latpix,$lngpix) = &Google_Coord_to_Pix( $value, $lat, $lng ) ;
94 ($tiley,$tilex) = &Google_Pix_to_Tile( $value, $latpix + $yiconoff, $lngpix + $xiconoff ) ;
95 $tiles{"$tiley $tilex"} = [$tilex, $tiley] ;
97 ($tiley,$tilex) = &Google_Pix_to_Tile( $value, $latpix + $yiconoff, $lngpix - $xiconoff ) ;
98 $tiles{"$tiley $tilex"} = [$tilex, $tiley] ;
100 ($tiley,$tilex) = &Google_Pix_to_Tile( $value, $latpix - $yiconoff, $lngpix + $xiconoff ) ;
101 $tiles{"$tiley $tilex"} = [$tilex, $tiley] ;
103 ($tiley,$tilex) = &Google_Pix_to_Tile( $value, $latpix - $yiconoff, $lngpix - $xiconoff ) ;
104 $tiles{"$tiley $tilex"} = [$tilex, $tiley] ;
106 foreach $x (keys %tiles)
109 $dbh->do("insert into gvp_world_tiles (zoom,tilex,tiley,latpix,lngpix) values ($zoom,$$y[0],$$y[1],$latpix,$lngpix)" ) ;
113 if ( int($count/100)*100 == $count )
115 print "Processed $count points...\n" ;
119 # Make sure there have been records to process before continuing...
124 print "No records to process...\n" ;
128 print "Total Points: $count\n" ;
130 # Remove old images...
132 for ( $zoom = $minzoom; $zoom <= $maxzoom; $zoom++ )
134 warn "clean $path/$zoom\n";
135 rmtree "$path/$zoom";
136 mkpath "$path/$zoom";
138 # Open up map icon files as images...
140 $imicon1 = GD::Image->newFromPng( $icon1 ) ;
141 $imicon2 = GD::Image->newFromPng( $icon2 ) ;
145 $dbh->do("create index gvp_world_tiles_main on gvp_world_tiles (zoom,tilex,tiley)") ;
146 $dbh->do("analyze gvp_world_tiles") ;
148 # Create tiles by zoom...
150 $sth = $dbh->prepare("select distinct zoom,tilex,tiley from gvp_world_tiles") ;
156 while ( ($zoom,$tilex,$tiley) = $sth->fetchrow_array )
160 # Calculate tile fields...
162 $file = $path . '/' . $zoom . '/v_' . $tilex . '_' . $tiley . '.png' ;
164 ($top,$left) = &Google_Tile_to_Pix( $value, $tiley, $tilex ) ;
168 $im = new GD::Image(256,256,0) ;
170 $white = $im->colorAllocate(255,255,255) ;
172 $im->interlaced('true') ;
174 $im->transparent($white) ;
176 $im->setThickness(1) ;
178 # Calculate which icon to use based on zoom...
188 my $custom_icon = "$name/icons/$zoom.png";
189 $imicon = GD::Image->newFromPng( $custom_icon ) if -e $custom_icon;
191 $sti = $dbh->prepare("select latpix,lngpix from gvp_world_tiles where zoom = $zoom and tilex = $tilex and tiley = $tiley") ;
195 while ( ($latpix,$lngpix) = $sti->fetchrow_array )
197 $ix = $lngpix - $left - $xiconoff ; # Remove half image size
198 $iy = $latpix - $top - $yiconoff ; # Remove half image size
199 $im->copy($imicon,$ix,$iy,0,0,$xiconpix,$yiconpix) ;
202 open(my $PNG, '>', $file) || die "$file: $!";
203 print $PNG $im->png ;
205 # chmod(0444, $file) ;
206 if ( int($count/100)*100 == $count )
208 print "Processed $count tiles...\n" ;
211 print "Processed $count total tiles...\n" ;
213 #$dbh->do("drop table gvp_world_tiles") ;
215 # allow web server to select data
216 $dbh->do("grant select on gvp_world to public") ;