don't die on unexistant table drop
[google-map-tiles.git] / tiles.pl
1 #!/usr/bin/perl -w
2 # Create Map overlay of worldwide volcanoes...
3 # Author. John D. Coryat 02/2010...
4 # Copyright 1997-2010 USNaviguide LLC. All rights reserved.
5
6 use DBI ;
7 use strict ;
8 use GD ;
9 use USNaviguide_Google_Tiles ;
10 use File::Path;
11
12 my $name = shift @ARGV || die "usage: $0 database\n";
13
14 warn "WORKING on $name\n";
15
16 my $zoom        = 0 ;
17 my $lat         = 0 ;
18 my $lng         = 0 ;
19 my $latpix      = 0 ;
20 my $lngpix      = 0 ;
21 my %tiles       = ( ) ;
22 my $tilex       = 0 ;
23 my $tiley       = 0 ;
24 my $nacount     = 0 ;
25 my %tile        = ( ) ;
26 my $top         = 0 ;
27 my $left        = 0 ;
28 my $count       = 0 ;
29 my $x           = '' ;
30 my $y           = '' ;
31 my $i           = 0 ;
32 my $j           = 0 ;
33 my $k           = 0 ;
34 my $ix          = 0 ;
35 my $iy          = 0 ;
36 my $im          = '' ;
37 my $white       = '' ;
38 my $sth         = '' ;
39 my $sti         = '' ;
40 my $curcol      = '' ;
41 my $value       = '' ;
42 my $file        = '' ;
43 my $imicon      = '' ;
44 my $imicon1     = '' ;
45 my $imicon2     = '' ;
46 my $maxzoom     = 10 ;
47 my $minzoom     = 2 ;
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 } ) ;
56
57 # Make sure icon file exists...
58
59 if ( !(-e $icon1) or !(-e $icon2))                                      # Icon file missing - bad thing
60 {
61  print "Map Icon(s) missing\n" ;
62  exit ;
63 }
64
65
66 # Relations: 
67 # Y,Top,N,S,Lat,Height
68 # X,Left,E,W,Lng,Width
69
70 # (35,-89),(34,-90)
71
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)") ;
74
75 $sth = $dbh->prepare("select (volpnt)[0] as lat, (volpnt)[1] as lng from gvp_world") ;
76
77 $sth->execute ;
78
79 while ( ($lat,$lng) = $sth->fetchrow_array )
80 {
81  $count++ ;
82
83  # Figure out what tiles are needed...
84
85  for ( $zoom = $minzoom; $zoom <= $maxzoom; $zoom++ )
86  {
87   $value = &Google_Tile_Factors($zoom) ;                # Calculate Tile Factors
88
89   ($latpix,$lngpix) = &Google_Coord_to_Pix( $value, $lat, $lng ) ;
90   %tiles = ( ) ;
91
92   ($tiley,$tilex) = &Google_Pix_to_Tile( $value, $latpix + $yiconoff, $lngpix + $xiconoff ) ;
93   $tiles{"$tiley $tilex"} = [$tilex, $tiley] ;
94
95   ($tiley,$tilex) = &Google_Pix_to_Tile( $value, $latpix + $yiconoff, $lngpix - $xiconoff ) ;
96   $tiles{"$tiley $tilex"} = [$tilex, $tiley] ;
97
98   ($tiley,$tilex) = &Google_Pix_to_Tile( $value, $latpix - $yiconoff, $lngpix + $xiconoff ) ;
99   $tiles{"$tiley $tilex"} = [$tilex, $tiley] ;
100
101   ($tiley,$tilex) = &Google_Pix_to_Tile( $value, $latpix - $yiconoff, $lngpix - $xiconoff ) ;
102   $tiles{"$tiley $tilex"} = [$tilex, $tiley] ;
103
104   foreach $x (keys %tiles)
105   {
106    $y = $tiles{$x} ;
107    $dbh->do("insert into gvp_world_tiles (zoom,tilex,tiley,latpix,lngpix) values ($zoom,$$y[0],$$y[1],$latpix,$lngpix)" ) ;
108   }
109  }
110
111  if ( int($count/100)*100 == $count )
112  {
113   print "Processed $count points...\n" ;
114  }
115 }
116
117 # Make sure there have been records to process before continuing...
118
119 if ( !$count )
120 {
121  $dbh->disconnect ;
122  print "No records to process...\n" ;
123  exit ;
124 }
125
126 print "Total Points: $count\n" ;
127
128 # Remove old images...
129
130 for ( $zoom = $minzoom; $zoom <= $maxzoom; $zoom++ )
131 {
132  warn "clean $path/$zoom\n";
133  rmtree "$path/$zoom";
134  mkpath "$path/$zoom";
135 }
136 # Open up map icon files as images...
137
138 $imicon1 = GD::Image->newFromPng( $icon1 ) ;
139 $imicon2 = GD::Image->newFromPng( $icon2 ) ;
140
141 # Create index...
142
143 $dbh->do("create index gvp_world_tiles_main on gvp_world_tiles (zoom,tilex,tiley)") ;
144 $dbh->do("analyze gvp_world_tiles") ;
145
146 # Create tiles by zoom...
147
148 $sth = $dbh->prepare("select distinct zoom,tilex,tiley from gvp_world_tiles") ;
149
150 $sth->execute ;
151
152 $count = 0 ;
153
154 while ( ($zoom,$tilex,$tiley) = $sth->fetchrow_array )
155 {
156  $count++ ;
157
158  # Calculate tile fields...
159
160  $file = $path . '/' . $zoom . '/v_' . $tilex . '_' . $tiley . '.png' ;
161
162  ($top,$left) = &Google_Tile_to_Pix( $value, $tiley, $tilex ) ;
163
164  # create a new image
165
166  $im = new GD::Image(256,256,0) ;
167
168  $white = $im->colorAllocate(255,255,255) ;
169
170  $im->interlaced('true') ;
171
172  $im->transparent($white) ;
173
174  $im->setThickness(1) ;
175
176  # Calculate which icon to use based on zoom...
177
178  if ( $zoom > 7 )
179  {
180   $imicon = $imicon2 ;
181  } else
182  {
183   $imicon = $imicon1 ;
184  }
185
186  $sti = $dbh->prepare("select latpix,lngpix from gvp_world_tiles where zoom = $zoom and tilex = $tilex and tiley = $tiley") ;
187
188  $sti->execute ;
189
190  while ( ($latpix,$lngpix) = $sti->fetchrow_array )
191  {
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) ;
195  }
196
197  open(my $PNG, '>', $file) || die "$file: $!";
198  print $PNG $im->png ;
199  close $PNG ;
200 # chmod(0444, $file) ;
201  if ( int($count/100)*100 == $count )
202  {
203   print "Processed $count tiles...\n" ;
204  }
205 }
206 print "Processed $count total tiles...\n" ;
207
208 #$dbh->do("drop table gvp_world_tiles") ;
209
210 # allow web server to select data
211 $dbh->do("grant select on gvp_world to public") ;
212
213 $dbh->disconnect ;
214