| RDB 2

rdb-example-03-select-points.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 #
4 #*******************************************************************************
5 #
6 # Copyright 2025 RIEGL Laser Measurement Systems
7 #
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
11 #
12 # http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
19 #
20 # SPDX-License-Identifier: Apache-2.0
21 #
22 #*******************************************************************************
23 #
24 """
25 rdb-example-03-select-points.py
26
27 This example shows different ways to open an existing database and query
28 some points.
29 """
30
31 import riegl.rdb
32
33
34 def example_a():
35 """
36 Load points that meet filter criteria and print them point-wise.
37 To load all points instead, simply remove the filter string or
38 specify 'None' (the constant, not the string "None").
39 """
40 print("Example A:")
41 with riegl.rdb.rdb_open("pointcloud.rdbx") as rdb:
42 for point in rdb.points(
43 "(riegl.xyz[2] > 5) && (riegl.reflectance > 35)"
44 ):
45 print(f"{point['riegl.id']}, {point['riegl.xyz']}, {point['riegl.reflectance']}")
46
47
48 def example_b():
49 """
50 Similar to example A, but instead of loading all point attributes
51 we specify the names of the point attributes to load and print.
52 """
53 print("Example B:")
54 with riegl.rdb.rdb_open("pointcloud.rdbx") as rdb:
55 for point in rdb.points(
56 "(riegl.xyz[2] > 5) && (riegl.reflectance > 35)",
57 ["riegl.id", "riegl.xyz", "riegl.reflectance"]
58 ):
59 print(f"{point['riegl.id']}, {point['riegl.xyz']}, {point['riegl.reflectance']}")
60
61
62 def example_c():
63 """
64 Similar to example A, but instead of loading point-by-point, we
65 load points chunk-wise and access the attributes via arrays.
66 Please note that we use select() instead of points() this time.
67 """
68 print("Example C:")
69 with riegl.rdb.rdb_open("pointcloud.rdbx") as rdb:
70 for points in rdb.select(
71 "(riegl.xyz[2] > 5) && (riegl.reflectance > 35)",
72 chunk_size=100000 # optional: number of points to load in one step
73 ):
74 print(points["riegl.id"]) # this time, the attribute is not a
75 print(points["riegl.xyz"]) # single value but an array of values
76 print(points["riegl.reflectance"])
77
78
79 def example_d():
80 """
81 This example shows the most flexible way as known from the RDB C++ API.
82 This time we manually create buffers for the point attributes read from
83 the point cloud and iterate the point cloud chunk-wise.
84 """
85 print("Example D:")
86 with riegl.rdb.rdb_open("pointcloud.rdbx") as rdb:
87 # number of points to load in one step
88 chunk_size = 100 * 1000
89
90 # create attribute buffers (array) for point coordinates and reflectance
91 buffer_xyz = riegl.rdb.AttributeBuffer(
92 rdb.point_attributes["riegl.xyz"], chunk_size
93 )
94 buffer_reflectance = riegl.rdb.AttributeBuffer(
95 rdb.point_attributes["riegl.reflectance"], chunk_size
96 )
97
98 # load all points chunk-wise
99 with rdb.select() as query:
100 # tell the query where to store the point attribute data to
101 query.bind(buffer_xyz)
102 query.bind(buffer_reflectance)
103
104 point_count = 1 # actual number of points not yet known
105 while point_count > 0:
106 point_count = query.next(chunk_size)
107 # Please note, that the buffer array length is not modified
108 # by query.next(). So the arrays will always have a length
109 # of 'chunk_size' but only the first 'point_count' elements
110 # actually contain data.
111 for i in range(point_count):
112 print(f"{buffer_xyz[i]}, {buffer_reflectance[i]}")
113
114
115 def example_e():
116 """
117 This example shows how to read points into a Pandas DataFrame.
118
119 Please note that 'attributes' must be specified as a tuple or list.
120 Do not forget to add a comma at the end if the tuple contains only
121 one character string.
122 """
123 print("Example E:")
124 frame = riegl.rdb.rdb_to_pandas(
125 pointcloud=riegl.rdb.rdb_open("pointcloud.rdbx"),
126 attributes=("riegl.id", "riegl.xyz", "riegl.reflectance"),
127 selection="riegl.id <= 10"
128 )
129 print(frame.dtypes)
130 print(frame)
131
132
133 if __name__ == "__main__":
134 example_a()
135 example_b()
136 example_c()
137 example_d()
138 example_e()