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 sys, os
8 import opals
9 
10 from opals import pyDM
11 
12 
13 
14 def DM_read_pointfile(filename):
15  ptsToOutput = 100 # for limiting point output. use -1 for output all points
16 
17  # if unspecified or 'auto' is set, import creator automatically detects file format based on its content
18  imp = pyDM.Import.create(filename, pyDM.DataFormat.auto)
19 
20  if not imp:
21  print("Unable to create import object for '" + filename + "'")
22  return
23 
24  # read header
25  imp.readHeaderSeparately()
26  header = imp.getHeader()
27 
28  # output header details
29  print("File {} details:".format(filename))
30  print("\tFile format:", str(imp.getFileFormat()))
31  print("\tPoint count:", str(header.getPointCount()))
32  print("\tCRS :", str(header.getCRS()))
33 
34  formatStr = "\t\t{:<25}{:<10}"
35  if len(header.properties()) > 0:
36  # output additional file properties
37  print("\tFile properties:")
38  print(formatStr.format("Name","Value"))
39  for prop in header.properties():
40  print(formatStr.format(prop, str(header.getProperty(prop))))
41  if len(header.attributeProperties()) > 0:
42  # also list attribute properties
43  print("\tAttribute properties: ", list(header.attributeProperties()))
44  if len(header.attributes()) > 0:
45  # now output the list of attributes
46  print("\tAttributes:")
47  print(formatStr.format("Name","Type"))
48  for attr in header.attributes():
49  print(formatStr.format(attr.getName(), str(attr.getType())))
50 
51  print() # print empty line
52 
53  overallPtCount = header.getPointCount()
54  ptsRead = 0
55  ptsOutput = 0
56  for obj in imp:
57  # points are always read in chunks of 1000 points (can be change when creating the import object)
58  ptsRead += obj.sizePoint()
59 
60  #loop over points
61  for i in range(obj.sizePoint()):
62  if ptsToOutput != -1 and ptsOutput >= ptsToOutput:
63  print("\t...")
64  break
65  pt = obj[i]
66  print("\t{0:5d} {1:12.3f} {2:12.3f} {3:10.3f}".format(ptsOutput, pt.x,pt.y,pt.z))
67  ptsOutput += 1
68 
69  if ptsToOutput != -1 and ptsOutput >= ptsToOutput:
70  break
71 
72  # statistic on how much points have been imported already
73  #print("{0:.1f} pts read".format((ptsRead/float(overallPtCount)*100)))
74 
75  # close file handle
76  del imp
77 
78 if len(sys.argv) < 1:
79  print("filename parameter missing")
80  sys.exit(-1)
81 
82 DM_read_pointfile(sys.argv[1])