Skip to content

Commit 8a4ccd9

Browse files
author
Tom MacWright
committed
Version 0.0.1, setup.py compatibility, another xy fix.
1 parent c2620a5 commit 8a4ccd9

File tree

6 files changed

+80
-31
lines changed

6 files changed

+80
-31
lines changed

CHANGES.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v0.0.1, March 14, 2011 -- Initial release

LICENSE.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Copyright (c), Development Seed
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without modification,
5+
are permitted provided that the following conditions are met:
6+
7+
- Redistributions of source code must retain the above copyright notice, this
8+
list of conditions and the following disclaimer.
9+
- Redistributions in binary form must reproduce the above copyright notice, this
10+
list of conditions and the following disclaimer in the documentation and/or
11+
other materials provided with the distribution.
12+
- Neither the name "Development Seed" nor the names of its contributors may be
13+
used to endorse or promote products derived from this software without
14+
specific prior written permission.
15+
16+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
20+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
23+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

MANIFEST.in

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include *.md

Makefile

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
test/tmp:
2+
mkdir test/tmp
3+
4+
test/data:
5+
mkdir test/data
6+
wget http://mapbox-tilesets.s3.amazonaws.com/mbtiles/World_Glass_1.1.mbtiles.zip -o test/data/World_Glass_1.1.mbtiles.zip
7+
unzip test/data/World_Glass_1.1.mbtiles.zip
8+
mv World_Glass_1.1.mbtiles test/data
9+
10+
test/tmp/world_glass:
11+
time ./mbutil.py test/data/World_Glass_1.1.mbtiles test/tmp/world_glass
12+
13+
test/tmp/world_glass_out.mbtiles:
14+
time ./mbutil.py test/tmp/world_glass test/tmp/world_glass_out.mbtiles
15+
16+
test: test/data test/tmp test/tmp/world_glass test/tmp/world_glass_out.mbtiles

mbutil.py

+23-31
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,10 @@ def mbtiles_connect(mbtiles_file):
3434
print e
3535
sys.exit(1)
3636

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""")
4241

4342
def compression_prepare(cur, con):
4443
cur.execute("""
@@ -53,14 +52,12 @@ def compression_prepare(cur, con):
5352
tile_row integer,
5453
tile_id VARCHAR(256));
5554
""")
56-
con.commit()
5755

58-
def optimize_database(con):
56+
def optimize_database(cur):
5957
print 'analyzing db'
60-
con.execute("""ANALYZE;""")
58+
cur.execute("""ANALYZE;""")
6159
print 'cleaning db'
62-
con.execute("""VACUUM;""")
63-
con.commit()
60+
cur.execute("""VACUUM;""")
6461

6562
def compression_do(cur, con, chunk):
6663
overlapping = 0
@@ -110,16 +107,14 @@ def compression_do(cur, con, chunk):
110107
print "insert into map: %s" % (time.time() - start)
111108
con.commit()
112109

113-
def compression_finalize(cur, con):
110+
def compression_finalize(cur):
114111
cur.execute("""drop table tiles;""")
115-
con.commit()
116112
cur.execute("""create view tiles as
117113
select map.zoom_level as zoom_level,
118114
map.tile_column as tile_column,
119115
map.tile_row as tile_row,
120116
images.tile_data as tile_data FROM
121117
map JOIN images on images.tile_id = map.tile_id;""")
122-
con.commit()
123118
cur.execute("""
124119
CREATE UNIQUE INDEX map_index on map
125120
(zoom_level, tile_column, tile_row);""")
@@ -128,47 +123,44 @@ def compression_finalize(cur, con):
128123
(tile_id);""")
129124
cur.execute("""vacuum;""")
130125
cur.execute("""analyze;""")
131-
con.commit()
132126

133127
def disk_to_mbtiles(directory_path, mbtiles_file):
134128
print "Importing disk to MBTiles"
135129
print "%s --> %s" % (directory_path, mbtiles_file)
136130
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)
140134
try:
141135
metadata = json.load(open('%s/metadata.json' % directory_path, 'r'))
142136
for name, value in metadata.items():
143-
con.execute('insert into metadata (name, value) values (?, ?)',
137+
cur.execute('insert into metadata (name, value) values (?, ?)',
144138
(name, value))
145-
con.commit()
146139
print 'metadata from metadata.json restored'
147140
except Exception, e:
148141
print e
149142
print 'metadata.json not found'
150143

151144
count = 0
145+
start_time = time.time()
152146
msg = ""
153147
for r1, zs, ignore in os.walk(directory_path):
154148
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,
161155
tile_row, tile_column, tile_data) values
162156
(?, ?, ?, ?);""",
163157
(z, x, y, sqlite3.Binary(f.read())))
164158
f.close()
165159
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)
172164
print 'tiles inserted.'
173165
optimize_database(con)
174166

setup.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from distutils.core import setup
2+
3+
setup(
4+
name='mbutil',
5+
version='0.0.1',
6+
author='Tom MacWright',
7+
author_email='[email protected]',
8+
packages=[],
9+
scripts=['mbutil.py'],
10+
url='https://github.com/mapbox/mbutil',
11+
license='LICENSE.md',
12+
description='An importer and exporter for MBTiles',
13+
long_description=open('README.md').read(),
14+
)

0 commit comments

Comments
 (0)