Loading [MathJax]/extensions/tex2jax.js
DM_numpy_spatial_query.py
1 ## @package python.demo.DM_numpy_spatial_query
2 # "
3 # example script showing how to load an OPALS datamanager
4 # and perform a spatial query returned as numpy dictionary
5 #
6 
7 from __future__ import print_function # print function syntax as in python 3
8 import sys
9 
10 from opals import pyDM
11 
12 def DM_spatial_query(filename, renderPoints = False):
13  # pyDM.Datamanager.load parameter: filename(string), readOnly(bool) threadSafety(bool)
14  dm = pyDM.Datamanager.load(filename, True, False)
15  if not dm:
16  print("Unable to open ODM '" + filename + "'")
17  sys.exit(1)
18 
19  # output some pyDM statistic
20  limit = dm.getLimit()
21  print("ODM contains", dm.sizePoint(), "points")
22  print("2D-limit (%.3f," % limit.xmin, "%.3f) -" % limit.ymin, "(%.3f," % limit.xmax, "%.3f)" % limit.ymax)
23 
24  # build layout for attributes of interest (subset of odm attributes)
25  lf = pyDM.AddInfoLayoutFactory()
26  type, inDM = lf.addColumn(dm, "Id", True); assert inDM == True
27  type, inDM = lf.addColumn(dm, "GPSTime", True); assert inDM == True
28  type, inDM = lf.addColumn(dm, "Amplitude", True); assert inDM == True
29  type, inDM = lf.addColumn(dm, "Classification", True); assert inDM == True
30  layout = lf.getLayout()
31 
32  # Create query object
33  # polygon
34  pf = pyDM.PolygonFactory()
35  pf.addPoint(529600, 5338600)
36  pf.addPoint(529650, 5338620)
37  pf.addPoint(529650, 5338650)
38  pf.addPoint(529630, 5338650)
39  pf.closePart()
40  queryPoly = pf.getPolygon()
41  queryWin = pyDM.Window(529600, 5338600, 529650, 5338650) # window
42 
43 
44  # creaate optional filter. select only ground points
45  filter = pyDM.Filter("class[ground]")
46 
47  # [query points as dict of numpy arrays]
48  print("\nPerform spatial query")
49  result = pyDM.NumpyConverter.searchPoint(dm, queryPoly, layout, withCoordinates=True) # polygon query without filter
50  #result = pyDM.NumpyConverter.searchPoint(dm, queryWin, layout, withCoordinates=True, filter=filter) # window query with filter
51 
52  print("result=", result) # print returned object
53  # [query points as dict of numpy arrays]
54 
55  # number of points found
56  ptsQueried = result[next(iter(result))].size
57  print(f"{ptsQueried} points queried")
58 
59  if renderPoints:
60  # render points if requested
61  import matplotlib.pyplot as plt
62  fig = plt.figure()
63  ax = fig.add_subplot(projection='3d')
64  ax.scatter(result["x"], result["y"], result["z"], c=result["Amplitude"], marker='.', cmap=plt.cm.Greys.reversed())
65 
66  plt.axis('equal')
67  plt.show()
68 
69 
70 if len(sys.argv) == 1:
71  print("ODM parameter missing")
72  sys.exit(-1)
73 
74 renderPts = False
75 if len(sys.argv) > 2:
76  renderPts = sys.argv[2] == '1' or sys.argv[2].lower() == 'true'
77 
78 DM_spatial_query(sys.argv[1], renderPts)