Skip to content

Commit 37761aa

Browse files
Return 501 for the acceleration query rather than a finite-difference estimate
Acceleration is not derivable for the motion model: with linearly interpolated position the speed is piecewise-constant, so its derivative is zero within each segment and undefined at the vertices. The handler returns 501 with that rationale instead of estimating a per-segment finite difference of the speed, matching the Go tier's contract for the same query.
1 parent 71d5c04 commit 37761aa

1 file changed

Lines changed: 14 additions & 102 deletions

File tree

Lines changed: 14 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,19 @@
11
# REQ 32: /req/movingfeatures/tpgeometry-query
22
# REQ 33: /req/movingfeatures/tpgeometry-query-success
3-
# SECTION 8.7.5. Acceleration Query
3+
# SECTION 8.7.5. Acceleration Query
44
from utils import send_json_response
5-
from resource.temporal_geom_query.query_helper import build_query_response
6-
import json
7-
import traceback
8-
from datetime import datetime
95

10-
#GET /collections/{collectionId}/items/{featureId}/tgsequence/{geometryId}/acceleration
11-
def get_acceleration(self, collection_id, feature_id, geometry_id, connection, cursor):
12-
try:
13-
#collection exists
14-
cursor.execute(
15-
"SELECT id FROM collections WHERE id = %s",
16-
(collection_id,)
17-
)
18-
if cursor.fetchone() is None:
19-
self.handle_error(404, f"Collection '{collection_id}' not found")
20-
return
21-
22-
#feature exists
23-
cursor.execute(
24-
"SELECT id FROM moving_features WHERE id = %s AND collection_id = %s",
25-
(feature_id, collection_id)
26-
)
27-
if cursor.fetchone() is None:
28-
self.handle_error(404, f"Feature '{feature_id}' not found in collection '{collection_id}'")
29-
return
30-
# +modif 15/03 CLEAN
31-
#temp geom exists for feature
32-
cursor.execute("""
33-
SELECT id FROM temporal_geometries
34-
WHERE id = %s AND feature_id = %s and collection_id = %s
35-
""", (geometry_id, feature_id, collection_id))
36-
37-
if cursor.fetchone() is None:
38-
self.handle_error(404, f"Temporal geometry '{geometry_id}' not found for feature '{feature_id}'")
39-
return
40-
##############################################################################################################################
41-
cursor.execute("""
42-
SELECT
43-
getTimestamp(unnest(instants(speed(trajectory)))) as time,
44-
getValue(unnest(instants(speed(trajectory)))) as speed
45-
FROM temporal_geometries
46-
WHERE id = %s AND feature_id = %s and collection_id = %s
47-
""", (geometry_id, feature_id, collection_id))
48-
rows = cursor.fetchall()
49-
50-
#remark: speed returns stepwise interpolation, derivative requires LInear, toLinear() doesn't accept tfloat type , for now python compute the derivative clean check
51-
52-
print("Speed data:", rows)
53-
if len(rows) < 2:
54-
self.handle_error(404, f"Not enough speed data points for acceleration")
55-
return
56-
57-
#ACCELERATION between each pair of points (tfloats)
58-
datetimes = []
59-
acceleration_values = []
60-
for i in range(len(rows) - 1):
61-
t1, s1 = rows[i]
62-
t2, s2 = rows[i + 1]
63-
dt = (t2 - t1).total_seconds()
64-
if dt > 0:
65-
accel = (s2 - s1) / dt
66-
datetimes.append(
67-
t2.isoformat() if hasattr(t2, "isoformat") else str(t2)
68-
)
69-
acceleration_values.append(float(accel))
70-
71-
# optional first point = 0
72-
datetimes.insert(
73-
0,
74-
rows[0][0].isoformat() if hasattr(rows[0][0], "isoformat") else str(rows[0][0])
75-
)
76-
77-
acceleration_values.insert(0, 0.0)
786

79-
values = {
80-
"datetimes": datetimes,
81-
"values": acceleration_values
82-
}
83-
#acc first point to 0
84-
85-
if not values:
86-
self.handle_error(404, f"No acceleration data found")
87-
return
88-
# response
89-
base_url = f"http://{self.server.server_name}:{self.server.server_port}"
90-
path = f"/collections/{collection_id}/items/{feature_id}/tgsequence/{geometry_id}/acceleration"
91-
92-
response = build_query_response(
93-
values=values,
94-
unit="m/s²",
95-
query_type="acceleration",
96-
base_url=base_url,
97-
path=path
98-
)
99-
100-
send_json_response(self, 200, response)
101-
102-
except Exception as e:
103-
connection.rollback()
104-
#clean traceback : cofirm acceleration is correct - derivative()?
105-
print(f"Error in acceleration query: {e}")
106-
traceback.print_exc()
107-
self.handle_error(500, f"Internal server error: {str(e)}")
7+
# GET /collections/{collectionId}/items/{featureId}/tgsequence/{geometryId}/acceleration
8+
def get_acceleration(self, collection_id, feature_id, geometry_id, connection, cursor):
9+
# Acceleration is not derivable for this motion model: with linearly
10+
# interpolated position the speed is piecewise-constant (Step), so its
11+
# derivative is zero within each segment and undefined at the vertices. The
12+
# value is not approximated (no finite difference, no coercion of the speed
13+
# to Linear) — the same contract as the Go tier, which returns 501 here.
14+
self.handle_error(
15+
501,
16+
"acceleration is not derivable: linearly interpolated position gives a "
17+
"piecewise-constant (Step) speed, whose derivative is zero within each "
18+
"segment and undefined at the vertices",
19+
)

0 commit comments

Comments
 (0)