if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ]
then
        echo "========================================================================"
        echo "usage:"
        echo "        ./gethybrid.sh [latitude] [longitude] [base zoom] [map size]"
        echo "example:"
        echo "         ./gethybrid.sh 40.619442 -73.970926 1 16"
        echo "latitude/longitude must be in decimal format"
        echo "base zoom: 0-16"
        echo "map size: number of 256x256 pix tiles in 1 row"
        echo "map size: 16 will create an equivalent of 4096x4096 pixel map at base zoom"
        echo "========================================================================"
        exit
fi



PI=3.14159265358979326433
bc=6.28318530717958652866
Wa=0.017453292519943295912944444444444

lat=$1
lon=$2
basezoom=$3

c=256
d=17

while [ $d -ge $basezoom ]
do

        let d=d-1
        pixelsPerLonDegree=`echo "scale=10; $c/360" | bc -l`
        pixelsPerLonRadian=`echo "scale=10; $c/$bc" | bc -l`
        bitmapOrigo=`echo "scale=10; $c/2" | bc -l`
        numTiles=`echo "scale=10; $c/256" | bc -l`
        c=`echo "scale=10; $c*2" |bc -l`
done
x=`echo "scale=10; ($bitmapOrigo+$lon*$pixelsPerLonDegree)/256" | bc -l| cut -d \. -f1`
e=`echo "scale=10;  s($lat*$Wa)" |bc -l`


y=`echo "scale=10; ($bitmapOrigo+0.5* l((1+($e))/(1-($e)))*-1*$pixelsPerLonRadian)/256" | bc -l| cut -d \. -f1`

echo $x $y




rm log 2>/dev/null

zoom=$3
mapsize=$4

ms=$4
total=0;
while [ $ms -gt 1 ]
do
        let total=total+ms*ms
        let ms=ms/2
done

dirname="_MAP_FOR_LAT_$1_LON_$2_$4x"
mkdir $dirname

ms_original=$mapsize
let xs=x/mapsize*mapsize
let ys=y/mapsize*mapsize
echo $xs $ys $zoom>$dirname/coords.txt 

zm=1

tilecount=1
while [ $mapsize -gt 1 ]
do
yc=0
	let zm=ms_original/mapsize
	echo "ms_original:$ms_original:$mapsize:$zm"
	mkdir $dirname/${zm}x
while  [ $yc -lt $mapsize ]
do
	mkdir "$dirname/${zm}x/`printf %03d ${yc}`"
	xc=0
	let y=ys+yc
	while  [ $xc -lt $mapsize ]
	do
		let x=xs+xc
		URL="http://mt.google.com/mt?n=404&v=w2t.22&x=$x&y=$y&zoom=$zoom"
		 echo "Getting $tilecount out of $total"
                 let tilecount=tilecount+1
		#echo "$URL"
		wget -t0 -o log -O `printf %s/%dx/%03d/%dx%03d%03d $dirname $zm $yc $zm $yc $xc`.png "$URL"
		keyhole=`java  -classpath . GoogleTile $1 $2 $zoom $x $y|grep keyhole=|cut -d '=' -f2`
		URL="http://kh1.google.com/kh?n=404&v=7&t=$keyhole"
		#echo "$URL"
		wget -t0 -o log -O `printf %s/%dx/%03d/K%dx%03d%03d $dirname $zm $yc $zm $yc $xc`.jpg "$URL"
		jpegtopnm  `printf %s/%dx/%03d/K%dx%03d%03d $dirname $zm $yc $zm $yc $xc`.jpg 2>/dev/null |pnmtopng > `printf %s/%dx/%03d/K%dx%03d%03d $dirname $zm $yc $zm $yc $xc`.png
		composite `printf %s/%dx/%03d/%dx%03d%03d $dirname $zm $yc $zm $yc $xc`.png `printf %s/%dx/%03d/K%dx%03d%03d $dirname $zm $yc $zm $yc $xc`.png `printf %s/%dx/%03d/%dx%03d%03d $dirname $zm $yc $zm $yc $xc`.png
	rm `printf %s/%dx/%03d/K%dx%03d%03d $dirname $zm $yc $zm $yc $xc`.jpg 2>/dev/null
	rm  `printf %s/%dx/%03d/K%dx%03d%03d $dirname $zm $yc $zm $yc $xc`.png 2>/dev/null
		pngtopnm `printf %s/%dx/%03d/%dx%03d%03d $dirname $zm $yc $zm $yc $xc`.png| pnmtojpeg > `printf %s/%dx/%03d/%dx%03d%03d $dirname $zm $yc $zm $yc $xc`.jpg
	rm  `printf %s/%dx/%03d/%dx%03d%03d $dirname $zm $yc $zm $yc $xc`.png 2>/dev/null
		let xc=xc+1
	done
		let yc=yc+1
done

let xs=xs/2
let ys=ys/2
let zoom=zoom+1
let mapsize=mapsize/2
#echo $xs $ys $zoom $maxsize

done
