forked from Amsterdam-AI-Team/Urban_PointCloud_Processing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_utils.py
292 lines (244 loc) · 10.4 KB
/
plot_utils.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
"""Visualisation utilities."""
import numpy as np
import matplotlib.colors as mcolors
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from shapely.geometry import Polygon
from shapely.ops import cascaded_union
from ..utils import clip_utils
from ..utils import las_utils
from ..utils import bgt_utils
from ..utils.labels import Labels
bgt_labels = {'boom': 'Tree',
'lichtmast': 'Lamp post',
'verkeersbord': 'Traffic sign',
'pand': 'Building',
'wegdeel': 'Road'}
bgt_colors = {'boom': 'green',
'lichtmast': 'orange',
'verkeersbord': 'darkviolet',
'pand': 'lightblue',
'pand_poly': 'royalblue',
'wegdeel': 'lightgrey'}
cloud_colors = {'Unlabelled': 'lightgrey',
'Ground': 'peru',
'Building': 'lightblue',
'Tree': 'green',
'Street light': 'orange',
'Traffic sign': 'darkviolet',
'Traffic light': 'red',
'Car': 'grey',
'Noise': 'whitesmoke'}
def plot_cloud_slice(las_file, ahn_reader, plane_height=1.5, hide_noise=False,
ax=None, title=None, legend_below=False):
full_plot = False
if ax is None:
fig, ax = plt.subplots(1, constrained_layout=True)
full_plot = True
las = las_utils.read_las(las_file)
labels = las.label
points = np.vstack((las.x, las.y, las.z)).T
tilecode = las_utils.get_tilecode_from_filename(las_file)
((x_min, y_max), (x_max, y_min)) =\
las_utils.get_bbox_from_tile_code(tilecode)
points_z = ahn_reader.interpolate(tilecode, points,
np.ones((len(points),), dtype=bool),
'ground_surface')
plane_mask = ((points[:, 2] >= points_z + plane_height - 0.05)
& (points[:, 2] <= points_z + plane_height + 0.05))
label_set = np.unique(labels[plane_mask])
for label in label_set:
if label == Labels.NOISE and hide_noise:
continue
label_mask = plane_mask & (labels == label)
label_str = Labels.get_str(label)
ax.scatter(points[label_mask, 0], points[label_mask, 1],
c=cloud_colors[label_str], marker='.', label=label_str)
box = patches.Rectangle((x_min, y_min), x_max-x_min, y_max-y_min,
linewidth=1, linestyle='--', edgecolor='black',
fill=False)
ax.add_patch(box)
if title is not None:
ax.set_title(title)
ax.set_xlabel('X')
ax.set_xticks(range(x_min, x_max+1, 10))
ax.set_xticklabels(range(x_min, x_max+1, 10))
ax.set_ylabel('Y')
ax.set_yticks(range(y_min, y_max+1, 10))
ax.set_yticklabels(range(y_min, y_max+1, 10))
if not legend_below:
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
else:
ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.15), ncol=3)
ax.set_aspect('equal', adjustable='box')
if full_plot:
plt.show()
def plot_bgt(tilecode, building_file=None, road_file=None, point_file=None,
ax=None, title=None, show_legend=True, legend_below=False):
padding = 2.5
full_plot = False
if ax is None:
width = 7 if show_legend else 5
fig, ax = plt.subplots(1, figsize=(width, 5), constrained_layout=True)
full_plot = True
if title is None:
title = f'BGT tile {tilecode}'
buildings = []
roads = []
points = []
if building_file:
buildings = bgt_utils.get_polygons(building_file, tilecode)
if road_file:
roads = bgt_utils.get_polygons(road_file, tilecode)
if point_file:
points = bgt_utils.get_points(point_file, tilecode, padding=padding)
((x_min, y_max), (x_max, y_min)) =\
las_utils.get_bbox_from_tile_code(tilecode)
for poly in [Polygon(bld) for bld in buildings]:
x, y = poly.exterior.xy
ax.fill(x, y, c=bgt_colors['pand'], label=bgt_labels['pand'])
ax.plot(x, y, c=bgt_colors['pand_poly'])
for poly in [Polygon(rd) for rd in roads]:
x, y = poly.exterior.xy
ax.fill(x, y, c=bgt_colors['wegdeel'], label=bgt_labels['wegdeel'])
for pt in points:
ax.scatter(pt[1], pt[2],
c=bgt_colors[pt[0]], marker='x', label=bgt_labels[pt[0]])
box = patches.Rectangle((x_min, y_min), x_max-x_min, y_max-y_min,
linewidth=1, linestyle='--', edgecolor='black',
fill=False)
ax.add_patch(box)
ax.set_title(title)
ax.set_xlabel('X')
ax.set_ylabel('Y')
if show_legend:
handles, labels = ax.get_legend_handles_labels()
by_label = dict(zip(labels, handles))
if not legend_below:
ax.legend(by_label.values(), by_label.keys(),
loc='center left', bbox_to_anchor=(1, 0.5))
else:
ax.legend(by_label.values(), by_label.keys(),
loc='upper center', bbox_to_anchor=(0.5, -0.15), ncol=3)
ax.set_xticks(range(x_min, x_max+1, 10))
ax.set_xticklabels(range(x_min, x_max+1, 10))
ax.set_yticks(range(y_min, y_max+1, 10))
ax.set_yticklabels(range(y_min, y_max+1, 10))
ax.set_xlim((x_min - padding, x_max + padding))
ax.set_ylim((y_min - padding, y_max + padding))
ax.set_aspect('equal', adjustable='box')
if full_plot:
plt.show()
def plot_bgt_and_cloudslice(tilecode, las_file, ahn_reader,
building_file=None, road_file=None,
point_file=None, plane_height=1.5,
hide_noise=False):
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5.5))
plot_bgt(tilecode, building_file, road_file, point_file,
title='BGT data', ax=ax1, legend_below=True)
plot_cloud_slice(las_file, ahn_reader, plane_height=plane_height,
hide_noise=hide_noise, title='LAS labels', ax=ax2,
legend_below=True)
ax2.set_yticklabels([])
ax2.yaxis.label.set_visible(False)
fig.suptitle(f'Tile {tilecode}', fontsize=14)
fig.subplots_adjust(top=0.95)
plt.show()
def plot_ahn_surface(ahn_tile, surf='ground_surface', ax=None, cbar_pad=0.04):
cmap = {'ground_surface': 'gist_earth',
'building_surface': 'Oranges'}
cbar_title = {'ground_surface': 'Ground elevation (m)',
'building_surface': 'Building height (m)'}
full_plot = False
if ax is None:
full_plot = True
fig, ax = plt.subplots(1)
x_min = int(ahn_tile['x'][0])
x_max = int(np.ceil(ahn_tile['x'][-1]))
y_min = int(ahn_tile['y'][-1])
y_max = int(np.ceil(ahn_tile['y'][0]))
im = ax.imshow(ahn_tile[surf], extent=[x_min, x_max, y_min, y_max],
interpolation='none', cmap=cmap[surf])
ax.set_title(surf)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_xticks(range(x_min, x_max+1, 10))
ax.set_xticklabels(range(x_min, x_max+1, 10))
ax.set_yticks(range(y_min, y_max+1, 10))
ax.set_yticklabels(range(y_min, y_max+1, 10))
cbar = plt.colorbar(im, ax=[ax], fraction=0.046, pad=cbar_pad)
cbar.ax.set_ylabel(cbar_title[surf], rotation=270, labelpad=10)
if full_plot:
plt.show()
def plot_ahn_sidebyside(tilecode, ahn_reader):
ahn_tile = ahn_reader.filter_tile(tilecode)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5))
plot_ahn_surface(ahn_tile, surf='ground_surface', ax=ax1)
plot_ahn_surface(ahn_tile, surf='building_surface', ax=ax2)
ax2.set_yticklabels([])
ax2.yaxis.label.set_visible(False)
fig.suptitle(f'AHN tile {tilecode}', fontsize=14)
plt.show()
def plot_ahn_merged(tilecode, ahn_reader):
ahn_tile = ahn_reader.filter_tile(tilecode)
fig, ax1 = plt.subplots(1, figsize=(7, 5))
plot_ahn_surface(ahn_tile, surf='ground_surface', ax=ax1, cbar_pad=0.14)
plot_ahn_surface(ahn_tile, surf='building_surface', ax=ax1, cbar_pad=0.04)
ax1.set_title(f'AHN tile {tilecode}', fontsize=14)
plt.show()
def plot_buildings_ahn_bgt(tilecode, ahn_reader, building_file, offset=1,
show_elevation=True, offset_only=True, title=None):
ahn_tile = ahn_reader.filter_tile(tilecode)
buildings = bgt_utils.get_polygons(building_file, tilecode)
if offset > 0:
buildings_ofs = list(cascaded_union(
[Polygon(clip_utils.poly_offset(bld, offset))
for bld in buildings]))
else:
buildings_ofs = []
((x_min, y_max), (x_max, y_min)) =\
las_utils.get_bbox_from_tile_code(tilecode)
fig, ax = plt.subplots(1, figsize=(7, 5), constrained_layout=True)
if show_elevation:
plot_ahn_surface(
ahn_tile, surf='building_surface', ax=ax, cbar_pad=0.04)
if title is None:
title = 'Building footprints and elevation'
else:
cmap = mcolors.LinearSegmentedColormap.from_list('sg', ['lightgrey']*2)
ax.imshow(ahn_tile['building_surface'],
extent=[x_min, x_max, y_min, y_max],
interpolation='none', cmap=cmap)
dummy = patches.Patch(color='lightgrey', label='AHN surface')
if title is None:
title = 'Building footprints'
if not offset_only:
for poly in [Polygon(bld) for bld in buildings]:
x, y = poly.exterior.xy
ax.plot(x, y, c=bgt_colors['pand_poly'], label='BGT polygon')
for poly in buildings_ofs:
x, y = poly.exterior.xy
ax.plot(x, y, c=bgt_colors['pand_poly'], linestyle='--',
label='Offset polygon')
box = patches.Rectangle((x_min, y_min), x_max-x_min, y_max-y_min,
linewidth=1, linestyle='--', edgecolor='black',
fill=False)
ax.add_patch(box)
ax.set_title(title)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_xticks(range(x_min, x_max+1, 10))
ax.set_xticklabels(range(x_min, x_max+1, 10))
ax.set_yticks(range(y_min, y_max+1, 10))
ax.set_yticklabels(range(y_min, y_max+1, 10))
if not show_elevation:
handles, labels = ax.get_legend_handles_labels()
by_label = dict(zip(labels, handles))
by_label[dummy.get_label()] = dummy
ax.legend(by_label.values(), by_label.keys(),
loc='center left', bbox_to_anchor=(1, 0.5))
padding = 2.5
ax.set_xlim((x_min - padding, x_max + padding))
ax.set_ylim((y_min - padding, y_max + padding))
ax.set_aspect('equal', adjustable='box')
plt.show()