Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw an ellipse using Shapely

Tags:

python

shapely

I'm integrating Shapely into my code, and I have to deal with several different kinds of geometric objects. Most of my needs are satisfied with Lines, Polygons and LineStrings, but I need to use ellipses. Is there a way to create an ellipse in Shapely by a bounding box or by semi axis, without having to discretize the ellipse into lines?

like image 440
Ivan Avatar asked Oct 28 '12 02:10

Ivan


2 Answers

There isn't any way to represent a polygon in Shapely without discretizing it.

At the base level Shapely deals with points. Everything from a LineString to a Polygon is just a list of points. A good example of this is what happens when you take a Point and buffer it out:

>>> import shapely
>>> from shapely.geometry.point import Point
>>> p = Point(0, 0)
>>> circle = p.buffer(1.0)
>>> list(circle.exterior.coords)
[(1.0, 0.0), (0.99518472667219693, -0.098017140329560506), (0.98078528040323054, -0.19509032201612808), (0.95694033573220894, -0.29028467725446211), (0.92387953251128696, -0.38268343236508939), (0.88192126434835527, -0.4713967368259972), (0.83146961230254557, -0.55557023301960173), (0.77301045336273744, -0.63439328416364493), (0.70710678118654813, -0.70710678118654691), (0.63439328416364626, -0.77301045336273633), (0.55557023301960307, -0.83146961230254468), (0.47139673682599859, -0.88192126434835449), (0.38268343236509084, -0.92387953251128629), (0.29028467725446361, -0.95694033573220849), (0.19509032201612964, -0.98078528040323021), (0.098017140329562089, -0.99518472667219671), (1.615542552166338e-15, -1.0), (-0.098017140329558883, -0.99518472667219704), (-0.19509032201612647, -0.98078528040323076), (-0.2902846772544605, -0.95694033573220938), (-0.38268343236508784, -0.92387953251128752), (-0.4713967368259957, -0.88192126434835605), (-0.55557023301960051, -0.83146961230254635), (-0.63439328416364393, -0.77301045336273821), (-0.70710678118654624, -0.70710678118654879), (-0.77301045336273588, -0.63439328416364682), (-0.83146961230254435, -0.55557023301960362), (-0.88192126434835427, -0.47139673682599903), (-0.92387953251128618, -0.38268343236509111), (-0.95694033573220849, -0.29028467725446366), (-0.98078528040323021, -0.19509032201612947), (-0.99518472667219682, -0.098017140329561714), (-1.0, -1.010639055082363e-15), (-0.99518472667219693, 0.098017140329559702), (-0.98078528040323065, 0.1950903220161275), (-0.95694033573220905, 0.29028467725446172), (-0.92387953251128696, 0.38268343236508923), (-0.88192126434835527, 0.47139673682599725), (-0.83146961230254546, 0.55557023301960196), (-0.7730104533627371, 0.63439328416364527), (-0.70710678118654768, 0.70710678118654746), (-0.63439328416364593, 0.77301045336273666), (-0.55557023301960295, 0.83146961230254479), (-0.4713967368259987, 0.88192126434835449), (-0.38268343236509117, 0.92387953251128618), (-0.29028467725446411, 0.95694033573220838), (-0.19509032201613041, 0.98078528040322999), (-0.098017140329563102, 0.9951847266721966), (-2.8482262121737323e-15, 1.0), (0.098017140329557426, 0.99518472667219715), (0.19509032201612481, 0.9807852804032311), (0.29028467725445867, 0.95694033573220993), (0.3826834323650859, 0.9238795325112884), (0.47139673682599365, 0.88192126434835716), (0.55557023301959818, 0.8314696123025479), (0.63439328416364149, 0.77301045336274021), (0.70710678118654358, 0.70710678118655146), (0.77301045336273322, 0.63439328416365004), (0.83146961230254179, 0.5555702330196074), (0.88192126434835194, 0.47139673682600342), (0.92387953251128407, 0.38268343236509617), (0.95694033573220671, 0.29028467725446927), (0.98078528040322899, 0.19509032201613569), (0.99518472667219615, 0.098017140329568472), (1.0, 8.2385270480656025e-15), (1.0, 0.0)]

As you can see, the circle is made up of 65 points that are spaced 0.0966 units from each other.

like image 130
Nathan Villaescusa Avatar answered Oct 06 '22 08:10

Nathan Villaescusa


For those who are interested, here is an example to create an ellipse with axis length of 15 and 20.

import shapely.affinity
from shapely.geometry import Point

circle = Point(0, 0).buffer(1)  # type(circle)=polygon
ellipse = shapely.affinity.scale(circle, 15, 20)  # type(ellipse)=polygon
like image 28
Ken T Avatar answered Oct 06 '22 10:10

Ken T