Loading [MathJax]/extensions/tex2jax.js
DM_numpy_spatial_query2.py
1 ## @package python.demo.DM_numpy_spatial_query2
2 # "
3 # example script showing how to load an OPALS datamanager,
4 # perform a spatial query and changing attribute values using
5 # numpy arrays
6 #
7 
8 from __future__ import print_function # print function syntax as in python 3
9 import sys
10 
11 from opals import pyDM
12 
13 def DM_spatial_query_and_classify(filename, classValue):
14  # open datamanager with write access
15  dm = pyDM.Datamanager.load(filename, readOnly=False, threadSafety=False)
16  if not dm:
17  print("Unable to open ODM '" + filename + "'")
18  sys.exit(1)
19 
20  # output some pyDM statistic
21  limit = dm.getLimit()
22  print("ODM contains", dm.sizePoint(), "points")
23  print("2D-limit (%.3f," % limit.xmin, "%.3f) -" % limit.ymin, "(%.3f," % limit.xmax, "%.3f)" % limit.ymax)
24 
25  # build layout for attributes of interest (subset of odm attributes)
26  lf = pyDM.AddInfoLayoutFactory()
27  type, inDM = lf.addColumn(dm, "Id", True); assert inDM == True
28  type, inDM = lf.addColumn(dm, "Classification", True); assert inDM == True
29  layout = lf.getLayout()
30 
31  # Create query object
32  queryWin = pyDM.Window(529600, 5338600, 529650, 5338650) # window
33  print("\nPerform spatial query")
34  result = pyDM.NumpyConverter.searchPoint(dm, queryWin, layout, withCoordinates=True) # window query
35 
36  # number of points found
37  ptsQueried = result[next(iter(result))].size
38  print(f"{ptsQueried} points queried")
39 
40  if ptsQueried == 0:
41  return
42 
43  print(f"Change selected points to classification {classValue}")
44  classArray = result["Classification"]
45  classArray.fill(classValue)
46 
47  # Create a new dict only containing the relevant numpy arrays
48  setObj = {}
49  setObj["Id"] = result["Id"] # we need the ids to identify the points
50  setObj["Classification"] = classArray # those are the changed classification values
51 
52  # write changed attribute values to the ODM
53  pyDM.NumpyConverter.setById(setObj, dm, layout)
54 
55  print(f"Save ODM")
56  dm.save()
57 
58 
59 if len(sys.argv) == 1:
60  print("ODM parameter missing")
61  sys.exit(-1)
62 
63 DM_spatial_query_and_classify(sys.argv[1], classValue=6)