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 $sth = $dbh->prepare("select (volpnt)[0] as lat, (volpnt)[1] as lng from gvp_world") ;
79 while ( ($lat,$lng) = $sth->fetchrow_array )
83 # Figure out what tiles are needed...
85 for ( $zoom = $minzoom; $zoom <= $maxzoom; $zoom++ )
87 $value = &Google_Tile_Factors($zoom) ; # Calculate Tile Factors
89 ($latpix,$lngpix) = &Google_Coord_to_Pix( $value, $lat, $lng ) ;
92 ($tiley,$tilex) = &Google_Pix_to_Tile( $value, $latpix + $yiconoff, $lngpix + $xiconoff ) ;
93 $tiles{"$tiley $tilex"} = [$tilex, $tiley] ;
95 ($tiley,$tilex) = &Google_Pix_to_Tile( $value, $latpix + $yiconoff, $lngpix - $xiconoff ) ;
96 $tiles{"$tiley $tilex"} = [$tilex, $tiley] ;
98 ($tiley,$tilex) = &Google_Pix_to_Tile( $value, $latpix - $yiconoff, $lngpix + $xiconoff ) ;
99 $tiles{"$tiley $tilex"} = [$tilex, $tiley] ;
101 ($tiley,$tilex) = &Google_Pix_to_Tile( $value, $latpix - $yiconoff, $lngpix - $xiconoff ) ;
102 $tiles{"$tiley $tilex"} = [$tilex, $tiley] ;
104 foreach $x (keys %tiles)
107 $dbh->do("insert into gvp_world_tiles (zoom,tilex,tiley,latpix,lngpix) values ($zoom,$$y[0],$$y[1],$latpix,$lngpix)" ) ;
111 if ( int($count/100)*100 == $count )
113 print "Processed $count points...\n" ;
117 # Make sure there have been records to process before continuing...
122 print "No records to process...\n" ;
126 print "Total Points: $count\n" ;
128 # Remove old images...
130 for ( $zoom = $minzoom; $zoom <= $maxzoom; $zoom++ )
132 warn "clean $path/$zoom\n";
133 rmtree "$path/$zoom";
134 mkpath "$path/$zoom";
136 # Open up map icon files as images...
138 $imicon1 = GD::Image->newFromPng( $icon1 ) ;
139 $imicon2 = GD::Image->newFromPng( $icon2 ) ;
143 $dbh->do("create index gvp_world_tiles_main on gvp_world_tiles (zoom,tilex,tiley)") ;
144 $dbh->do("analyze gvp_world_tiles") ;
146 # Create tiles by zoom...
148 $sth = $dbh->prepare("select distinct zoom,tilex,tiley from gvp_world_tiles") ;
154 while ( ($zoom,$tilex,$tiley) = $sth->fetchrow_array )
158 # Calculate tile fields...
160 $file = $path . '/' . $zoom . '/v_' . $tilex . '_' . $tiley . '.png' ;
162 ($top,$left) = &Google_Tile_to_Pix( $value, $tiley, $tilex ) ;
166 $im = new GD::Image(256,256,0) ;
168 $white = $im->colorAllocate(255,255,255) ;
170 $im->interlaced('true') ;
172 $im->transparent($white) ;
174 $im->setThickness(1) ;
176 # Calculate which icon to use based on zoom...
186 $sti = $dbh->prepare("select latpix,lngpix from gvp_world_tiles where zoom = $zoom and tilex = $tilex and tiley = $tiley") ;
190 while ( ($latpix,$lngpix) = $sti->fetchrow_array )
192 $ix = $lngpix - $left - $xiconoff ; # Remove half image size
193 $iy = $latpix - $top - $yiconoff ; # Remove half image size
194 $im->copy($imicon,$ix,$iy,0,0,$xiconpix,$yiconpix) ;
197 open(my $PNG, '>', $file) || die "$file: $!";
198 print $PNG $im->png ;
200 # chmod(0444, $file) ;
201 if ( int($count/100)*100 == $count )
203 print "Processed $count tiles...\n" ;
206 print "Processed $count total tiles...\n" ;
208 #$dbh->do("drop table gvp_world_tiles") ;
210 # allow web server to select data
211 $dbh->do("grant select on gvp_world to public") ;