View Single Post
Posts: 61 | Thanked: 36 times | Joined on Feb 2006 @ Harpenden
#87
And here's a (very)quick and (very)dirty Python script to scrape maps.

Code:
#!/usr/bin/env python
import sys,os
from math import *
from optparse import OptionParser
from urllib import urlretrieve

MERCATOR_SPAN=(-6.28318377773622)
MERCATOR_TOP=(3.14159188886811)
WORLD_SIZE_UNITS=(1 << 26)

def latlon2unit(lat, lon):
	unitx = (lon + 180.0) * (WORLD_SIZE_UNITS / 360.0) + 0.5
	tmp = sin(lat * (pi / 180.0))
	unity = 0.50 + (WORLD_SIZE_UNITS / MERCATOR_SPAN) * (log((1.0 + tmp) / (1.0 - tmp)) * 0.50 - MERCATOR_TOP);
	return (unitx,unity)

def tile2zunit(tile, zoom):
	return ((tile) << (8 + zoom))

def unit2ztile(munit, zoom):
	return ((int)(munit) >> (8 + zoom))

def loadImage(x,y,zoom):
	url = "http://mt.google.com/mt?n=404&v=w2.11&x="+str(x)+"&y="+str(y)+"&zoom="+str(zoom)
	destination = dirpath+"/"+str(y)+".jpg"
	if(os.access(destination,os.R_OK) == False):
		print "Downloading "+url+" to "+destination
	else:
		print "Already got " + destination
	urlretrieve(url,destination)
	

parser = OptionParser()
parser.add_option("-t", "--start-lat", dest="startlat",help="start latitude",type="float")
parser.add_option("-l", "--start-long",dest="startlong",help="start longitude",type="float")
parser.add_option("-b", "--end-lat", dest="endlat",help="end latitude",type="float")
parser.add_option("-r", "--end-long",dest="endlong",help="end longitude",type="float")
parser.add_option("-z", "--zoom",dest="zoom",help="zoom level",type="int")

(options, args) = parser.parse_args()

(sux,suy) = latlon2unit(options.startlat,options.startlong)
(eux,euy) = latlon2unit(options.endlat,options.endlong)

if eux < sux:
	x = eux
	eux = sux
	sux = x

if euy < suy:
	y = euy
	euy = suy
	suy = y

start_tilex = unit2ztile(sux, options.zoom + 1);

start_tiley = unit2ztile(suy, options.zoom + 1);

end_tilex = unit2ztile(eux, options.zoom + 1);

end_tiley = unit2ztile(euy, options.zoom + 1);


numMaps=(end_tilex-start_tilex)*(end_tiley-start_tiley)

print "About to retrieve "+str(numMaps)+" maps"

for x in range(start_tilex,end_tilex):
	dirpath="maps/"+str(options.zoom)+"/"+str(x)
	if(os.access(dirpath,os.W_OK) == False):
		os.makedirs(dirpath)
	for y in range(start_tiley,end_tiley):
		loadImage(x,y,options.zoom)
Code:
./getMaps.py -h
usage: getMaps.py [options]

options:
  -h, --help            show this help message and exit
  -t STARTLAT, --start-lat=STARTLAT
                        start latitude
  -l STARTLONG, --start-long=STARTLONG
                        start longitude
  -b ENDLAT, --end-lat=ENDLAT
                        end latitude
  -r ENDLONG, --end-long=ENDLONG
                        end longitude
  -z ZOOM, --zoom=ZOOM  zoom level
use it like
Code:
 ./getMaps.py -t 51.82 -l -0.3 -b 51.49 -r 0 -z 4