How to find the nearest road segment given an global x, y position?

As titled, say I know the ego vehicle location, how can I get the nearest road segment in the map?

Currently, my solution would be to define a small shapely polygon around the ego vehicle and check if it has an intersection with the road segment polygon. Is there a better way to do this?

I guess the easiest approach is to get all records within x meters (say x=300) and then manually compute the distance. It may not be super precise due to the rasterization, but I suggest using get_map_mask() to get the binary road segment mask around the ego vehicle. On a binary mask you should be able to use standard algorithms to find the distance to the nearest road segment.

Perhaps you can use

    def get_records_in_radius(self, x: float, y: float, radius: float,
                              layer_names: List[str], mode: str = 'intersect') -> Dict[str, List[str]]:
2 Likes

Ah! That is a good idea, my approach is actually stem from it: I traced down the implementation of the get_map_mask() and found the polygon intersection check.

Will take a look. Thank you jerrickhoang!