Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add new points to existing Polygon in Shapely

Tags:

python

shapely

How do I modify an existing Polygon? For a start, I'd like to add a Point to its exterior.

poly = Polygon([(0, 0), (1, 1), (1, 0)])

I was looking for something like this:

poly.append_at(idx=3, Point(1, -1))

But I cannot find any even similar methods for doing this.

like image 884
racic Avatar asked Dec 29 '12 17:12

racic


1 Answers

It doesn't make sense to add or remove points from a Polygon's exterior, because you'd want to recalculate poly.area, poly.length, etc. anyway. Instead, create a new Polygon instance from the old polygon's coordinates:

coords = poly.exterior.coords[:]
coords[1] = (2.0, 6.0) # coordinate to change

new_poly = Polygon(coords)

Note that this doesn't account for points in poly.interior.

like image 171
Lynn Avatar answered Oct 13 '22 18:10

Lynn