-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdashboard.py
218 lines (171 loc) · 6.93 KB
/
dashboard.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
from operator import attrgetter
import numbers
from sqlalchemy.exc import DBAPIError
from sqlalchemy import create_engine
from sqlalchemy import sql
from sqlalchemy.orm import sessionmaker
from geoalchemy2 import func as geosql
from geoalchemy2 import shape as geoshape
from shapely import geometry
import geopandas as geo
import numpy as np
import dash_vtk
import pandas as pd
from dash import Dash, html, dcc, Input, Output, State, dash_table, no_update
from dash.long_callback import DiskcacheLongCallbackManager
import dash_bootstrap_components as dbc
import diskcache
import hydra
from db import LazPoints
with hydra.initialize("configs") as config_dir:
CONFIG = hydra.compose("insert_data")
FILE_DROPDOWN = "file-selection-id"
CHUNK_DROP_DOWN = "chunk-dropdown-id"
PLOT_3D_ID = "3d-scatter-id"
PROGRESS_BAR_ID = "progress-bar-id"
BUTTON_ID = "draw-button-id"
TABLE_ID = "datatable-id"
ERROR_ID = "error-id"
RADIUS_SLIDER_ID = "slider-id"
CACHE_DIR = "./dashboard-cache"
app = Dash(__name__, external_stylesheets=[
dbc.themes.BOOTSTRAP], long_callback_manager=DiskcacheLongCallbackManager(diskcache.Cache(CACHE_DIR)))
app.layout = dbc.Container([
html.Div(children=[dcc.Link("View control", href="https://dash.plotly.com/vtk/intro#view")]),
html.Label("File:", id="start"),
dcc.Dropdown(id=FILE_DROPDOWN),
html.Label("Chunk id:"),
dcc.Dropdown(id=CHUNK_DROP_DOWN, multi=True),
html.Label("Radius around mean point [units of the data]:"),
dcc.Slider(0, 4, value=2, step=0.1, id=RADIUS_SLIDER_ID, tooltip={
"placement": "bottom", "always_visible": True}),
html.P(id=ERROR_ID, style={"color": "red"}),
html.Div(children=[html.Button("Query points and draw", id=BUTTON_ID, style={"width": "25%"})]),
html.Progress(id=PROGRESS_BAR_ID, max=str(100),
title="Loading...", style={"visibility": "hidden"}),
dbc.Row(
children=[
dbc.Col(
dash_table.DataTable(id=TABLE_ID, page_action="native", sort_action="native", page_size=12, page_current=0), width=2),
dbc.Col(id=PLOT_3D_ID)
], class_name="h-75"
)
], fluid=True, style={"height": "80vh"}
)
@ app.callback(
Output(FILE_DROPDOWN, "value"),
Output(FILE_DROPDOWN, "options"),
Output(ERROR_ID, "children"),
Input("start", "style")
)
def select_files(_):
try:
engine = create_engine(CONFIG.db.url, pool_pre_ping=True)
except DBAPIError as exc:
return no_update, no_update, str(exc)
Session = sessionmaker(engine)
with Session.begin() as session:
all_files = list(map(attrgetter("file"), session.query(
LazPoints.file).order_by(LazPoints.file.asc()).distinct()))
engine.dispose()
return all_files[0], all_files, ""
@ app.callback(
Output(CHUNK_DROP_DOWN, "value"),
Output(CHUNK_DROP_DOWN, "options"),
Output(TABLE_ID, "data"),
Output(TABLE_ID, "columns"),
Input(FILE_DROPDOWN, "value"),
)
def select_chunk_ids(file):
try:
engine = create_engine(CONFIG.db.url, pool_pre_ping=True)
except DBAPIError as exc:
return no_update, no_update, no_update, no_update
Session = sessionmaker(engine)
with Session.begin() as session:
info_query = sql.select(
LazPoints.chunk_id.label("chunk id"), geosql.ST_NPoints(LazPoints.points).label("number of points"))\
.where(LazPoints.file == file).order_by("chunk id")
data = pd.read_sql(info_query, session.connection())
engine.dispose()
return data["chunk id"][0], data["chunk id"], data.to_dict("records"), [{"name": i, "id": i} for i in data.columns]
@ app.long_callback(
output=Output(PLOT_3D_ID, "children"),
inputs=[Input(BUTTON_ID, "n_clicks")],
state=[State(FILE_DROPDOWN, "value"), State(
CHUNK_DROP_DOWN, "value"), State(RADIUS_SLIDER_ID, "value")],
running=[
(Output(FILE_DROPDOWN, "disabled"), True, False),
(Output(CHUNK_DROP_DOWN, "disabled"), True, False),
(Output(BUTTON_ID, "disabled"), True, False),
(
Output(PROGRESS_BAR_ID, "style"),
{"visibility": "visible"},
{"visibility": "hidden"},
)
],
interval=4000,
progress=[Output(PROGRESS_BAR_ID, "value")],
prevent_initial_call=True
)
def select_chunk(set_progress, n_click, file_path, chunk_ids, radius_around_centroid):
if isinstance(chunk_ids, numbers.Number):
chunk_ids = [chunk_ids]
if not chunk_ids:
return dash_vtk.View(
background=[0, 0, 0]
)
set_progress([str(10)])
try:
engine = create_engine(CONFIG.db.url, pool_pre_ping=True)
except DBAPIError as exc:
return no_update
Session = sessionmaker(engine)
geom_col_name = "point"
color_col_name = "color"
with Session.begin() as session:
subq = sql.select(geosql.ST_DumpPoints(LazPoints.points).geom.label("point"))\
.filter(LazPoints.file == file_path) \
.filter(LazPoints.chunk_id.in_(chunk_ids)).subquery()
mean_point = session.execute(sql.select(
sql.func.avg(geosql.ST_X(subq.c.point)).label("x"),
sql.func.avg(geosql.ST_Y(subq.c.point)).label("y"),
sql.func.avg(geosql.ST_Z(subq.c.point)).label("z"))).one()
mean_point = geoshape.from_shape(geometry.Point(mean_point.x, mean_point.y, mean_point.z))
set_progress([str(25)])
del subq
first_filtered_points = sql.select(geosql.ST_DumpPoints(LazPoints.points).label("point_info"), LazPoints.colors)\
.where(geosql.ST_3DDWithin(LazPoints.points, mean_point, radius_around_centroid)).subquery()
filtered_points = sql.select(first_filtered_points.c.point_info.geom.label("point"),
first_filtered_points.c.colors[first_filtered_points.c.point_info.path[1]: first_filtered_points.c.point_info.path[1]].label("color")) \
.where(geosql.ST_3DDWithin(first_filtered_points.c.point_info.geom, mean_point, radius_around_centroid))
points_data = geo.read_postgis(
filtered_points, session.connection(), geom_col=geom_col_name)
engine.dispose()
set_progress([str(50)])
xyz = []
colors = []
for row in points_data.itertuples(index=False):
coord = getattr(row, geom_col_name)
xyz.extend(coord.coords)
colors.extend(np.array(getattr(row, color_col_name)).reshape(-1))
set_progress([str(75)])
xyz = np.array(xyz)
xyz -= xyz.mean(axis=0)
scale = 1 / max(np.linalg.norm(xyz.max(axis=0) - xyz.min(axis=0)), 1e-4)
xyz *= scale
xyz = xyz.reshape(-1).tolist()
vtk_view = dash_vtk.View(
[
dash_vtk.PointCloudRepresentation(
xyz=xyz,
rgb=colors,
property={"pointSize": 2}
)
],
cameraPosition=[0, 0, 3],
background=[0, 0, 0]
)
return vtk_view
if __name__ == "__main__":
app.run_server(debug=False, dev_tools_ui=False)