Apr 25, 2009

How to download full resolution satellite images from Google Earth/Maps

Google Maps uses the following technique to serve images: It displays 256x256px blocks of images from their server. The server displays the image from the given parameters, for example:
http://khm3.google.com/kh/v=38&hl=en&x=35415&s=&y=23296&z=16
So, with a script, you can fetch all the images from the starting X and Y, to the ending and then stitch them together piece by piece. Below is an example how to get the parameters for the image (v,x,y,z) And here's a script that does all the work:
<?php
error_reporting(E_ALL ^ E_NOTICE);
set_time_limit(0);
ini_set("memory_limit","1024M");

// Bottom Left
//http://khm0.google.com/kh/v=38&hl=en&x=141234&s=&y=93147&z=18&s=G
//Top Right
//http://khm3.google.com/kh/v=38&hl=en&x=141273&s=&y=93105&z=18&s=Gali

//How big is the block (in pixels)
$block_width = 256;
$block_height = 256;

//I don't know what that is, but look at the URL
$v = 38;
//Zoom level
$z = 18;

//Get these parameters from the url
$start_x = 141234;
$start_y = 93105;

$end_x = 141273;
$end_y = 93147;

//Do some simple math... Dimensions of the panorama
$dim_x = $end_x - $start_x;
$dim_y = $end_y - $start_y;

$dim_x *= $block_width;
$dim_y *= $block_height;

print("$dim_x x $dim_y px\n");

//Allocate memory space for the panorama
$img = imagecreatetruecolor($dim_x,$dim_y);

for($x = $start_x ; $x <= $end_x ; $x++) {
 for($y = $start_y ; $y <= $end_y ; $y++) {
  $f = "$x-$y-$z.jpg";
  $url = "http://www.najdi.si/servlet/ServletRedirectTileImage?x=$x&y=$y&zl=$z";
  $url = "http://khm3.google.com/kh/v=$v&x=$x&y=$y&z=$z";
  
  //Download the block
  $cache = "wget \"$url\" -O $f";
  print("$cache\n");
  passthru($cache);
  
  //Put it on the panorama
  $block = imagecreatefromjpeg($f);
  imagecopy($img, $block, ($x-$start_x)*$block_width, ($y-$start_y)*$block_height, 0, 0, $block_width, $block_height);
  imagedestroy($block);
 }
}

imagejpeg($img,"$start_x-$end_x-_-$start_y-$end_y.jpg",100);
?>
I also noticed that many mapping sites are using the same technique. End result:


full.jpg

No comments: