Loading [MathJax]/extensions/tex2jax.js
DM_pointfile_read.py
1 ## @package python.demo.DM_pointfile_read
2 #
3 # Small example showing how to the read a point cloud file 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  ptsRead = 0
44  ptsOutput = 0
45  for obj in imp:
46  # points are always read in chunks of 1000 points (can be change when creating the import object)
47  ptsRead += obj.sizePoint()
48 
49  #loop over points
50  for i in range(obj.sizePoint()):
51  if ptsToOutput != -1 and ptsOutput >= ptsToOutput:
52  print("\t...")
53  break
54  pt = obj[i]
55  print("\t{0:5d} {1:12.3f} {2:12.3f} {3:10.3f}".format(ptsOutput, pt.x,pt.y,pt.z))
56  ptsOutput += 1
57 
58  if ptsToOutput != -1 and ptsOutput >= ptsToOutput:
59  break
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])