Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

adding converter script

  • Loading branch information...
commit 9c488acd10558a4da6ef3cb8ee60e903a7875460 1 parent 4450a45
@pbarry authored
Showing with 52 additions and 0 deletions.
  1. +52 −0 convert.py
View
52 convert.py
@@ -0,0 +1,52 @@
+import os
+import sys
+import sqlite3
+
+######################################
+# Helper functions
+
+def safeMakeDir(d):
+ if os.path.exists(d):
+ return
+ os.makedirs(d)
+
+def setDir(d):
+ safeMakeDir(d)
+ os.chdir(d)
+
+######################################
+# Let's do shit
+
+if not len(sys.argv) == 2:
+ print 'Please provide exactly 1 parameter - the mbtiles input filename'
+ exit()
+
+# Process input
+input_filename = sys.argv[1]
+dirname = input_filename[0:input_filename.index('.')]
+print 'Converting file "%s" into tiles in local directory "%s"' % (input_filename, dirname)
+
+# This will fail if there is already a directory.
+# I could make a better error message, but I intend for this to fail,
+# because it's better to not delete data.
+os.makedirs(dirname)
+
+# Database connection boilerplate
+connection = sqlite3.connect(input_filename)
+cursor = connection.cursor()
+
+# The mbtiles format helpfully provides a table that aggregates all necessary info
+cursor.execute('select * from tiles')
+
+os.chdir(dirname)
+for row in cursor:
+ setDir(str(row[0]))
+ setDir(str(row[1]))
+ # TODO: Should we support jpg too?
+ output_file = open(str(row[2]) + '.png', 'w')
+ output_file.write(row[3])
+ output_file.close()
+ os.chdir('..')
+ os.chdir('..')
+
+print 'Done!'
Please sign in to comment.
Something went wrong with that request. Please try again.