-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpg2cityjson.py
259 lines (228 loc) · 6.53 KB
/
pg2cityjson.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
import psycopg2
import json
import configparser
from pathlib import Path
from cjio import cityjson
from cjio.models import CityObject, Geometry
config = configparser.ConfigParser()
config.read('config.ini')
host = config['DEFAULT']['host']
schema = config['DEFAULT']['schema']
table = config['DEFAULT']['table']
dbname = config['DEFAULT']['dbname']
user = config['DEFAULT']['user']
bbox = [111664.8,559448.0, 113415.9,560217.5]
cm = cityjson.CityJSON()
def simpletocjio(geom):
surface = geom.replace('LINESTRING Z (','',1).replace(')','').split(',')
s = []
for point in surface:
c = point.split(' ')
c = map(lambda v: float(v),c)
s.append(tuple(c))
return s
def runquery(query):
global cm
conn = psycopg2.connect(
host=host,
database=dbname,
user=user
)
cur = conn.cursor()
cur.execute("CREATE SEQUENCE IF NOT EXISTS counter;")
cur.execute(query)
rows = cur.fetchall()
conn.commit()
cur.close()
conn.close()
for row in rows:
bdry = [[]]
for g in row[4]:
bdry[0].append([simpletocjio(g)])
co = CityObject(
id=row[2]
)
co_attrs = {
'blockid': row[1],
'other_attribute': 'bla bla'
}
co.attributes = co_attrs
geom = Geometry(type='Solid', lod=1)
geom.boundaries = bdry
co.geometry.append(geom)
#-------------------
srf = {
0:{'surface_idx': [], 'type': 'WallSurface'},
1:{'surface_idx': [], 'type': 'GroundSurface'},
2:{'surface_idx': [], 'type': 'RoofSurface'}
}
for i,color in enumerate(row[3]):
if color == '#F8F8f8':
srf[0]['surface_idx'].append([0,i])
elif color == '#FF9823':
srf[2]['surface_idx'].append([0,i])
else:
srf[2]['surface_idx'].append([0,i])
geom.surfaces = srf
#-------------------------
mat = {
"irradiation": {
"values": [[]]
}
}
for i,color in enumerate(row[3]):
if color == '#F8F8f8':
mat['irradiation']['values'][0].append(1)
elif color == '#FF9823':
mat['irradiation']['values'][0].append(0)
else:
mat['irradiation']['values'][0].append(0)
geom.material = mat
#-------------------------
co.type = row[5]
cm.cityobjects[co.id] = co
cityobjects, vertex_lookup = cm.reference_geometry()
cm.add_to_j(cityobjects,vertex_lookup)
return 0
buildingquery = """
WITH sub AS (
SELECT gridid,
blockid,
id,
colors,
(ST_Dump(geom)).*
FROM results.extruded
WHERE ST_Intersects(geom_2d,ST_MakeEnvelope({},{},{},{},28992))
--LIMIT 100 --safety
)
SELECT
gridid,
blockid,
nextval('counter') as id,
colors,
array_agg(ST_AsText(ST_ExteriorRing(geom))) geom,
'Building' AS type
FROM sub
GROUP BY gridid,
blockid,
id,
colors
;
""".format(bbox[0],bbox[1],bbox[2],bbox[3])
runquery(buildingquery)
landquery = """
WITH sub AS (
SELECT gridid,
id as blockid,
id,
ARRAY['#000000'] as colors,
type,
geom
FROM results.bgtvlakkenz
WHERE ST_Intersects(geom,ST_MakeEnvelope({},{},{},{},28992))
--LIMIT 100 --safety
)
SELECT
gridid,
blockid,
nextval('counter') as id,
colors,
array_agg(ST_AsText(ST_Reverse(ST_ExteriorRing(geom)))) geom,
CASE
WHEN type IN ('onbegroeidterreindeel') THEN 'LandUse'
WHEN type IN ('begroeidterreindeel') THEN 'PlantCover'
WHEN type IN ('wegdeel','ondersteunendwegdeel') THEN 'Road'
WHEN type IN ('waterdeel','ondersteunendwaterdeel') THEN 'WaterBody'
END as type
FROM sub
GROUP BY gridid,
blockid,
id,
type,
colors
;
""".format(bbox[0],bbox[1],bbox[2],bbox[3])
runquery(landquery)
#ADd materials to cm.j.cityObjects.materials
arr = cm.j['CityObjects']
matarray = []
for o in arr:
#cm.j['CityObjects'][o]['geometry'][0]["material"] =
matarray.append({
"roof": {
"values": cm.j['CityObjects'][o]['geometry'][0]['semantics']['values']
}
})
#print(cm.j['CityObjects'][o])
#cm.j['CityObjects'][o]["material"] = {
# "roof": {
# "values": [[0, 0, 1, null]]
#"""
#TODO cm.j['CityObjects'][o]['semantics']['surfaces'] -> find values from:
#'semantics': {
# 'surfaces': [
# {'type': 'WallSurface'},
# {'type': 'GroundSurface'},
# {'type': 'RoofSurface'}
# ],
# 'values': [
# [2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
# ]
#}
#"""
# },
# "wall": {
# "values": [[2, 2, 1, null]]
# }
#}
cm.j["materials"] = [
{
"name": "blue",
"ambientIntensity": 0.2000,
"diffuseColor": [0.9000, 0.1000, 0.7500],
"emissiveColor": [0.9000, 0.1000, 0.7500],
"specularColor": [0.9000, 0.1000, 0.7500],
"shininess": 0.2,
"transparency": 0.5,
"isSmooth": False
},
{
"name": "red",
"ambientIntensity": 0.4000,
"diffuseColor": [0.1000, 0.1000, 0.9000],
"emissiveColor": [0.1000, 0.1000, 0.9000],
"specularColor": [0.9000, 0.1000, 0.7500],
"shininess": 0.0,
"transparency": 0.5,
"isSmooth": True
},
{
"name": "green",
"ambientIntensity": 0.2000,
"diffuseColor": [0.9000, 0.1000, 0.7500],
"emissiveColor": [0.9000, 0.1000, 0.7500],
"specularColor": [0.9000, 0.1000, 0.7500],
"shininess": 0.2,
"transparency": 0.5,
"isSmooth": False
}
]
print('Update bbox')
cm.update_bbox()
print('Validating')
outfile = 'mycitymodel.json'
#cityjson.save(cm, outfile)
cityobjects, vertex_lookup = cm.reference_geometry()
cm.add_to_j(cityobjects, vertex_lookup)
#Add materials
for mat in matarray:
cm.j['CityObjects'][o]['geometry'][0]["material"] = mat
cm.remove_duplicate_vertices()
cm.remove_orphan_vertices()
#cm.validate()
try:
with open(outfile, 'w') as fout:
json_str = json.dumps(cm.j, indent=2)
fout.write(json_str)
except IOError as e:
raise IOError('Invalid output file: %s \n%s' % (path, e))