forked from gsi-cyberjapan/vector_tiles_convert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
193 lines (169 loc) · 5.68 KB
/
main.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
from clipping import main as clip
from os import listdir,walk,system,chdir,curdir,rmdir
from os.path import join,isdir,exists,abspath,dirname,normpath
import geojson
from utils import u_makedirs as makedirs, u_cp as cp, u_rm as rm, u_getfiles as getfiles, u_rmall as rmall
def get_geojson(path,zoom=None):
ret = getfiles(path,".geojson")
if zoom is not None:
ret = [x
for x in ret
if x.split("/")[1:2] == [zoom]]
return ret
def run(cmd,path,zoom,base,features,test,code,shpfile,mergedir):
if cmd == "add":
clip(zoom,path,base,features,test,code,shpfile)
elif cmd == "update":
ext = ".tmp"
code = clip(zoom,path,base+ext,features,test,code,shpfile)
diffs = get_diffs(base,base+ext,code,zoom)
rmall(base+ext)
rmdir(base+ext)
print "CHANGED:",len(diffs)
merge_diffs(base,mergedir,diffs)
else:
assert(cmd == "merge")
merge(base,mergedir,zoom)
def get_diffs(base1,base2,code,zoom):
old = get_geojson(join(base1,code),zoom)
tmp = get_geojson(join(base2,code),zoom)
changed = []
for x in tmp:
xtmp = join(base2,code,x)
xold = join(base1,code,x)
if x in old:
if open(xtmp).read() != open(xold).read():
changed.append(x)
cp(xtmp,xold)
else:
changed.append(x)
cp(xtmp,xold)
for x in old:
if x not in tmp:
xold = join(base1,code,x)
changed.append(x)
rm(xold)
return changed
def merge_diffs(base,out,diffs,prefix="M_"):
dirs = [x
for x in listdir(base)
if isdir(join(base,x)) and not x.startswith(prefix)]
M = {}
for x in diffs:
M[x] = []
for code in dirs:
path = join(base,code,x)
if exists(path):
M[x].append(code)
merge_json(M,base,prefix+out)
def merge(base,out,zoom,prefix="M_"):
dirs = [x
for x in listdir(base)
if isdir(join(base,x)) and not x.startswith(prefix)]
M = {}
for code in dirs:
path = join(base,code)
for y in get_geojson(path,zoom):
M.setdefault(y,[]).append(code)
merge_json(M,base,prefix+out)
def merge_json(M,base,out):
print "BO",base,out
if True:
NEWM = {}
check = set()
for path in M:
if not M[path]:
ps = path.split("/")
newpath = "/".join(ps[1:])
check.add(newpath)
for code in M[path]:
ps = path.split("/")
newpath = "/".join(ps[1:])
newcode = code+"/"+ps[0]
NEWM.setdefault(newpath,[]).append(newcode)
for x in check:
if x not in NEWM:
NEWM[x] = []
M = NEWM
for k in M:
new = join(base,out,k)
makedirs(dirname(new))
if len(M[k]) == 0:
rm(new)
elif len(M[k]) == 1:
old = join(base,M[k][0],k)
cp(old,new)
else:
print "K",k,"MK",M[k][:10]
gs = [(x,geojson.load(open(join(base,x,k)))) for x in M[k]]
x0,g = gs[0]
for x,h in gs[1:]:
print "merging",x0,x
g["features"] += h["features"]
f = open(new,"w")
geojson.dump(g,f)
f.close()
def help(name,ret):
print "usage:"
print name,"[-h]"
print " this help message"
print name,"(add path | update path [-m mergedir] | merge [-m mergedir]) [-b base] [-c code] [-f features] [-z zoom] [-s]"
print "where"
print "path => path to gml/shp data for add/update; mergedir otherwise "
print "base => path to root directory (default '.')"
print "mergedir => mergedir (used by merge and update)"
print "code => date code (only needed for irregular filenames)"
print "feature => comma-separated list of features, e.g AdmArea,... (default all)"
print "zoom => zoom factor (default 18)"
print "use option '-s' when adding shapefiles"
exit(ret)
if __name__ == "__main__":
from sys import argv,exit
base = "."
code = None
features = []
zoom = 18
test = False
shpfile = False
mergedir = None
path = None
n = len(argv)
if n == 1:
help(argv[0],1)
elif n == 2 and argv[1] == "-h":
help(argv[0],0)
else:
cmd = argv[1]
try:
cmd = argv[1]
assert(cmd in ("add","update","merge"))
if cmd == "merge":
off = 0
else:
off = 1
path = argv[2]
for i,opt in enumerate(argv[2+off:]):
if opt == "-b":
base = argv[2+off+i+1]
elif opt == "-m":
mergedir = argv[2+off+i+1]
elif opt == "-c":
code = argv[2+off+i+1]
elif opt == "-f":
features = argv[2+off+i+1].split(",")
elif opt == "-z":
zoom = int(argv[2+off+i+1])
elif opt == "-t":
test = True
elif opt == "-s":
shpfile = True
if exists(base):
assert(isdir(base))
if cmd != "add":
assert(mergedir is not None)
assert(path is None or exists(path))
assert(10 <= zoom and zoom <= 24)
except:
raise
help(argv[0],1)
run(cmd=cmd,path=path,zoom=str(zoom),code=code,base=base,features=features,test=test,shpfile=shpfile,mergedir=mergedir)