Ask Your Question

(Shapely.geometry.Polygon to geometry_msgs.PolygonStamped) Shapely polygon representation in RViz

asked 2019-04-26 00:41:02 -0600

RalLpezMartn gravatar image

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!

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2020-09-11 09:02:35 -0600

UserK gravatar image

You 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 to fill the points list And this one to fill the details of each point

Hope this helps

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2019-04-26 00:41:02 -0600

Seen: 211 times

Last updated: Sep 11