DM_spatial_query_point_index.py
## @package python.DM_spatial_query_point_index
#
# example script showing how to load an OPALS datamanager and
# perform a simple circle, cylinder and sphere queries on the point index
#
import sys, math
from datetime import datetime
from opals import pyDM
if len(sys.argv) == 1:
print("ODM parameter missing")
exit(-1)
# open the odm / pyDM.Datamanager.load parameters: filename(string), readOnly(bool) threadSafety(bool)
dm = pyDM.Datamanager.load(sys.argv[1], True, False)
# if the dm wasn't opened successfully exit function
if not dm:
print("Unable to open ODM '" + sys.argv[1] + "'")
exit(-1)
# Create a PointIndex from where we can access spatial queries
PointIndex = dm.getPointIndex()
# Get realistic coordinates for our shapes
lim = dm.getLimit()
x = (lim.xmax + lim.xmin) / 2
y = (lim.ymax + lim.ymin) / 2
a = (lim.xmax - lim.xmin) / 3 # thirds, since we don't want the whole thing
b = (lim.ymax - lim.ymin) / 3
r = (a + b) / 2
z = (lim.zmin + lim.zmax) / 2
c = (lim.zmax - lim.zmin) / 3
zmax = z + c
zmin = z - c
# Create a few query shapes
cir = pyDM.Circle(x, y, r)
cyl = pyDM.Cylinder(x, y, r, zmin, zmax)
sph = pyDM.Sphere(x, y, z, r)
# Create a 2D-Polygon (approximating the circle)
PolygonF = pyDM.PolygonFactory()
for alp in range(0, 360, 6):
alpr = alp * math.pi / 180
PolygonF.addPoint(x + r * math.sin(alpr), y + r * math.cos(alpr))
PolygonF.closePart()
pol = PolygonF.getPolygon(pyDM.Orientation.cw)
# Count points in the shapes
cirstart = datetime.now()
PointsInCir = PointIndex.searchPoint(cir)
NumPointsInCir = len(PointsInCir)
cirend = datetime.now()
cylstart = datetime.now()
PointsInCyl = PointIndex.searchPoint(cyl)
NumPointsInCyl = len(PointsInCyl)
cylend = datetime.now()
sphstart = datetime.now()
PointsInSph = PointIndex.searchPoint(sph)
NumPointsInSph = len(PointsInSph)
sphend = datetime.now()
polstart = datetime.now()
PointsInPol = PointIndex.searchPoint(pol)
NumPointsInPol = len(PointsInPol)
polend = datetime.now()
print("Out of %6i points total..." % dm.sizePoint())
print("The circle contains %6i points. (runtime: %s)" % (NumPointsInCir, str(cirend - cirstart)))
print("The cylinder contains %6i points. (runtime: %s)" % (NumPointsInCyl, str(cylend - cylstart)))
print("The sphere contains %6i points. (runtime: %s)" % (NumPointsInSph, str(sphend - sphstart)))
print("The polygon contains %6i points. (runtime: %s)" % (NumPointsInPol, str(polend - polstart)))