RDB 2
rdb-example-03-select-points.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 #
4 #*******************************************************************************
5 #
6 # Copyright 2021 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("{0}, {1}, {2}".format(
46  point.riegl_id, # access point attributes like real
47  point["riegl.xyz"], # attributes or like a dictionary
48  point.riegl_reflectance
49  ))
50 
51 
52 def example_b():
53  """
54  Similar to example A, but instead of loading all point attributes
55  we specify the names of the point attributes to load and print.
56  """
57  print("Example B:")
58  with riegl.rdb.rdb_open("pointcloud.rdbx") as rdb:
59  for point in rdb.points(
60  "(riegl.xyz[2] > 5) && (riegl.reflectance > 35)",
61  ["riegl.id", "riegl.xyz", "riegl.reflectance"]
62  ):
63  print(point)
64 
65 
66 def example_c():
67  """
68  Similar to example A, but instead of loading point-by-point, we
69  load points chunk-wise and access the attributes via arrays.
70  Please note that we use select() instead of points() this time.
71  """
72  print("Example C:")
73  with riegl.rdb.rdb_open("pointcloud.rdbx") as rdb:
74  for points in rdb.select(
75  "(riegl.xyz[2] > 5) && (riegl.reflectance > 35)",
76  chunk_size=100000 # optional: number of points to load in one step
77  ):
78  print(points.riegl_id) # this time, the attribute is not a
79  print(points["riegl.xyz"]) # single value but an array of values
80  print(points.riegl_reflectance)
81 
82 
83 def example_d():
84  """
85  This example shows the most flexible way as known from the RDB C++ API.
86  This time we manually create buffers for the point attributes read from
87  the point cloud and iterate the point cloud chunk-wise.
88  """
89  print("Example D:")
90  with riegl.rdb.rdb_open("pointcloud.rdbx") as rdb:
91  # number of points to load in one step
92  chunk_size = 100 * 1000
93 
94  # create attribute buffers (array) for point coordinates and reflectance
95  buffer_xyz = riegl.rdb.AttributeBuffer(
96  rdb.point_attributes.riegl_xyz, chunk_size
97  )
98  buffer_reflectance = riegl.rdb.AttributeBuffer(
99  rdb.point_attributes.riegl_reflectance, chunk_size
100  )
101 
102  # load all points chunk-wise
103  with rdb.select() as query:
104  # tell the query where to store the point attribute data to
105  query.bind(buffer_xyz)
106  query.bind(buffer_reflectance)
107 
108  point_count = 1 # actual number of points not yet known
109  while point_count > 0:
110  point_count = query.next(chunk_size)
111  # Please note, that the buffer array length is not modified
112  # by query.next(). So the arrays will always have a length
113  # of 'chunk_size' but only the first 'point_count' elements
114  # actually contain data.
115  for i in range(point_count):
116  print("{0}, {1}".format(
117  buffer_xyz[i],
118  buffer_reflectance[i]
119  ))
120 
121 
122 if __name__ == "__main__":
123  example_a()
124  example_b()
125  example_c()
126  example_d()