-
Notifications
You must be signed in to change notification settings - Fork 23
/
query_examples.py
167 lines (137 loc) · 5.43 KB
/
query_examples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python
import roslib; roslib.load_manifest("soma_trajectory")
import rospy
from human_trajectory.msg import Trajectory
from human_trajectory.msg import Trajectories
from soma_trajectory.srv import TrajectoryQuery, TrajectoryQueryRequest, TrajectoryQueryResponse
class QueryClient():
def __init__(self):
service_name = 'trajectory_query'
rospy.wait_for_service(service_name)
self.ser = rospy.ServiceProxy(service_name, TrajectoryQuery)
def query(self, query, vis = False):
try:
req = TrajectoryQueryRequest()
req.query = query
req.visualize = vis
res = self.ser(req)
return res
except rospy.ServiceException, e:
rospy.logerr("Service call failed: %s"%e)
if __name__=="__main__":
rospy.init_node("query_examples")
rospy.loginfo("Running query examples")
client = QueryClient()
## Get all trajectories
# query = '{}'
# rospy.loginfo("Query: %s" % query )
# res = client.query(query, True)
# rospy.loginfo("Result: %s trajectories" % len(res.trajectories.trajectories))
#raw_input("Press enter to continue")
# Malformed queries lead to an error => always check error flag in result!
query = '{'
rospy.loginfo("Query: %s" % query)
res = client.query(query, True)
if res.error:
rospy.logerr("Result: error: %s (Invalid query: %s)" % (res.error, query))
raw_input("Press enter to continue")
# TEMPORAL QUERIES
# Available attributes (example):
# "start" : 1416840575.527016,
# "end" : 1416840578.927002,
# "start_hour" : 14,
# "end_hour" : 14,
# "start_minute" : 49,
# "end_minute" : 49,
# "start_weekday" : 0,
# "end_weekday" : 0
# Get all trajectories that start between 14-15 o'clock
query = '{"start_hour": {"$gt": 13, "$lt": 15}}'
rospy.loginfo("Query: %s" % query )
res = client.query(query, True)
rospy.loginfo("Result: %s trajectories" % len(res.trajectories.trajectories))
raw_input("Press enter to continue")
# Get all trajectories that start between 14-15 o'clock and weekday is 0
query = '{"start_hour": {"$gt": 13, "$lt": 15}, "start_weekday": 0}'
rospy.loginfo("Query: %s" % query )
res = client.query(query, True)
rospy.loginfo("Result: %s trajectories" % len(res.trajectories.trajectories))
raw_input("Press enter to continue")
# Get all trajectories that start between 14-15 o'clock and end between 15-16 o'clock
query = '{"start_hour": {"$gt": 13, "$lt": 15}, "end_hour": {"$gt": 14, "$lt": 16}}'
rospy.loginfo("Query: %s" % query )
res = client.query(query, True)
rospy.loginfo("Result: %s trajectories" % len(res.trajectories.trajectories))
raw_input("Press enter to continue")
# # GEO SPATIAL QUERIES
# near object [geometry is a point, maxDistance in meters]
query = '{"loc": {"$nearSphere": { "$geometry": { "type" : "Point", "coordinates" : [ -0.0002281133006505343, -4.632269674686995e-05 ] }, "$maxDistance": 1}}}'
rospy.loginfo("Query: %s" % query )
res = client.query(query, True)
rospy.loginfo("Result: %s trajectories" % len(res.trajectories.trajectories))
raw_input("Press enter to continue")
# within region of interest (ROI) [geometry is a polygon]
query ='''{"loc": { "$geoWithin": { "$geometry":
{
"type" : "Polygon",
"coordinates" : [
[
[
-0.0002246355582968818,
-2.519034444503632e-05
],
[
-0.0002241486476179944,
-7.42736662147081e-05
],
[
-0.000258645873657315,
-7.284014769481928e-05
],
[
-0.0002555339747090102,
-2.521782172948406e-05
],
[
-0.0002246355582968818,
-2.519034444503632e-05
]
]
]
}}}}'''
rospy.loginfo("Query: %s" % query )
res = client.query(query, True)
rospy.loginfo("Result: %s trajectories" % len(res.trajectories.trajectories))
raw_input("Press enter to continue")
# intersects with other shape [geometry is one of point, polygon, linestring]
# within region of interest (ROI) [geometry is a polygon]
query ='''{"loc": { "$geoIntersects": { "$geometry": {
"type" : "Polygon",
"coordinates" : [
[
[
-0.0002246355582968818,
-2.519034444503632e-05
],
[
-0.0002241486476179944,
-7.42736662147081e-05
],
[
-0.000258645873657315,
-7.284014769481928e-05
],
[
-0.0002555339747090102,
-2.521782172948406e-05
],
[
-0.0002246355582968818,
-2.519034444503632e-05
]
]
]
}}}}'''
rospy.loginfo("Query: %s" % query )
res = client.query(query, True)
rospy.loginfo("Result: %s trajectories" % len(res.trajectories.trajectories))