ROS Answers: Open Source Q&A Forum - RSS feedhttps://answers.ros.org/questions/Open source question and answer forum written in Python and DjangoenROS Answers is licensed under Creative Commons Attribution 3.0Fri, 11 Sep 2020 09:02:35 -0500(Shapely.geometry.Polygon to geometry_msgs.PolygonStamped) Shapely polygon representation in RVizhttps://answers.ros.org/question/321957/shapelygeometrypolygon-to-geometry_msgspolygonstamped-shapely-polygon-representation-in-rviz/ I am working with a polygon done with Shapely and I want to represent it on RViz. Until now I've been using geometry_msg.PolygonStamped to plot all the polygons. Is there a easy way to transform from shapely.geometry.Polygon to geometry_msgs.PolygonStamped? If not, how can I get the points from the boundary of the Polygon in shapely? I've tried with polygon.xy (NotImplementedError) and polygon.exterior.coords.xy (several exterior attribute not defined, sometimes it work), any other way I can get those points from?
Thank you in advance!Fri, 26 Apr 2019 00:41:02 -0500https://answers.ros.org/question/321957/shapelygeometrypolygon-to-geometry_msgspolygonstamped-shapely-polygon-representation-in-rviz/Answer by UserK for <p>I am working with a polygon done with Shapely and I want to represent it on RViz. Until now I've been using geometry_msg.PolygonStamped to plot all the polygons. Is there a easy way to transform from shapely.geometry.Polygon to geometry_msgs.PolygonStamped? If not, how can I get the points from the boundary of the Polygon in shapely? I've tried with polygon.xy (NotImplementedError) and polygon.exterior.coords.xy (several exterior attribute not defined, sometimes it work), any other way I can get those points from?</p>
<p>Thank you in advance!</p>
https://answers.ros.org/question/321957/shapelygeometrypolygon-to-geometry_msgspolygonstamped-shapely-polygon-representation-in-rviz/?answer=361398#post-id-361398You should check the geometry type before trying to extract the boundaries.
With this you should cover all cases. Let's consider that you object is called multi.
if multi.geom_type == 'MultiPolygon':
for p in multi:
if p.geom_type == 'MultiPolygon':
print("MultiPolygon in MultiPolygon!!!")
for sp in p:
xd,yd = sp.exterior.coords.xy
elif p.geom_type in ['Point', 'LineString']:
xd,yd = p.xy
elif p.geom_type == 'MultiLineString':
for ea in p:
xd,yd = ea.xy
elif p.geom_type == 'Polygon':
xd,yd = p.exterior.coords.xy
Then check this [link](http://docs.ros.org/melodic/api/geometry_msgs/html/msg/Polygon.html) to fill the points list
And [this](http://docs.ros.org/melodic/api/geometry_msgs/html/msg/Point32.html) one to fill the details of each point
Hope this helpsFri, 11 Sep 2020 09:02:35 -0500https://answers.ros.org/question/321957/shapelygeometrypolygon-to-geometry_msgspolygonstamped-shapely-polygon-representation-in-rviz/?answer=361398#post-id-361398