Locator
Create a Locator object¶
In [1]:
Copied!
import omega_prime
import numpy as np
import omega_prime
import numpy as np
In [ ]:
Copied!
r = omega_prime.Recording.from_file(
"../../example_files/pedestrian.osi", map_path="../../example_files/fabriksgatan.xodr", apply_proj=False
)
r = omega_prime.Recording.from_file(
"../../example_files/pedestrian.osi", map_path="../../example_files/fabriksgatan.xodr", apply_proj=False
)
In [3]:
Copied!
locator = omega_prime.Locator.from_map(r.map)
locator = omega_prime.Locator.from_map(r.map)
Locate an object on the Map¶
In [4]:
Copied!
mv = r.moving_objects[1]
mv
mv = r.moving_objects[1]
mv
Out[4]:
<omega_prime.recording.MovingObject at 0x1e92d67d910>
In [16]:
Copied!
sts = locator.locate_mv(mv)
print(
f"s[0]: {np.asarray(sts.s[0])}\nt[0]: {np.asarray(sts.t[0])}\nroadlane_id[0]: {np.asarray(sts.roadlane_id[0])}\ntime[0]: {np.asarray(sts.time[0])}"
)
sts = locator.locate_mv(mv)
print(
f"s[0]: {np.asarray(sts.s[0])}\nt[0]: {np.asarray(sts.t[0])}\nroadlane_id[0]: {np.asarray(sts.roadlane_id[0])}\ntime[0]: {np.asarray(sts.time[0])}"
)
s[0]: 14.864804534670384 t[0]: -0.49999423398891507 roadlane_id[0]: XodrLaneId(road_id='0', lane_id=3, section_id=0) time[0]: 0
In [17]:
Copied!
sts_poly = locator.locate_mv(mv, use_polygon=True)
print(
f"s[0]: {np.asarray(sts_poly.s[0])}\nt[0]: {np.asarray(sts_poly.t[0])}\nroadlane_id[0]: {np.asarray(sts_poly.roadlane_id[0])}\ntime[0]: {np.asarray(sts_poly.time[0])}"
)
sts_poly = locator.locate_mv(mv, use_polygon=True)
print(
f"s[0]: {np.asarray(sts_poly.s[0])}\nt[0]: {np.asarray(sts_poly.t[0])}\nroadlane_id[0]: {np.asarray(sts_poly.roadlane_id[0])}\ntime[0]: {np.asarray(sts_poly.time[0])}"
)
s[0]: 14.864804534670384 t[0]: -0.49999423398891507 roadlane_id[0]: XodrLaneId(road_id='0', lane_id=3, section_id=0) time[0]: 0
Precision loss of Locator¶
In [18]:
Copied!
derived_xys = locator.sts2xys(sts)
derived_xys_poly = locator.sts2xys(sts_poly)
derived_xys_poly.shape
derived_xys = locator.sts2xys(sts)
derived_xys_poly = locator.sts2xys(sts_poly)
derived_xys_poly.shape
Out[18]:
(434, 2)
In [19]:
Copied!
np.max(np.linalg.norm(derived_xys_poly - mv.df["x", "y"].to_numpy(), axis=1))
np.max(np.linalg.norm(derived_xys_poly - mv.df["x", "y"].to_numpy(), axis=1))
Out[19]:
np.float64(2.0727719536908503e-05)
In [22]:
Copied!
# %matplotlib ipympl
# pip install ipympl
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.patches import Polygon as PltPolygon
_, ax = plt.subplots(1, 1)
for lid, l in r.map.lanes.items():
ax.add_patch(PltPolygon(l.polygon.exterior.coords, fc="blue", alpha=0.2, ec="blue"))
ax.plot(*mv.df["x", "y"].to_numpy().T, label="xys", c="green")
ax.plot(*derived_xys_poly.T, label="derived_xys_based_on_polygon", c="yellow", linestyle="dashed")
ax.plot(*derived_xys.T, label="derived_xys", c="red", linestyle="dotted")
ax.autoscale()
ax.set_aspect(1)
ax.legend(loc="lower center")
ax.set_xlim(-10, 50)
ax.set_ylim(-75, 0)
# %matplotlib ipympl
# pip install ipympl
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.patches import Polygon as PltPolygon
_, ax = plt.subplots(1, 1)
for lid, l in r.map.lanes.items():
ax.add_patch(PltPolygon(l.polygon.exterior.coords, fc="blue", alpha=0.2, ec="blue"))
ax.plot(*mv.df["x", "y"].to_numpy().T, label="xys", c="green")
ax.plot(*derived_xys_poly.T, label="derived_xys_based_on_polygon", c="yellow", linestyle="dashed")
ax.plot(*derived_xys.T, label="derived_xys", c="red", linestyle="dotted")
ax.autoscale()
ax.set_aspect(1)
ax.legend(loc="lower center")
ax.set_xlim(-10, 50)
ax.set_ylim(-75, 0)
Out[22]:
(-75.0, 0.0)
In [21]:
Copied!
sts11 = locator.xys2lane_sts(lane_id=omega_prime.map_odr.XodrLaneId("0", 1, 0), xys=mv.df["x", "y"].to_numpy())
sts11.shape
sts11 = locator.xys2lane_sts(lane_id=omega_prime.map_odr.XodrLaneId("0", 1, 0), xys=mv.df["x", "y"].to_numpy())
sts11.shape
Out[21]:
(434, 2)