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