RDB 2
rdb-example-02-insert-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-02-insert-points.py
26 
27 This example shows how to open an existing database and add some points.
28 This example is based on the database of rdb-example-1-create-database.
29 """
30 
31 import random
32 
33 import riegl.rdb
34 
35 
36 def example_plain():
37  # Access existing database
38  with riegl.rdb.rdb_open("pointcloud.rdbx") as rdb:
39 
40  # Query some attribute details
41  details_coordinates = rdb.point_attributes.riegl_xyz
42  details_reflectance = rdb.point_attributes.riegl_reflectance
43 
44  # Before we can modify the database, we must start a transaction
45  with riegl.rdb.Transaction(
46  rdb, # point cloud object
47  "Import", # transaction title
48  "Point Importer v1.0" # software name
49  ) as transaction:
50 
51  # Prepare point attribute buffers
52  buffer_size = 100 # point block/chunk size
53  point_count = 1500 # total number of points
54  buffer_coordinates = riegl.rdb.AttributeBuffer(
55  details_coordinates, buffer_size
56  )
57  buffer_reflectance = riegl.rdb.AttributeBuffer(
58  details_reflectance, buffer_size
59  )
60 
61  # Start new insert query and define buffers
62  with rdb.insert() as query:
63  query.bind(buffer_coordinates)
64  query.bind(buffer_reflectance)
65 
66  # Insert points block-wise
67  total = 0
68  while total < point_count:
69  # Fill buffers with some random data
70  for i in range(buffer_size):
71  buffer_coordinates[i] = [random.uniform(
72  details_coordinates.minimum_value,
73  details_coordinates.maximum_value
74  ) for _ in range(3)]
75  buffer_reflectance[i] = random.uniform(
76  details_reflectance.minimum_value,
77  details_reflectance.maximum_value
78  )
79 
80  # Actually insert points
81  total += query.next(buffer_size)
82 
83  # Finally commit transaction
84  transaction.commit()
85 
86 
87 def example_numpy():
88  """
89  Similar to example_plain() but we use NumPy arrays here.
90  """
91  import numpy as np
92 
93  # Create some numpy arrays
94  c = np.array([[1, 2, 3], [4, 5, 6]])
95  r = np.array([7, 8])
96 
97  # Access existing database
98  with riegl.rdb.rdb_open("pointcloud.rdbx") as rdb:
99 
100  # Before we can modify the database, we must start a transaction
101  with riegl.rdb.Transaction(
102  rdb, # point cloud object
103  "Import", # transaction title
104  "Point Importer v1.0" # software name
105  ) as transaction:
106 
107  # start inserting points
108  with rdb.insert() as insert:
109  # Create buffers or all point attributes
110  buffers = riegl.rdb.PointBuffer(rdb, count=len(c), attributes=[
111  "riegl.xyz",
112  "riegl.reflectance"
113  ])
114 
115  # Copy data to point attribute buffers
116  np.copyto(buffers["riegl.xyz"] .data, c)
117  np.copyto(buffers["riegl.reflectance"].data, r)
118 
119  # Actually insert points
120  insert.bind(buffers)
121  insert.next(len(c))
122 
123  # Finally commit transaction
124  transaction.commit()
125 
126 
127 if __name__ == "__main__":
128  example_plain()
129  example_numpy()