-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmergetiles.py
121 lines (93 loc) · 2.93 KB
/
mergetiles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# -*- coding: utf-8 -*-
#con.execute_non_query(INSERT_EX_SQ.encode('your language encoder'))
#
# MBTile Tile Merge tool
#
# USAGE:python mergetiles -src src.mbtiles -dest dest.mbtiles -zoom 17,18,19
#
from os import getenv
import sys
import math
import sqlite3
def Usage():
print('Usage: mergetiles.py ')
print(' [-src src_mbtiles_file]')
print(' [-dest dest_mbtiles_file]')
print(' [-zoom zoom_level]')
def num2deg(xtile, ytile, zoom):
n = 2.0 ** zoom
lon_deg = xtile / n * 360.0 - 180.0
lat_rad = math.atan(math.sinh(math.pi * (1 - 2 * ytile / n)))
lat_deg = math.degrees(lat_rad)
return (-lat_deg, lon_deg)
srctablename=None
desttablename=None
zoom_level=[]
argv=sys.argv
i = 1
while i < len(argv):
arg = argv[i]
if arg == '-src':
i+=1
srctablename = argv[i]
elif arg == '-dest':
i+=1
desttablename = argv[i]
elif arg == '-zoom':
i+=1
arg=argv[i].split(",")
zoom_level=map(int,arg)
elif arg[:1] == '-':
Usage()
sys.exit(0)
else:
i+=1
print srctablename
print desttablename
print zoom_level
if srctablename == None or desttablename == None or len(zoom_level) < 1:
Usage()
sys.exit(0)
try :
conn1 = sqlite3.connect(srctablename)
cursor1 = conn1.cursor()
conn2 = sqlite3.connect(desttablename)
cursor2 = conn2.cursor()
# src から dest へタイルインサート
for src_zoom_level in zoom_level:
sqltext = u"SELECT * FROM tiles where zoom_level = " + str(src_zoom_level)
cursor1.execute(sqltext)
row = cursor1.fetchone()
while row:
print row[0],row[1],row[2]
cursor2.execute('''INSERT OR REPLACE INTO tiles(zoom_level, tile_column, tile_row, tile_data) VALUES (?,?,?,?);''',(row[0],row[1],row[2],buffer(row[3])))
row = cursor1.fetchone()
conn2.commit()
#dest の最大レベルのエクステントを作成する
cursor2.execute("select min(zoom_level),max(zoom_level) from tiles")
row = cursor2.fetchone()
print row
minzoom = min(row[0],min(zoom_level))
maxzoom = max(row[1],max(zoom_level))
print minzoom,maxzoom
cursor2.execute("select min(tile_column), min(tile_row), max(tile_column),max(tile_row) from tiles where zoom_level=%i" % maxzoom)
row = cursor2.fetchone()
print row[0],row[1],row[2],row[3]
lonlat = num2deg(row[0],row[1],maxzoom)
print lonlat
lonlat2 = num2deg(row[2]+1,row[3]+1,maxzoom)
print lonlat2
s= str(lonlat[1])+','+str(lonlat[0])+','+str(lonlat2[1])+','+str(lonlat2[0])
print s
cursor2.execute('''INSERT OR REPLACE INTO metadata(name, value) VALUES (?,?);''',('minZoom',minzoom))
cursor2.execute('''INSERT OR REPLACE INTO metadata(name, value) VALUES (?,?);''',('maxZoom',maxzoom))
cursor2.execute('''INSERT OR REPLACE INTO metadata(name, value) VALUES (?,?);''',('bounds',s))
conn2.commit()
cursor2.execute("""ANALYZE;""")
cursor2.execute("""VACUUM;""")
conn2.close()
conn1.close()
sys.exit(0)
except Exception,err:
print err
sys.exit(1)