简体   繁体   English

使用Shapely绘制一个椭圆

[英]Draw an ellipse using Shapely

I'm integrating Shapely into my code, and I have to deal with several different kinds of geometric objects. 我正在将Shapely集成到我的代码中,我必须处理几种不同类型的几何对象。 Most of my needs are satisfied with Lines, Polygons and LineStrings, but I need to use ellipses. 我的大多数需求都满足于Lines,Polygons和LineStrings,但我需要使用省略号。 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? 有没有办法通过边界框或半轴在Shapely中创建椭圆,而不必将椭圆离散成线?

There isn't any way to represent a polygon in Shapely without discretizing it. 没有任何方法可以在Shapely中表示多边形而不对其进行离散化。

At the base level Shapely deals with points. 在基础级别Shapely处理点数。 Everything from a LineString to a Polygon is just a list of points. 从LineString到Polygon的所有内容都只是一个点列表。 A good example of this is what happens when you take a Point and buffer it out: 一个很好的例子是当你拿一个Point并缓冲它时会发生什么:

>>> 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. 如您所见,圆圈由65个点组成,彼此间隔0.0966单位。

For those who are interested, here is an example to create an ellipse with axis length of 15 and 20. 对于那些感兴趣的人,这里有一个例子来创建一个轴长为15和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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM