@@ -34,11 +34,10 @@ def mbtiles_connect(mbtiles_file):
34
34
print e
35
35
sys .exit (1 )
36
36
37
- def optimize_connection (con ):
38
- con .execute ("""PRAGMA synchronous=0""" )
39
- con .execute ("""PRAGMA locking_mode=EXCLUSIVE""" )
40
- con .execute ("""PRAGMA journal_mode=TRUNCATE""" )
41
- con .commit ()
37
+ def optimize_connection (cur ):
38
+ cur .execute ("""PRAGMA synchronous=0""" )
39
+ cur .execute ("""PRAGMA locking_mode=EXCLUSIVE""" )
40
+ cur .execute ("""PRAGMA journal_mode=TRUNCATE""" )
42
41
43
42
def compression_prepare (cur , con ):
44
43
cur .execute ("""
@@ -53,14 +52,12 @@ def compression_prepare(cur, con):
53
52
tile_row integer,
54
53
tile_id VARCHAR(256));
55
54
""" )
56
- con .commit ()
57
55
58
- def optimize_database (con ):
56
+ def optimize_database (cur ):
59
57
print 'analyzing db'
60
- con .execute ("""ANALYZE;""" )
58
+ cur .execute ("""ANALYZE;""" )
61
59
print 'cleaning db'
62
- con .execute ("""VACUUM;""" )
63
- con .commit ()
60
+ cur .execute ("""VACUUM;""" )
64
61
65
62
def compression_do (cur , con , chunk ):
66
63
overlapping = 0
@@ -110,16 +107,14 @@ def compression_do(cur, con, chunk):
110
107
print "insert into map: %s" % (time .time () - start )
111
108
con .commit ()
112
109
113
- def compression_finalize (cur , con ):
110
+ def compression_finalize (cur ):
114
111
cur .execute ("""drop table tiles;""" )
115
- con .commit ()
116
112
cur .execute ("""create view tiles as
117
113
select map.zoom_level as zoom_level,
118
114
map.tile_column as tile_column,
119
115
map.tile_row as tile_row,
120
116
images.tile_data as tile_data FROM
121
117
map JOIN images on images.tile_id = map.tile_id;""" )
122
- con .commit ()
123
118
cur .execute ("""
124
119
CREATE UNIQUE INDEX map_index on map
125
120
(zoom_level, tile_column, tile_row);""" )
@@ -128,47 +123,44 @@ def compression_finalize(cur, con):
128
123
(tile_id);""" )
129
124
cur .execute ("""vacuum;""" )
130
125
cur .execute ("""analyze;""" )
131
- con .commit ()
132
126
133
127
def disk_to_mbtiles (directory_path , mbtiles_file ):
134
128
print "Importing disk to MBTiles"
135
129
print "%s --> %s" % (directory_path , mbtiles_file )
136
130
con = mbtiles_connect (mbtiles_file )
137
- optimize_connection ( con )
138
- mbtiles_setup ( con . cursor () )
139
- con . commit ( )
131
+ cur = con . cursor ( )
132
+ optimize_connection ( cur )
133
+ mbtiles_setup ( cur )
140
134
try :
141
135
metadata = json .load (open ('%s/metadata.json' % directory_path , 'r' ))
142
136
for name , value in metadata .items ():
143
- con .execute ('insert into metadata (name, value) values (?, ?)' ,
137
+ cur .execute ('insert into metadata (name, value) values (?, ?)' ,
144
138
(name , value ))
145
- con .commit ()
146
139
print 'metadata from metadata.json restored'
147
140
except Exception , e :
148
141
print e
149
142
print 'metadata.json not found'
150
143
151
144
count = 0
145
+ start_time = time .time ()
152
146
msg = ""
153
147
for r1 , zs , ignore in os .walk (directory_path ):
154
148
for z in zs :
155
- for r2 , ys , ignore in os .walk (os .path .join (r1 , z )):
156
- for y in ys :
157
- for r2 , ignore , xs in os .walk (os .path .join (r1 , z , y )):
158
- for x in xs :
159
- f = open (os .path .join (r1 , z , y , x ), 'r ' )
160
- con .execute ("""insert into tiles (zoom_level,
149
+ for r2 , xs , ignore in os .walk (os .path .join (r1 , z )):
150
+ for x in xs :
151
+ for r2 , ignore , ys in os .walk (os .path .join (r1 , z , x )):
152
+ for y in ys :
153
+ f = open (os .path .join (r1 , z , x , y ), 'rb ' )
154
+ cur .execute ("""insert into tiles (zoom_level,
161
155
tile_row, tile_column, tile_data) values
162
156
(?, ?, ?, ?);""" ,
163
157
(z , x , y , sqlite3 .Binary (f .read ())))
164
158
f .close ()
165
159
count = count + 1
166
- for c in msg : sys .stdout .write (chr (8 ))
167
- msg = "%s tiles inserted" % count
168
- sys .stdout .write (msg )
169
- if (count % 5000 ):
170
- con .commit ()
171
- con .commit ()
160
+ if (count % 100 ) == 0 :
161
+ for c in msg : sys .stdout .write (chr (8 ))
162
+ msg = "%s tiles inserted (%d tiles/sec)" % (count , count / (time .time () - start_time ))
163
+ sys .stdout .write (msg )
172
164
print 'tiles inserted.'
173
165
optimize_database (con )
174
166
0 commit comments