Filtering disconnected components of a MultiLineString
's intersection with a Polygon
?
#2267
-
This is more of a question than an issue, but figured I would give it a shot. I am trying to simulate a lidar sensor by taking a lidar_lines = MultiLineString(lidar_coords)
lidar_lines = lidar_lines.intersection(free_space_polygon) (by the way, As you can see, there are several undesired line segments that are disjoint and "behind" the walls w.r.t. the center of the robot. I can filter this manually by looping through lidar_line_coords = [
geom.coords for
geom in lidar_lines.intersection(free_space_polygon).geoms
if geom.intersects(robot_polygon)
] Are there ways to avoid having to do this filtering? I haven't really been able to find this in the Shapely docs. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Just get the parts that intersect using vectorised API. parts = shapely.get_parts(lidar_lines)
clean_lidar_lines = parts[shapely.intersects(parts, robot_polygon)] |
Beta Was this translation helpful? Give feedback.
-
This might give another easy/simple speedup... import shapely
parts = shapely.get_parts(lidar_lines)
shapely.prepare(robot_polygon)
clean_lidar_lines = parts[shapely.intersects(robot_polygon, parts)] You could also try to use a spatial index, but I'd guess this will probably not be useful for this case. If you would want to have a look, more info here: https://shapely.readthedocs.io/en/stable/strtree.html |
Beta Was this translation helpful? Give feedback.
Just get the parts that intersect using vectorised API.