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" ;
61 $imicon = GD::Image->newFromPng( $zoom > 7 ? $icon2 : $icon1 ) ;
62 # Calculate which icon to use based on zoom...
64 my $custom_icon = "$name/icons/$zoom.png";
65 $imicon = GD::Image->newFromPng( $custom_icon ) if -e $custom_icon;
67 my $xiconpix = $imicon->width;
68 my $yiconpix = $imicon->height;
70 # FIXME make click position configurable
71 my $xiconoff = $xiconpix / 2;
72 my $yiconoff = $yiconpix / 2;
74 return ( $xiconpix, $yiconpix, $xiconoff, $yiconoff );
78 # Y,Top,N,S,Lat,Height
79 # X,Left,E,W,Lng,Width
83 eval { $dbh->do("drop table gvp_world_tiles") };
84 $dbh->do("create table gvp_world_tiles (zoom int2,tilex int4,tiley int4,latpix int4,lngpix int4)") ;
86 my $sql = "select (volpnt)[0] as lat, (volpnt)[1] as lng from gvp_world" ;
87 $sql = "select (point)[0] as lat, (point) [1] as lng from geo_count" if $name =~ m/koha/;
88 $sth = $dbh->prepare( $sql );
92 while ( ($lat,$lng) = $sth->fetchrow_array )
96 # Figure out what tiles are needed...
98 for ( my $zoom = $minzoom; $zoom <= $maxzoom; $zoom++ )
100 $value = &Google_Tile_Factors($zoom) ; # Calculate Tile Factors
102 ($latpix,$lngpix) = &Google_Coord_to_Pix( $value, $lat, $lng ) ;
105 my ( $xiconpix, $yiconpix, $xiconoff, $yiconoff ) = get_icon $zoom;
107 my ($tiley,$tilex) = &Google_Pix_to_Tile( $value, $latpix + $yiconoff, $lngpix + $xiconoff ) ;
108 $tiles{"$tiley $tilex"} = [$tilex, $tiley] ;
110 ($tiley,$tilex) = &Google_Pix_to_Tile( $value, $latpix + $yiconoff, $lngpix - $xiconoff ) ;
111 $tiles{"$tiley $tilex"} = [$tilex, $tiley] ;
113 ($tiley,$tilex) = &Google_Pix_to_Tile( $value, $latpix - $yiconoff, $lngpix + $xiconoff ) ;
114 $tiles{"$tiley $tilex"} = [$tilex, $tiley] ;
116 ($tiley,$tilex) = &Google_Pix_to_Tile( $value, $latpix - $yiconoff, $lngpix - $xiconoff ) ;
117 $tiles{"$tiley $tilex"} = [$tilex, $tiley] ;
119 foreach $x (keys %tiles)
122 $dbh->do("insert into gvp_world_tiles (zoom,tilex,tiley,latpix,lngpix) values ($zoom,$$y[0],$$y[1],$latpix,$lngpix)" ) ;
126 if ( int($count/100)*100 == $count )
128 print "Processed $count points...\n" ;
132 # Make sure there have been records to process before continuing...
137 print "No records to process...\n" ;
141 print "Total Points: $count\n" ;
143 # Remove old images...
145 for ( $zoom = $minzoom; $zoom <= $maxzoom; $zoom++ )
147 warn "clean $path/$zoom\n";
148 rmtree "$path/$zoom";
149 mkpath "$path/$zoom";
151 # Open up map icon files as images...
155 $dbh->do("create index gvp_world_tiles_main on gvp_world_tiles (zoom,tilex,tiley)") ;
156 $dbh->do("analyze gvp_world_tiles") ;
158 # Create tiles by zoom...
160 $sth = $dbh->prepare("select distinct zoom,tilex,tiley from gvp_world_tiles") ;
166 while ( my ($zoom,$tilex,$tiley) = $sth->fetchrow_array )
170 # Calculate tile fields...
172 $file = $path . '/' . $zoom . '/v_' . $tilex . '_' . $tiley . '.png' ;
174 ($top,$left) = &Google_Tile_to_Pix( $value, $tiley, $tilex ) ;
178 $im = new GD::Image(256,256,0) ;
180 $white = $im->colorAllocate(255,255,255) ;
182 $im->interlaced('true') ;
184 $im->transparent($white) ;
186 $im->setThickness(1) ;
188 my ( $xiconpix, $yiconpix, $xiconoff, $yiconoff ) = get_icon $zoom;
190 $sti = $dbh->prepare("select latpix,lngpix from gvp_world_tiles where zoom = $zoom and tilex = $tilex and tiley = $tiley") ;
194 while ( ($latpix,$lngpix) = $sti->fetchrow_array )
196 $ix = $lngpix - $left - $xiconoff ; # Remove half image size
197 $iy = $latpix - $top - $yiconoff ; # Remove half image size
198 # $im->copy($imicon,$ix,$iy,0,0,$xiconpix,$yiconpix) ;
199 $im->copyMerge($imicon,$ix,$iy,0,0,$xiconpix,$yiconpix,50) ;
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") ;