Skip to content

Commit 0fa50e6

Browse files
authored
Support new JuPedSim trajectory format (#295)
1 parent aa0338f commit 0fa50e6

4 files changed

Lines changed: 377 additions & 54 deletions

File tree

pedpy/io/trajectory_loader.py

Lines changed: 69 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import h5py # type: ignore
99
import pandas as pd
10+
import shapely
1011

1112
from pedpy.column_identifier import FRAME_COL, ID_COL, X_COL, Y_COL
1213
from pedpy.data.geometry import WalkableArea
@@ -332,6 +333,11 @@ def load_walkable_area_from_jupedsim_sqlite(
332333
) -> WalkableArea:
333334
"""Loads the walkable area from the sqlite file as :class:`~geometry.WalkableArea`.
334335
336+
.. note::
337+
338+
When using a JuPedSim sqlite trajectory file with version 2, the walkable ware is the union
339+
of all provided walkable areas in the file.
340+
335341
Args:
336342
trajectory_file: trajectory file in JuPedSim sqlite format
337343
@@ -340,25 +346,71 @@ def load_walkable_area_from_jupedsim_sqlite(
340346
"""
341347
_validate_is_file(trajectory_file)
342348

343-
with sqlite3.connect(trajectory_file) as con:
344-
try:
345-
walkable_query_result = (
346-
con.cursor().execute("select wkt from geometry").fetchone()
347-
)
348-
except Exception as exc:
349-
raise LoadTrajectoryError(
350-
"The given sqlite trajectory is not a valid JuPedSim format, it does not not "
351-
"contain a 'geometry' table. Please check your file."
352-
) from exc
349+
with sqlite3.connect(trajectory_file) as connection:
350+
db_version = _get_jupedsim_sqlite_version(connection)
353351

354-
if walkable_query_result is None:
355-
raise LoadTrajectoryError(
356-
"The given sqlite trajectory file seems not include a geometry. "
357-
"Please check your file."
358-
)
359-
walkable_area = walkable_query_result[0]
352+
if db_version == 1:
353+
return _load_walkable_area_from_jupedsim_sqlite_v1(connection)
354+
355+
if db_version == 2:
356+
return _load_walkable_area_from_jupedsim_sqlite_v2(connection)
357+
358+
raise LoadTrajectoryError(
359+
f"The given sqlite trajectory has unsupported db version {db_version}. "
360+
f"Supported are versions: 1, 2."
361+
)
362+
363+
364+
def _get_jupedsim_sqlite_version(connection: sqlite3.Connection) -> int:
365+
cur = connection.cursor()
366+
return int(
367+
cur.execute(
368+
"SELECT value FROM metadata WHERE key = ?", ("version",)
369+
).fetchone()[0]
370+
)
371+
372+
373+
def _load_walkable_area_from_jupedsim_sqlite_v1(
374+
con: sqlite3.Connection,
375+
) -> WalkableArea:
376+
try:
377+
walkable_query_result = (
378+
con.cursor().execute("select wkt from geometry").fetchone()
379+
)
380+
except Exception as exc:
381+
raise LoadTrajectoryError(
382+
"The given sqlite trajectory is not a valid JuPedSim format, it does not not "
383+
"contain a 'geometry' table. Please check your file."
384+
) from exc
385+
386+
if walkable_query_result is None:
387+
raise LoadTrajectoryError(
388+
"The given sqlite trajectory file seems not include a geometry. "
389+
"Please check your file."
390+
)
391+
392+
return WalkableArea(walkable_query_result[0])
393+
394+
395+
def _load_walkable_area_from_jupedsim_sqlite_v2(
396+
con: sqlite3.Connection,
397+
) -> WalkableArea:
398+
try:
399+
res = con.cursor().execute("SELECT wkt FROM geometry")
400+
geometries = [shapely.from_wkt(s) for s in res.fetchall()]
401+
except Exception as exc:
402+
raise LoadTrajectoryError(
403+
"The given sqlite trajectory is not a valid JuPedSim format, it does not not "
404+
"contain a 'geometry' table. Please check your file."
405+
) from exc
406+
407+
if not geometries:
408+
raise LoadTrajectoryError(
409+
"The given sqlite trajectory file seems not include a geometry. "
410+
"Please check your file."
411+
)
360412

361-
return WalkableArea(walkable_area)
413+
return WalkableArea(shapely.union_all(geometries))
362414

363415

364416
def load_trajectory_from_ped_data_archive_hdf5(
File renamed without changes.
3.83 MB
Binary file not shown.

0 commit comments

Comments
 (0)