DM_numpy_pointfile_read.py
1 ## @package python.demo.DM_numpy_pointfile_read
2 #
3 # Small example showing how to the read a point cloud file as numpy dict using the import functionality of the pyDM
4 #
5 from __future__ import print_function
6 
7 import opals, sys, os
8 
9 from opals import pyDM
10 
11 
12 
13 def DM_read_pointfile(filename):
14  ptsToOutput = 100 # for limiting point output. use -1 for output all points
15 
16  # if unspecified or 'auto' is set, import creator automatically detects file format based on its content
17  imp = pyDM.Import.create(filename, pyDM.DataFormat.auto)
18 
19  if not imp:
20  print("Unable to create import object for '" + filename + "'")
21  return
22 
23  # read header
24  imp.readHeaderSeparately()
25  header = imp.getHeader()
26 
27  # output header details
28  print("File {} details:".format(filename))
29  print("\tFile format:", str(imp.getFileFormat()))
30  print("\tPoint count:", str(header.getPointCount()))
31  print("\tCRS :", str(header.getCRS()))
32 
33  if len(header.attributes()) > 0:
34  print("\tAttributes:")
35  formatStr = "\t\t{:<25}{:<10}"
36  print(formatStr.format("Name","Type"))
37  for attr in header.attributes():
38  print(formatStr.format(attr.getName(), str(attr.getType())))
39 
40  print() # print empty line
41 
42  overallPtCount = header.getPointCount()
43  layout = header.getDataLayout()
44 
45  ptsRead = 0
46  ptsOutput = 0
47  for obj in imp:
48  # points are always read in chunks of 1000 points (can be change when creating the import object)
49  pts = pyDM.NumpyConverter.get(obj, layout) # get points as numpy dict
50  rows = pts["x"].shape[0]
51  ptsRead += rows
52 
53  for i in range(rows):
54  if ptsToOutput != -1 and ptsOutput >= ptsToOutput:
55  print("\t...")
56  break
57  print(f'\t{ptsOutput:5d} { pts["x"][i]:12.3f} {pts["y"][i]:12.3f} {pts["z"][i]:10.3f}')
58  ptsOutput += 1
59 
60 
61  # statistic on how much points have been imported already
62  #print("{0:.1f} pts read".format((ptsRead/float(overallPtCount)*100)))
63 
64  # close file handle
65  del imp
66 
67 if len(sys.argv) < 1:
68  print("filename parameter missing")
69  sys.exit(-1)
70 
71 DM_read_pointfile(sys.argv[1])