DM_read_pointfile.py
1 ## @package python.demo.DM_read_pointfile
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  overallPtCount = header.getPointCount()
34  ptsRead = 0
35  ptsOutput = 0
36  for obj in imp:
37  # points are always read in chunks of 1000 points (can be change when creating the import object)
38  ptsRead += obj.sizePoint()
39 
40  #loop over points
41  for i in range(obj.sizePoint()):
42  if ptsToOutput != -1 and ptsOutput >= ptsToOutput:
43  print("\t...")
44  break
45  pt = obj[i]
46  print("\t{0:5d} {1:12.3f} {2:12.3f} {3:10.3f}".format(ptsOutput, pt.x,pt.y,pt.z))
47  ptsOutput += 1
48 
49  if ptsToOutput != -1 and ptsOutput >= ptsToOutput:
50  break
51 
52  # statistic on how much points have been imported already
53  #print("{0:.1f} pts read".format((ptsRead/float(overallPtCount)*100)))
54 
55  # close file handle
56  del imp
57 
58 if len(sys.argv) < 1:
59  print("filename parameter missing")
60  sys.exit(-1)
61 
62 DM_read_pointfile(sys.argv[1])