#!/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" ;