Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 11-12 of 12 total

Converting between 2 google map tile types

Google maps is a marvelous app.
I try to program a prototype of it on pys60
http://bigbold.com/snippets/posts/show/458

There I only show the default map, not the satellite images.
Retrieving a different mode isn't that difficult.
I read the info from here
http://intepid.com/2005-07-17/21.50/
Then I begin comparing the 2 tile types of the same area
(around California)
http://mt.google.com/mt?v=w2.5&x=20&y=49&zoom=10 (map)
http://kh.google.com/kh?v=3&t=tqtsqrqt (satellite)

Here's the conversion routine between x,y,zoom and quadtree
def quadtree(x,y, zoom):
	out = []
	m = {(0,0):'q', (0,1):'t', (1,0):'r', (1,1):'s'}
	for i in range(17-zoom):
		x, rx = divmod(x, 2)
		y, ry = divmod(y, 2)
		out.insert(0, m[(rx,ry)])
	return 't' + ''.join(out)

Then to convert back
def xyzoom(quad):
	x, y, z = 0, 0, 17
	m = {'q':(0,0), 't':(0,1), 'r':(1,0), 's':(1,1)}
 	for c in quad[1:]:
		x = x*2 + m[c][0]
		y = y*2 + m[c][1]
		z -= 1
	return x, y, z

Using them is simple
>>> quadtree(20,49,10)
'tqtsqrqt'
>>> xyzoom('tqtsqrqt')
(20, 49, 10)
>>> sat_url = 'http://kh.google.com/kh?v=3&t=' + quadtree(20,49,10)

Using google map on mobile phone

Download the tile images for testing
from urllib import urlretrieve
turl =  'http://mt.google.com/mt?v=w2.4&x=%s&y=%s&zoom=%s'
tfile = '%s-%s-%s.gif'
z = 12
for x in range(5,10):
  for y in range(10,14):
    urlretrieve(turl % (x,y,z), tfile % (z,y,x))

Then put the images in the phone. Then use the following code
to browse around the images (and zoom in/out)
from appuifw import *
from key_codes import *
from graphics import Image
import e32

app.screen = 'full'
app.body = c= Canvas()

x, y, z = 10*256, 24*256, 11
dirname = u'C:\\system\\data\\gmap\\'  # where I save the tile images

def draw():
    gx, ox = divmod(x, 256)
    gy, oy = divmod(y, 256)
    f = dirname + '%s-%s-%s.gif' % (z,gy,gx)
    c.blit(Image.open(f), target=(-ox,-oy))

    if ox > 80:
        f = dirname + '%s-%s-%s.gif' % (z,gy,gx+1)
        c.blit(Image.open(f), target=(256-ox,-oy))
    if oy > 48:
        f = dirname + '%s-%s-%s.gif' % (z,gy+1,gx)
        c.blit(Image.open(f), target=(-ox,256-oy))
    if ox > 80 and oy > 48:
        f = dirname + '%s-%s-%s.gif' % (z,gy+1,gx+1)
        c.blit(Image.open(f), target=(256-ox,256-oy))

def move(dx,dy):
    global x, y
    x += dx * 50
    y += dy * 50
    draw()

def zoomin():
    global x,y,z
    x = x*2 + 88
    y = y*2 + 104
    z = z-1
    draw()

def zoomout():
    global x,y,z
    x = x/2 - 44
    y = y/2 - 52
    z = z+1
    draw()

c.bind(EKeyRightArrow,lambda:move(1, 0))
c.bind(EKeyLeftArrow,lambda:move(-1, 0))
c.bind(EKeyUpArrow,lambda:move(0, -1))
c.bind(EKeyDownArrow,lambda:move(0, 1))
c.bind(EKeySelect, zoomin)
c.bind(EKeyStar, zoomout)

running = 1
def quit():
    global running
    running = 0
app.exit_key_handler= quit

draw()
while running:
    e32.ao_sleep(0.1)

I put a bit more explanation in Nokia's forum here. (Newer version is available)
http://discussion.forum.nokia.com/forum/showthread.php?s=&postid=153609
Also see the screenshot here
http://flickr.com/photos/korakot/30189624/
« Newer Snippets
Older Snippets »
Showing 11-12 of 12 total