DM_spatial_query_polylines.py
1 ## @package python.demo.DM_spatial_query_polylines
2 # "
3 # example script showing how to add polylines to an OPALS datamanager
4 # and perform a spatial queries
5 # (script works in python 2 and 3)
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 output_polyline(line,prefix=""):
14  print(prefix,"Polyline has",line.sizePart(),"parts with",line.sizePoint(),"points")
15  for idx, part in enumerate(line.parts()):
16  print("\tPart",idx,"with",part.sizePoint(),"points")
17  for p in part.points():
18  if sys.float_info.max == p.z:
19  print("\t\t(%.3f," % p.x, " %.3f," % p.y, "---)")
20  else:
21  print("\t\t(%.3f," % p.x, " %.3f," % p.y, "%.3f)" % p.z)
22 
23  return
24 
25 def output_search(lines):
26  print(len(lines)," polylines found")
27  for idx,l in enumerate(lines):
28  output_polyline(l,str(idx)+" line:")
29 
30 # create odm / pyDM.Datamanager.create parameters: filename(string), threadSafety(bool)
31 odm = pyDM.Datamanager.create("polyline_query_test.odm", False)
32 
33 
34 f = pyDM.PolylineFactory()
35 
36 #create polylines and add them to the odm
37 f.addPoint(0, 0, 0)
38 f.addPoint(5, 0, 0)
39 f.addPoint(10, 1, 0)
40 
41 odm.addPolyline( f.getPolyline() )
42 
43 f.addPoint(0, 2, 0)
44 f.addPoint(5, 2, 0)
45 f.addPoint(10, 3, 0)
46 
47 odm.addPolyline( f.getPolyline() )
48 
49 
50 pi = odm.getPolylineIndex()
51 
52 #perform window query
53 print("\nwindow query on polyines:")
54 searchLines = pi.searchGeometry(pyDM.Window(1,1,5,5), pyDM.SpatialQueryMode.intersect)
55 output_search(searchLines)
56 
57 #nearest neighbor search
58 print("\nnearest neighbor search:")
59 searchLines = pi.searchGeometry(1,pyDM.Point(0,0,0) )
60 output_search(searchLines)