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";
46 my $path = "$name/tiles" ;
47 my $icon1 = 'images/gvp_icon_1.png' ; # Zooms up to 7
48 my $icon2 = 'images/gvp_icon_2.png' ; # Zooms after 7
49 my $dbh = DBI->connect ( "dbi:Pg:dbname=$name" , "" , "" , { AutoCommit => 1 } ) ;
51 # Make sure icon file exists...
53 if ( !(-e $icon1) or !(-e $icon2)) # Icon file missing - bad thing
55 print "Map Icon(s) missing\n" ;
62 # Calculate which icon to use based on zoom...
63 $imicon = GD::Image->newFromPng( $zoom > 7 ? $icon2 : $icon1 ) ;
66 my $custom_icon = "$name/icons/$zoom.png";
67 if ( -e $custom_icon ) {
68 $imicon = GD::Image->newFromPng( $custom_icon );
72 my $xiconpix = $imicon->width;
73 my $yiconpix = $imicon->height;
75 # FIXME make click position configurable
76 my $xiconoff = $xiconpix / 2;
77 my $yiconoff = $yiconpix / 2;
79 return ( $xiconpix, $yiconpix, $xiconoff, $yiconoff, $merge );
83 # Y,Top,N,S,Lat,Height
84 # X,Left,E,W,Lng,Width
88 eval { $dbh->do("drop table gvp_world_tiles") };
89 $dbh->do("create table gvp_world_tiles (zoom int2,tilex int4,tiley int4,latpix int4,lngpix int4)") ;
91 my $sql = "select (volpnt)[0] as lat, (volpnt)[1] as lng from gvp_world" ;
92 $sql = "select (point)[0] as lat, (point) [1] as lng from geo_count" if $name =~ m/koha/;
93 $sth = $dbh->prepare( $sql );
97 while ( ($lat,$lng) = $sth->fetchrow_array )
101 # Figure out what tiles are needed...
103 for ( my $zoom = $minzoom; $zoom <= $maxzoom; $zoom++ )
105 $value = &Google_Tile_Factors($zoom) ; # Calculate Tile Factors
107 ($latpix,$lngpix) = &Google_Coord_to_Pix( $value, $lat, $lng ) ;
110 my ( $xiconpix, $yiconpix, $xiconoff, $yiconoff ) = get_icon $zoom;
112 my ($tiley,$tilex) = &Google_Pix_to_Tile( $value, $latpix + $yiconoff, $lngpix + $xiconoff ) ;
113 $tiles{"$tiley $tilex"} = [$tilex, $tiley] ;
115 ($tiley,$tilex) = &Google_Pix_to_Tile( $value, $latpix + $yiconoff, $lngpix - $xiconoff ) ;
116 $tiles{"$tiley $tilex"} = [$tilex, $tiley] ;
118 ($tiley,$tilex) = &Google_Pix_to_Tile( $value, $latpix - $yiconoff, $lngpix + $xiconoff ) ;
119 $tiles{"$tiley $tilex"} = [$tilex, $tiley] ;
121 ($tiley,$tilex) = &Google_Pix_to_Tile( $value, $latpix - $yiconoff, $lngpix - $xiconoff ) ;
122 $tiles{"$tiley $tilex"} = [$tilex, $tiley] ;
124 foreach $x (keys %tiles)
127 $dbh->do("insert into gvp_world_tiles (zoom,tilex,tiley,latpix,lngpix) values ($zoom,$$y[0],$$y[1],$latpix,$lngpix)" ) ;
131 if ( int($count/100)*100 == $count )
133 print "Processed $count points...\n" ;
137 # Make sure there have been records to process before continuing...
142 print "No records to process...\n" ;
146 print "Total Points: $count\n" ;
148 # Remove old images...
150 for ( $zoom = $minzoom; $zoom <= $maxzoom; $zoom++ )
152 warn "clean $path/$zoom\n";
153 rmtree "$path/$zoom";
154 mkpath "$path/$zoom";
156 # Open up map icon files as images...
160 $dbh->do("create index gvp_world_tiles_main on gvp_world_tiles (zoom,tilex,tiley)") ;
161 $dbh->do("analyze gvp_world_tiles") ;
163 # Create tiles by zoom...
165 $sth = $dbh->prepare("select distinct zoom,tilex,tiley from gvp_world_tiles") ;
171 while ( my ($zoom,$tilex,$tiley) = $sth->fetchrow_array )
175 # Calculate tile fields...
177 $file = $path . '/' . $zoom . '/v_' . $tilex . '_' . $tiley . '.png' ;
179 ($top,$left) = &Google_Tile_to_Pix( $value, $tiley, $tilex ) ;
183 $im = new GD::Image(256,256,0) ;
185 $white = $im->colorAllocate(255,255,255) ;
187 $im->interlaced('true') ;
189 $im->transparent($white) ;
191 $im->setThickness(1) ;
193 my ( $xiconpix, $yiconpix, $xiconoff, $yiconoff, $merge ) = get_icon $zoom;
195 $sti = $dbh->prepare("select latpix,lngpix from gvp_world_tiles where zoom = $zoom and tilex = $tilex and tiley = $tiley") ;
199 while ( ($latpix,$lngpix) = $sti->fetchrow_array )
201 $ix = $lngpix - $left - $xiconoff ; # Remove half image size
202 $iy = $latpix - $top - $yiconoff ; # Remove half image size
203 # $im->copy($imicon,$ix,$iy,0,0,$xiconpix,$yiconpix) ;
204 $im->copyMerge($imicon,$ix,$iy,0,0,$xiconpix,$yiconpix,$merge) ;
207 open(my $PNG, '>', $file) || die "$file: $!";
208 print $PNG $im->png ;
210 # chmod(0444, $file) ;
211 if ( int($count/100)*100 == $count )
213 print "Processed $count tiles...\n" ;
216 print "Processed $count total tiles...\n" ;
218 #$dbh->do("drop table gvp_world_tiles") ;
220 # allow web server to select data
221 $dbh->do("grant select on gvp_world to public") ;