fix count from query if needed
[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 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 );
78
79 $sth->execute ;
80
81 while ( ($lat,$lng) = $sth->fetchrow_array )
82 {
83  $count++ ;
84
85  # Figure out what tiles are needed...
86
87  for ( $zoom = $minzoom; $zoom <= $maxzoom; $zoom++ )
88  {
89   $value = &Google_Tile_Factors($zoom) ;                # Calculate Tile Factors
90
91   ($latpix,$lngpix) = &Google_Coord_to_Pix( $value, $lat, $lng ) ;
92   %tiles = ( ) ;
93
94   ($tiley,$tilex) = &Google_Pix_to_Tile( $value, $latpix + $yiconoff, $lngpix + $xiconoff ) ;
95   $tiles{"$tiley $tilex"} = [$tilex, $tiley] ;
96
97   ($tiley,$tilex) = &Google_Pix_to_Tile( $value, $latpix + $yiconoff, $lngpix - $xiconoff ) ;
98   $tiles{"$tiley $tilex"} = [$tilex, $tiley] ;
99
100   ($tiley,$tilex) = &Google_Pix_to_Tile( $value, $latpix - $yiconoff, $lngpix + $xiconoff ) ;
101   $tiles{"$tiley $tilex"} = [$tilex, $tiley] ;
102
103   ($tiley,$tilex) = &Google_Pix_to_Tile( $value, $latpix - $yiconoff, $lngpix - $xiconoff ) ;
104   $tiles{"$tiley $tilex"} = [$tilex, $tiley] ;
105
106   foreach $x (keys %tiles)
107   {
108    $y = $tiles{$x} ;
109    $dbh->do("insert into gvp_world_tiles (zoom,tilex,tiley,latpix,lngpix) values ($zoom,$$y[0],$$y[1],$latpix,$lngpix)" ) ;
110   }
111  }
112
113  if ( int($count/100)*100 == $count )
114  {
115   print "Processed $count points...\n" ;
116  }
117 }
118
119 # Make sure there have been records to process before continuing...
120
121 if ( !$count )
122 {
123  $dbh->disconnect ;
124  print "No records to process...\n" ;
125  exit ;
126 }
127
128 print "Total Points: $count\n" ;
129
130 # Remove old images...
131
132 for ( $zoom = $minzoom; $zoom <= $maxzoom; $zoom++ )
133 {
134  warn "clean $path/$zoom\n";
135  rmtree "$path/$zoom";
136  mkpath "$path/$zoom";
137 }
138 # Open up map icon files as images...
139
140 $imicon1 = GD::Image->newFromPng( $icon1 ) ;
141 $imicon2 = GD::Image->newFromPng( $icon2 ) ;
142
143 # Create index...
144
145 $dbh->do("create index gvp_world_tiles_main on gvp_world_tiles (zoom,tilex,tiley)") ;
146 $dbh->do("analyze gvp_world_tiles") ;
147
148 # Create tiles by zoom...
149
150 $sth = $dbh->prepare("select distinct zoom,tilex,tiley from gvp_world_tiles") ;
151
152 $sth->execute ;
153
154 $count = 0 ;
155
156 while ( ($zoom,$tilex,$tiley) = $sth->fetchrow_array )
157 {
158  $count++ ;
159
160  # Calculate tile fields...
161
162  $file = $path . '/' . $zoom . '/v_' . $tilex . '_' . $tiley . '.png' ;
163
164  ($top,$left) = &Google_Tile_to_Pix( $value, $tiley, $tilex ) ;
165
166  # create a new image
167
168  $im = new GD::Image(256,256,0) ;
169
170  $white = $im->colorAllocate(255,255,255) ;
171
172  $im->interlaced('true') ;
173
174  $im->transparent($white) ;
175
176  $im->setThickness(1) ;
177
178  # Calculate which icon to use based on zoom...
179
180  if ( $zoom > 7 )
181  {
182   $imicon = $imicon2 ;
183  } else
184  {
185   $imicon = $imicon1 ;
186  }
187
188  my $custom_icon = "$name/icons/$zoom.png";
189  $imicon = GD::Image->newFromPng( $custom_icon ) if -e $custom_icon;
190
191  $sti = $dbh->prepare("select latpix,lngpix from gvp_world_tiles where zoom = $zoom and tilex = $tilex and tiley = $tiley") ;
192
193  $sti->execute ;
194
195  while ( ($latpix,$lngpix) = $sti->fetchrow_array )
196  {
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) ;
200  }
201
202  open(my $PNG, '>', $file) || die "$file: $!";
203  print $PNG $im->png ;
204  close $PNG ;
205 # chmod(0444, $file) ;
206  if ( int($count/100)*100 == $count )
207  {
208   print "Processed $count tiles...\n" ;
209  }
210 }
211 print "Processed $count total tiles...\n" ;
212
213 #$dbh->do("drop table gvp_world_tiles") ;
214
215 # allow web server to select data
216 $dbh->do("grant select on gvp_world to public") ;
217
218 $dbh->disconnect ;
219