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