[go: up one dir, main page]

Menu

[r1]: / omgm / dev / tilecutter.pl  Maximize  Restore  History

Download this file

172 lines (138 with data), 3.9 kB

  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
#!/usr/bin/perl -w
# Cut tiles from a static Mercator projected PNG image for a certain zoom...
# Author. John D. Coryat 02/2008...
# USNaviguide LLC
# Released under Apache 2.0 license...
#
# Run as: tilecutter.pl <zoom> <South Latitude>,<West Longitude> <North Latitude>,<East Longitude> <Source Image Name>
use DBI ;
use USNaviguide_Google_RelPix ;
use USNaviguide_Google_Tiles ;
use strict ;
use GD ;
# USA Box: SW: 25,-126 NE: 50,-66
my $zoom = $ARGV[0] ;
my $sw = $ARGV[1] ;
my $ne = $ARGV[2] ;
my $srcfile = $ARGV[3] ;
my @box = ( ) ;
my @d1 = ( ) ;
my @d2 = ( ) ;
my $ims = '' ;
my $iwd = 0 ;
my $iht = 0 ;
my $wd = 0 ;
my $ht = 0 ;
my $lat = 0 ;
my $lng = 0 ;
my $north = 0 ;
my $west = 0 ;
my $south = 0 ;
my $east = 0 ;
my $top = 0 ;
my $left = 0 ;
my $line = '' ;
my @tiles = ( ) ;
my %tile = ( ) ;
my $count = 0 ;
my $i = 0 ;
my $x = '' ;
my $y = '' ;
my $ix = 0 ;
my $iy = 0 ;
my $im = '' ;
my $imx = '' ;
my $white = '' ;
my $red = 0 ;
my $green = 0 ;
my $blue = 0 ;
my $path = '';
# Relations:
# Y,Top,N,S,Lat,Height
# X,Left,E,W,Lng,Width
if ( !defined($zoom) or !defined($sw) or !$sw or !defined($ne) or !$ne or !defined($srcfile) or !$srcfile)
{
print "Run as: tilecutter.pl <zoom> <South Latitude>,<West Longitude> <North Latitude>,<East Longitude> <Source PNG Image Name>\n" ;
exit ;
}
if ( $srcfile !~ /\.png$/i )
{
print "Source file must be a PNG file to be processed by this program. No other formats are supported at this time...\n" ;
exit ;
}
if ( (!-e ($srcfile)) )
{
print "Unable to open $srcfile, please check your file and retry.\n" ; exit ;
}
open (PNG,$srcfile) ;
$ims = newFromPng GD::Image(\*PNG) ;
close PNG ;
($iwd,$iht) = $ims->getBounds() ;
$white = $ims->transparent() ;
print "Transparent is $white\n";
if ( $white > -1 )
{
($red,$green,$blue) = $ims->rgb($white) ;
print "Transparent is $red,$green,$blue\n" ;
}
print scalar( localtime() ) . " Cut tiles from a static Mercator projected .png image for a certain zoom...\n" ;
print scalar( localtime() ) . " Zoom: $zoom \t SW: $sw \t NE: $ne \t Source Image: $srcfile\n" ;
print scalar( localtime() ) . " Source File Dimensions: Width $iwd\t Height $iht\n" ;
if( $sw =~ /(.*),(.*)/ )
{
$lat = $1 ;
$lng = $2 ;
$south = $1 ;
$west = $2 ;
push(@box,$1) ;
push(@box,$2) ;
@d1 = &Google_RelPix($lat, $lng, $zoom);
}
if( $ne =~ /(.*),(.*)/ )
{
$lat = $1 ;
$lng = $2 ;
$north = $1 ;
$east = $2 ;
push(@box,$1) ;
push(@box,$2) ;
@d2 = &Google_RelPix($lat, $lng, $zoom);
}
($top,$left) = &Google_RelPix($north, $west, $zoom);
$ht = abs( $d1[0] - $d2[0] ) ;
$wd = abs( $d1[1] - $d2[1] ) ;
# create a new image
$im = new GD::Image($wd,$ht,0) ;
if ( $white > -1 )
{
$x = $im->colorAllocate($red,$green,$blue) ;
$im->transparent( 2147483647 ) ;
}
$im->copyResampled($ims,0,0,0,0,$wd,$ht,$iwd,$iht);
print print scalar( localtime() ) . " Map dimensions: $wd,$ht\n" ;
# Create Tiles from Large Image...
@tiles = &Google_Tiles($south, $west, $north, $east, $zoom) ;
print print scalar( localtime() ) . " Tile Count: " . ($#tiles + 1) . "\n" ;
$count = 0 ;
for ( $i = 0; $i <= $#tiles; $i++ )
{
%tile = %{$tiles[$i]} ;
$count++ ;
$imx = new GD::Image(256,256,0) ;
$imx->interlaced('true') ;
if ( $white > -1 )
{
$x = $imx->colorAllocate($red,$green,$blue) ;
$imx->transparent( 2147483647 ) ;
}
$imx->copy($im,0,0,$tile{'PXW'} - $left,$tile{'PYN'} - $top,256,256);
$path = "/home/org/ifk/public_html/gmolm/uploads/imagetiles/$zoom/" . "x" . $tile{'NAMEX'} . "_y" . $tile{'NAMEY'} . ".png";
print print scalar( localtime() ) . " Produce tile: $path\n" ;
open PNG, ">".$path ;
print PNG $imx->png ;
close PNG ;
if ( int($count/100)* 100 == $count )
{
print print scalar( localtime() ) . " Tiles produced: ($zoom) $count\n" ;
}
}
print print scalar( localtime() ) . " Total Tiles produced: ($zoom) $count\n" ;