вторник, 13 октября 2009 г.

Python script to generate an rmaps-compatible sqlitedb

7:40
Yoav:
Hi. I wrote a small python script to generate an rmaps-compatible sqlitedb from a directory of tiles. Runs in linux (as opposed to the java-based solution) and takes less memory so it's suitable for large databases.
7:41
I used it to generate a topo map of the entire state of Israel. Works well. Anyway, if you have a need for such script, let me know.
0:15
Yoav:
Here's the script:

buildb.py:
#!/usr/bin/env python
# buildb.py - build an sqlitedb for rmaps from a tree of map tiles.
# (Yoav Weiss)
#
# Syntax: buildb.py
# should contain a tree of //.png

import sqlite3,sys,os

#minzoom=100
#maxzoom=0

db=sqlite3.connect(sys.argv[1])
try:
db.execute("CREATE TABLE tiles (x int, y int, z int, s int, image blob, PRIMARY KEY (x,y,z,s));")
except:
pass

for z in os.listdir(sys.argv[2]):
#if int(z) > maxzoom:
#maxzoom = int(z)
#if int(z) < minzoom:
#minzoom = int(z)
for x in os.listdir(sys.argv[2]+'/'+z):
for y in os.listdir(sys.argv[2]+'/'+z+'/'+x):
db.execute('INSERT INTO "tiles" VALUES(?,?,?,?,?);',(int(x),int(y.split('.')[0]),17-int(z),0,buffer(file("%s/%s/%s/%s" % (sys.argv[2],z,x,y)).read())))

#db.execute("CREATE TABLE info(minzoom,maxzoom);")
#db.execute('INSERT INTO "info" VALUES(?,?);',(minzoom,maxzoom))
try:
db.execute("CREATE TABLE android_metadata (locale TEXT);")
db.execute('INSERT INTO "android_metadata" VALUES("en");')
except:
pass
try:
db.execute('CREATE INDEX IND on tiles (x,y,z,s);')
except:
pass
db.commit();