# Copyright (C) 2009 # # This program is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by the # Free Software Foundation; either version 2, or (at your option) any # later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA # #!/usr/bin/python import sys import os import re from math import * from httplib import HTTPConnection tile_size = 256; root = 'maps' def dtor(degree): return degree * pi / 180 def rtod(radiant): return radiant / pi * 180 def get_tile(lat, lon, zoom): x = lon / 180 y = asinh(tan(dtor(lat))) / pi tile_x = pow(2, zoom) * (x + 1) / 2 tile_y = pow(2, zoom) * (1 - y) / 2 return tile_x, tile_y def get_coords(tile_x, tile_y, zoom): x = tile_x * 2 / pow(2, zoom) - 1 y = -(tile_y * 2 / pow(2, zoom) - 1) lon = x * 180 lat = rtod(atan(sinh(y * pi))) return lat, lon def write_cal(file, map_path, x, y, zoom): file.write("[Map]\n") file.write("Bitmap = " + map_path.split("/")[-1] + "\n") file.write("Size = SIZE(256, 256)\n") x = x * (1 << zoom) y = y * (1 << zoom) for i in range(0, 4): lat, lon = get_coords(x + (i % 2), y + i / 2, zoom) file.write("[Calibration Point " + str(i + 1) + "]\n") file.write("Longitude = " + str(lon) + "\n") file.write("Latitude = " + str(lat) + "\n") file.write("Pixel = POINT(" + str((i % 2) * 255) + "," + str((i / 2) * 255) + ")\n") def tiles_dir(position): path = root while len(position) > 3: path += '/' + position[:3] position = position[3:] try: os.makedirs(path) except: pass return path def fetch_tiles(position, precision): path = tiles_dir(position) out_path = path + '/' + position kal_path = out_path + ".kal" out_path = out_path + ".jpeg" if os.access(out_path, os.F_OK): print " ", out_path else: try: http = HTTPConnection('h0.ortho.tiles.virtualearth.net', 80) http.request('GET', '/tiles/h' + position + '?g=275') resp = http.getresponse() out = open(out_path, 'w') out.write(resp.read()) out.close() print "D", out_path except: print "F", out_path if 1 == 1 or not os.access(kal_path, os.F_OK): x = y = 0. i = 2. for p in position: if int(p) >= 2: y = y + 1 / i if int(p) % 2: x = x + 1 / i i = i * 2 print "x, y: ", get_coords(x * (1 << len(position)), y * (1 << len(position)), len(position)) out = open(kal_path, 'w') write_cal(out, out_path, x, y, len(position)) out.close() if len(position) < precision: fetch_tiles(position + '0', precision) fetch_tiles(position + '1', precision) fetch_tiles(position + '2', precision) fetch_tiles(position + '3', precision) position = sys.argv[1] precision = int(sys.argv[2]) fetch_tiles(position, precision)