I would appreciate any help or comment.
The idea is
to create a server in python that serves maps on the internet. The maps have to
be in MBTiles format, which is a SQLite database that store all the map tiles
in a single file. Taking this as an example
http://a.tiles.mapbox.com/v3/mapbox.geography-class/page.html#4.00/36.74/28.30
The tiles are images of the
map in png, like this one
http://a.tiles.mapbox.com/v3/mapbox.geography-class/4/7/7.png and apart from
the tiles, in the
database is stored the UTFGrid information, like this file
http://a.tiles.mapbox.com/v3/mapbox.geography-class/4/7/7.grid.json. The
UTFGrid (you can consult in http://www.mapbox.com/developers/utfgrid/) permits
than when you hover in the
map, some extra information appears referring to the point where the mouse is.
As you can see in this example
http://a.tiles.mapbox.com/v3/mapbox.geography-class/page.html#4.00/36.74/28.30
a infobox appears with the
flag of every country(which is the information stored in the UTFGrid file).
In the
MBTiles database there are two table (also other, but not important in this
case):
“tiles”, where are stored the tiles; and “grids”, where is stored the UTFGrid
associated to every tile.
>From the
MBTiles I can extract the tiles and display a normal map, and I can also
extract the UTFGrid file individually (not all together, like the tiles that I
can see the whole map; with the UTFGrid I just get one file). When I show the
map normally, the infoboxes do not appear. But I do not get any error in
command line or in the website. It just like the UTFGrid is empty.
Attached is
the code I am using to access the MBtiles file.
I am using
this SQL to access the MBTiles file
“select
grid from grids where tile_column=? and tile_row=? and zoom_level=?", (x,
y, z)
And if I
change it for this
"select
grid from grids where tile_column=? and tile_row=? and zoom_level=?", (67,
84, 7)
I am
getting always the same UTFGrid, but in this case it shows the infoboxes on the
map, that one for all the tiles.
It is like
if I have this part of the map
http://a.tiles.mapbox.com/v3/mapbox.geography-class/page.html#4.00/10.14/17.31
but I always get the
infoboxes of this tile
http://a.tiles.mapbox.com/v3/mapbox.geography-class/4/7/7.png I get the
infoboxes in all the
tiles, but all of them are showing the flag of that tile.
Thanks,Carmen
import sqlite3
import json
import zlib
def get_tile(layer, x, y, z, ext):
try:
# Connect to the database and get the cursor
db = sqlite3.connect("data/%s.mbtiles" % layer)
c = db.cursor()
#closing(db.cursor)
except:
# return None
# In case the connection can not be done
start_response('404 Not found', [('Content-Type', 'text/plain')])
return ["Not found: %s.mbtiles" % (layer,)]
# Get the tiles from the database, using the zoom and the coordinates we got previously
c.execute("select tile_data from tiles where tile_column=? and tile_row=? and zoom_level=?", (x, y, z))
res = c.fetchone()
if res:
# In case there are tiles, print them with their necesary headers
get_grid(layer, x, y, z)
return bytes(res[0])
return None
def get_grid(layer, x, y, z): #, ext):
print "accede a grid"
# Connect to the database and get the cursor
try:
db = sqlite3.connect("data/%s.mbtiles" % layer)
c1 = db.cursor()
c2 = db.cursor()
#closing(db.cursor)
except:
# In case the connection can not be done
start_response('404 Not found', [('Content-Type', 'text/plain')])
return ["Not found: %s.mbtiles" % (layer,)]
# Get the utfgrid info from the database, using the zoom and the coordinates we got previously
c1.execute("select grid from grids where tile_column=? and tile_row=? and zoom_level=?", (67, 84, 7)) #(31, 39, 6)) #
#c1.execute("select * from grids")
row = c1.fetchone()
if not row:
print "NO ROW"
return None
print "ROW"
bts = bytes(row[0])
# Decompresses the data in string, returning a string containing the uncompressed data.
files = zlib.decompress(bts)
# Deserialize files to a Python object -> http://docs.python.org/2/library/json.html#json-to-py-table
jsonfiles = json.loads(files)
#return jsonfiles
# Get the data
keys = []
#for keyrow in c2.execute("select key_name as key, key_json as json from grid_data where zoom_level=? and tile_column=? and tile_row=?", (z, x, y)):
for keyrow in c2.execute("SELECT keymap.key_name AS key_name, keymap.key_json AS key_json FROM map JOIN grid_utfgrid ON grid_utfgrid.grid_id = map.grid_id JOIN grid_key ON grid_key.grid_id = map.grid_id JOIN keymap ON grid_key.key_name = keymap.key_name WHERE tile_column=? and tile_row=? and zoom_level=?", (67, 84, 7)): #(31, 39, 6)): #(x, y, z)):
keyname, keydata = keyrow
keys.append((keyname, eval(keydata)))
datadict = dict(keys)
jsonfiles[u'data'] = datadict
# return jsonfiles
print "okey Z:" + z + "x,y" + x + y
# Serialize jsonfiles to a JSON formatted string using -> http://docs.python.org/2/library/json.html#py-to-json-table
res = json.dumps(jsonfiles)
# return res
# Wrapped in a function to make it compatible with Wax
sol = "grid(%s)" % res
return sol
--
http://mail.python.org/mailman/listinfo/python-list