merge icons 50% to better represent overlaping ones
[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 $nacount     = 0 ;
23 my %tile        = ( ) ;
24 my $top         = 0 ;
25 my $left        = 0 ;
26 my $count       = 0 ;
27 my $x           = '' ;
28 my $y           = '' ;
29 my $i           = 0 ;
30 my $j           = 0 ;
31 my $k           = 0 ;
32 my $ix          = 0 ;
33 my $iy          = 0 ;
34 my $im          = '' ;
35 my $white       = '' ;
36 my $sth         = '' ;
37 my $sti         = '' ;
38 my $curcol      = '' ;
39 my $value       = '' ;
40 my $file        = '' ;
41 my $imicon      = '' ;
42 my $imicon1     = '' ;
43 my $imicon2     = '' ;
44 my $maxzoom     = 10 ;
45 my $minzoom     = 2 ;
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 } ) ;
50
51 # Make sure icon file exists...
52
53 if ( !(-e $icon1) or !(-e $icon2))                                      # Icon file missing - bad thing
54 {
55  print "Map Icon(s) missing\n" ;
56  exit ;
57 }
58
59 sub get_icon {
60         my $zoom = shift;
61         $imicon = GD::Image->newFromPng( $zoom > 7 ? $icon2 : $icon1 ) ;
62          # Calculate which icon to use based on zoom...
63
64         my $custom_icon = "$name/icons/$zoom.png";
65         $imicon = GD::Image->newFromPng( $custom_icon ) if -e $custom_icon;
66
67         my $xiconpix = $imicon->width;
68         my $yiconpix = $imicon->height;
69
70         # FIXME make click position configurable
71         my $xiconoff = $xiconpix / 2;
72         my $yiconoff = $yiconpix / 2;
73
74         return ( $xiconpix, $yiconpix, $xiconoff, $yiconoff );
75 }
76
77 # Relations: 
78 # Y,Top,N,S,Lat,Height
79 # X,Left,E,W,Lng,Width
80
81 # (35,-89),(34,-90)
82
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)") ;
85
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 );
89
90 $sth->execute ;
91
92 while ( ($lat,$lng) = $sth->fetchrow_array )
93 {
94  $count++ ;
95
96  # Figure out what tiles are needed...
97
98  for ( my $zoom = $minzoom; $zoom <= $maxzoom; $zoom++ )
99  {
100   $value = &Google_Tile_Factors($zoom) ;                # Calculate Tile Factors
101
102   ($latpix,$lngpix) = &Google_Coord_to_Pix( $value, $lat, $lng ) ;
103   %tiles = ( ) ;
104
105   my ( $xiconpix, $yiconpix, $xiconoff, $yiconoff ) = get_icon $zoom;
106
107   my ($tiley,$tilex) = &Google_Pix_to_Tile( $value, $latpix + $yiconoff, $lngpix + $xiconoff ) ;
108   $tiles{"$tiley $tilex"} = [$tilex, $tiley] ;
109
110   ($tiley,$tilex) = &Google_Pix_to_Tile( $value, $latpix + $yiconoff, $lngpix - $xiconoff ) ;
111   $tiles{"$tiley $tilex"} = [$tilex, $tiley] ;
112
113   ($tiley,$tilex) = &Google_Pix_to_Tile( $value, $latpix - $yiconoff, $lngpix + $xiconoff ) ;
114   $tiles{"$tiley $tilex"} = [$tilex, $tiley] ;
115
116   ($tiley,$tilex) = &Google_Pix_to_Tile( $value, $latpix - $yiconoff, $lngpix - $xiconoff ) ;
117   $tiles{"$tiley $tilex"} = [$tilex, $tiley] ;
118
119   foreach $x (keys %tiles)
120   {
121    $y = $tiles{$x} ;
122    $dbh->do("insert into gvp_world_tiles (zoom,tilex,tiley,latpix,lngpix) values ($zoom,$$y[0],$$y[1],$latpix,$lngpix)" ) ;
123   }
124  }
125
126  if ( int($count/100)*100 == $count )
127  {
128   print "Processed $count points...\n" ;
129  }
130 }
131
132 # Make sure there have been records to process before continuing...
133
134 if ( !$count )
135 {
136  $dbh->disconnect ;
137  print "No records to process...\n" ;
138  exit ;
139 }
140
141 print "Total Points: $count\n" ;
142
143 # Remove old images...
144
145 for ( $zoom = $minzoom; $zoom <= $maxzoom; $zoom++ )
146 {
147  warn "clean $path/$zoom\n";
148  rmtree "$path/$zoom";
149  mkpath "$path/$zoom";
150 }
151 # Open up map icon files as images...
152
153 # Create index...
154
155 $dbh->do("create index gvp_world_tiles_main on gvp_world_tiles (zoom,tilex,tiley)") ;
156 $dbh->do("analyze gvp_world_tiles") ;
157
158 # Create tiles by zoom...
159
160 $sth = $dbh->prepare("select distinct zoom,tilex,tiley from gvp_world_tiles") ;
161
162 $sth->execute ;
163
164 $count = 0 ;
165
166 while ( my ($zoom,$tilex,$tiley) = $sth->fetchrow_array )
167 {
168  $count++ ;
169
170  # Calculate tile fields...
171
172  $file = $path . '/' . $zoom . '/v_' . $tilex . '_' . $tiley . '.png' ;
173
174  ($top,$left) = &Google_Tile_to_Pix( $value, $tiley, $tilex ) ;
175
176  # create a new image
177
178  $im = new GD::Image(256,256,0) ;
179
180  $white = $im->colorAllocate(255,255,255) ;
181
182  $im->interlaced('true') ;
183
184  $im->transparent($white) ;
185
186  $im->setThickness(1) ;
187
188  my ( $xiconpix, $yiconpix, $xiconoff, $yiconoff ) = get_icon $zoom;
189
190  $sti = $dbh->prepare("select latpix,lngpix from gvp_world_tiles where zoom = $zoom and tilex = $tilex and tiley = $tiley") ;
191
192  $sti->execute ;
193
194  while ( ($latpix,$lngpix) = $sti->fetchrow_array )
195  {
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) ;
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