-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
300 lines (260 loc) · 8.89 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import sys
import os
import fiona
from geomtypes import Point3d
from shapely.geometry import Polygon
from shapely.geometry import asShape
from shapely.geometry import mapping
from shapely.ops import polygonize
from shapely.ops import cascaded_union
import subprocess
# INFILE = '/Users/hugo/data/3dtop10nl/25ez1.gdb'
INFILE = '/Users/hugo/data/3dtop10nl/37ez2.gdb'
BLAYERS = ['gebouw_3D_LOD1']
TRLAYERS = ['terreinVlak_3D_LOD0', 'wegdeelVlak_3D_LOD0', 'waterdeelVlak_3D_LOD0']
REPAIR = True #-- automatic repair when possible
MERGEFEATURES = True #-- merging MultiPolygons based on 'TOP10_NL'
###############################################################################
def main():
print "Input file", INFILE
# validate_buildings()
validate_triangulation()
# # -- Find and print the number of geometries
# print "# MultiPolygons:", len(dPolys)
# totaltr = 0
# for tid in dPolys:
# for each in dPolys[tid]:
# totaltr += len(each)
# print "# triangles:", totaltr
# validate_triangles(dPolys)
# vertical_triangles(dPolys)
# validate_regions(dPolys)
def read_input_layer(l):
c = fiona.open(INFILE, 'r', driver='OpenFileGDB', layer=l)
#-- store the geometries in a list (shapely objects)
dPolys = {}
for gid, each in enumerate(c):
if MERGEFEATURES is True:
k = each['properties']['TOP10_ID']
else:
k = gid
if k not in dPolys:
dPolys[k] = [asShape(each['geometry'])]
else:
dPolys[k].append(asShape(each['geometry']))
return dPolys
def validate_triangulation():
for each in TRLAYERS:
print "Processing layer", each
dPolys = read_input_layer(each)
# -- Find and print the number of geometries
print "# MultiPolygons:", len(dPolys)
totaltr = 0
for tid in dPolys:
for each in dPolys[tid]:
totaltr += len(each)
print "# triangles:", totaltr
validate_triangles(dPolys)
vertical_triangles(dPolys)
validate_regions(dPolys)
def validate_buildings():
dPolys = read_input_layer(BLAYERS[0])
invalidbuildings = 0
invalidlist = []
for tid in dPolys:
print "----------- Validate #%s ----------" % (tid)
if (validate_one_building(dPolys[tid]) == False):
invalidbuildings += 1
invalidlist.append(tid)
print "\n"
print "=" * 40
print "# invalid building(s): %d/%d" % (invalidbuildings, len(dPolys))
# print invalidlist
def validate_one_building(mp):
isValid = True
lsNodes = []
lsFaces = []
for each in mp:
for p in each:
idpoly = []
for v in list(p.exterior.coords):
temp = Point3d(v[0], v[1], v[2])
if lsNodes.count(temp) == 0:
lsNodes.append(temp)
temp.id = len(lsNodes) - 1
idpoly.append(temp.id)
else:
idpoly.append(lsNodes.index(temp))
lsFaces.append(idpoly)
#-- write POLY file
tmp = []
tmp.append("%d 3 0 0" % (len(lsNodes)))
for i, n in enumerate(lsNodes):
tmp.append("%d %f %f %f" % (i, n[0], n[1], n[2]))
tmp.append("%d 0" % len(lsFaces))
for i, f in enumerate(lsFaces):
tmp.append("1 0")
tmp.append("%d %s" % (len(f) -1, " ".join(map(str, f[:-1]))))
tmp.append("0")
tmp.append("0")
polystr = "\n".join(tmp)
fOut = open("/Users/hugo/temp/0.poly", 'w')
fOut.write(polystr)
fOut.close()
#-- call val3dity binary
exampleerrors = []
str1 = "/Users/hugo/projects/val3dity/val3dity --xml -i /Users/hugo/temp/0.poly"
op = subprocess.Popen(str1.split(' '),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
R = op.poll()
if R:
res = op.communicate()
raise ValueError(res[1])
o = op.communicate()[0]
if o.find('ERROR') != -1:
i = o.find('<errorCode>')
while (i != -1):
if exampleerrors.count(o[i+11:i+14]) == 0:
exampleerrors.append(o[i+11:i+14])
tmp = o[i+1:].find('<errorCode>')
if tmp == -1:
i = -1
else:
i = tmp + i + 1
print "ERROR: %s" % ("--".join(map(str, exampleerrors)))
isValid = False
return isValid
def vertical_triangles(dPolys):
verticaltr = 0
for tid in dPolys:
for mp in dPolys[tid]:
for p in mp:
if (p.area == 0.0): #-- since shapely's area() ignores Z values
verticaltr += 1
print "# vertical triangle(s):", verticaltr
def validate_triangles(dPolys):
#-- validate individually each triangle
invalidtr = 0
for tid in dPolys:
for mp in dPolys[tid]:
for p in mp:
if (validate_one_triangle(p) == False):
invalidtr += 1
print "# invalid triangle(s):", invalidtr
def validate_regions(dPolys):
#-- validate individually each top10 polygon
invalidmp = 0
invalidlist = []
# toprocess = range(100)
# toprocess = ['125232222', '125231986', '124798131']
# toprocess = ['117116312', '125230993', '117198224']
# for tid in toprocess:
for tid in dPolys:
print "----------- Validate #%s ----------" % (tid)
if (validate_one_region(dPolys[tid]) == False):
invalidmp += 1
invalidlist.append(tid)
print "\n"
print "=" * 40
print "# invalid region(s):", invalidmp
print invalidlist
def validate_one_triangle(tr):
if len(list(tr.exterior.coords)) != 4:
return False
if len(tr.interiors) != 0:
return False
return True
def validate_one_region(lsmp):
isValid = True
lsNodes = []
lsTr = []
vertical = False
for mp in lsmp:
for p in mp:
tr = []
for i in range(3):
temp = Point3d(p.exterior.coords[i][0], p.exterior.coords[i][1], p.exterior.coords[i][2])
if lsNodes.count(temp) == 0:
lsNodes.append(temp)
temp.id = len(lsNodes) - 1
tr.append(temp.id)
else:
tr.append(lsNodes.index(temp))
if (p.area == 0.0):
vertical = True
# if REPAIR == True: #-- swap orientation of vertical triangles
# if (p.area == 0.0):
# tr[0],tr[1] = tr[1], tr[0]
lsTr.append(tr)
# if vertical == True:
# print '*************'
if REPAIR == True:
lsTr = remove_duplicate_triangles(lsTr)
# print lsNodes
# print lsTr
# #-- fuck up the lsTr for testing purposes
# b = [ lsTr[4][0], lsTr[4][2], lsTr[4][1] ]
# lsTr.append(b)
d = {}
for tr in lsTr:
pairs = ( (0, 1), (1, 2), (2, 0) )
for each in pairs:
a = tr[each[0]]
b = tr[each[1]]
if (a < b): #-- good order
k = str(a) + "-" + str(b)
if k in d:
if d[k] == -1:
d[k] = 0
else:
print "ERROR: duplicate edge (%s)" % k
print lsNodes[a]
print lsNodes[b]
isValid = False
d[k] = 999
else:
d[k] = 1
else: #-- reverse order
k = str(b) + "-" + str(a)
# print k
if k in d:
# print d[k]
if d[k] == 1:
d[k] = 0
else:
print "ERROR: duplicate edge (%s)" % k
print lsNodes[b]
print lsNodes[a]
isValid = False
d[k] = 999
else:
d[k] = -1
tmp = []
for k in d:
if ( (d[k] == -1) or (d[k] == 1) ):
tmp.append(k)
boundaryedges = []
for each in tmp:
t = each.split('-')
boundaryedges.append( [[lsNodes[int(t[0])][0], lsNodes[int(t[0])][1]], [lsNodes[int(t[1])][0], lsNodes[int(t[1])][1]]] )
polygons = list(polygonize(boundaryedges))
if len(polygons) > 1:
re = cascaded_union(polygons)
if re.geom_type != 'Polygon':
print "ERROR: disconnected area"
isValid = False
return isValid
def remove_duplicate_triangles(lsTr):
allduplicated = True
for i in range(0, len(lsTr), 2):
if lsTr[i] != lsTr[i + 1]:
allduplicated = False
if allduplicated == False:
print "ERROR: triangles are NOT duplicated :/"
return lsTr
else:
# print "ERROR: all triangles are duplicated. Will fix that automatically to continue..."
return lsTr[0:len(lsTr):2]
if __name__ == '__main__':
main()