使用Shapely绘制一个椭圆

时间:2012-10-28 02:32:57

标签: python shapely

我正在将Shapely集成到我的代码中,我必须处理几种不同类型的几何对象。我的大多数需求都满足于Lines,Polygons和LineStrings,但我需要使用省略号。有没有办法通过边界框或半轴形状地创建椭圆,而不必将椭圆离散成线?

干杯!

2 个答案:

答案 0 :(得分:15)

在没有离散化的情况下,没有任何方法可以在Shapely中表示多边形。

在基础级别Shapely处理积分。从LineString到Polygon的所有内容都只是一个点列表。这方面的一个很好的例子是当您使用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)]

如您所见,圆圈由65个点组成,彼此间隔0.0966个单位。

答案 1 :(得分:5)

对于那些感兴趣的人,这里有一个例子来创建一个轴长为15和20的椭圆。

from shapely.geometry.point import Point
import shapely.affinity

circle=Point(0,0).buffer(1) #type(circle)=polygon
ellipse=shapely.affinity.scale(circle,15,20) #type(ellipse)=polygon