Shapely geometric object have several methods that yield new objects not derived from set-theoretic analysis.
Shapely的几何对象有几种方法可以构建新对象,而不是基于集合论的分析方法。
- object.buffer(distance, resolution=16, cap_style=1, join_style=1, mitre_limit=5.0, single_sided=False)
Returns an approximate representation of all points within a given distance of the this geometric object.
The styles of caps are specified by integer values: 1 (round), 2 (flat), 3 (square). These values are also enumerated by the object shapely.geometry.CAP_STYLE
(see below).
he styles of joins between offset segments are specified by integer values: 1 (round), 2 (mitre), and 3 (bevel). These values are also enumerated by the object shapely.geometry.JOIN_STYLE
(see below).
- shapely.geometry.CAP_STYLE
Attribute | Value |
round | 1 |
flat | 2 |
square | 3 |
- shapely.geometry.JOIN_STYLE
Attribute | Value |
round | 1 |
mitre | 2 |
bevel | 3 |
>>> from shapely.geometry import CAP_STYLE, JOIN_STYLE
>>> CAP_STYLE.flat
2
>>> JOIN_STYLE.bevel
3
A positive distance has an effect of dilation; a negative distance, erosion. The optional resolution argument determines the number of segments used to approximate a quarter circle around a point.
正的距离有扩张的效果;负的距离有腐蚀的效果。可选的resolution 参数决定了用于近似包围一个点的四分之一圆的段数,即结果的光滑程度。
>>> line = LineString([(0, 0), (1, 1), (0, 2), (2, 2), (3, 1), (1, 0)])
>>> dilated = line.buffer(0.5)
>>> eroded = dilated.buffer(-0.3)
(Source code, png, hires.png, pdf)
The default (resolution of 16) buffer of a point is a polygonal patch with 99.8% of the area of the circular disk it approximates.
点的默认缓冲区是一个多边形图斑(resolution 参数为16),其面积为它所近似的圆形的99.8%。
>>> p = Point(0, 0).buffer(10.0)
>>> len(p.exterior.coords)
66
>>> p.area
313.65484905459385
With a resolution of 1, the buffer is a square patch.
在resolution参数为1的情况下,缓冲区是一个矩形。
>>> q = Point(0, 0).buffer(10.0, 1)
>>> len(q.exterior.coords)
5
>>> q.area
200.0
You may want a buffer only on one side. You can achieve this effect with single_sided option.
你可能只想在一侧生成缓冲区。可以用single_sided选项实现。
he side used is determined by the sign of the buffer distance:
根据缓冲距离的符号决定生成缓冲区的方向。
- a positive distance indicates the left-hand side(正的距离表示左边)
- a negative distance indicates the right-hand side(正的距离表示右边)
>>> line = LineString([(0, 0), (1, 1), (0, 2), (2, 2), (3, 1), (1, 0)])
>>> left_hand_side = line.buffer(0.5, single_sided=True)
>>> right_hand_side = line.buffer(-0.3, single_sided=True)
(Source code, png, hires.png, pdf)
The single-sided buffer of point geometries is the same as the regular buffer. The End Cap Style for single-sided buffers is always ignored, and forced to the equivalent of CAP_STYLE.flat.
点状几何要素的单边缓冲区与普通缓冲区相同。单面缓冲区的端部样式总是设定为CAP_STYLE.flat。
Passed a distance of 0, buffer()
can sometimes be used to “clean” self-touching or self-crossing polygons such as the classic “bowtie”. Users have reported that very small distance values sometimes produce cleaner results than 0. Your mileage may vary when cleaning surfaces.
传递一个0的距离,buffer()操作有时可以用来清除自接触或自交叉的多边形,如经典的 “领结”形式。用户报告说,非常小的距离值有时会产生比0更干净的结果。在清除此类多边形时,依实际情况可能有差别。
>>> coords = [(0, 0), (0, 2), (1, 1), (2, 2), (2, 0), (1, 1), (0, 0)]
>>> bowtie = Polygon(coords)
>>> bowtie.is_valid
False
>>> clean = bowtie.buffer(0)
>>> clean.is_valid
True
>>> clean
<shapely.geometry.multipolygon.MultiPolygon object at ...>
>>> len(clean.geoms)
2
>>> list(clean.geoms[0].exterior.coords)
[(0.0, 0.0), (0.0, 2.0), (1.0, 1.0), (0.0, 0.0)]
>>> list(clean.geoms[1].exterior.coords)
[(1.0, 1.0), (2.0, 2.0), (2.0, 0.0), (1.0, 1.0)]
Buffering splits the polygon in two at the point where they touch.
缓冲区操作将多边形在它们接触的地方一分为二。
- object.convex_hull
Returns a representation of the smallest convex Polygon containing all the points in the object unless the number of points in the object is less than three. For two points, the convex hull collapses to a LineString; for 1, a Point.
返回包含对象中所有点的最小凸包,除非对象中点的数量少于3。对于两个点,凸面体退化为LineString;对于1个点,为Point。
>>> Point(0, 0).convex_hull
<shapely.geometry.point.Point object at 0x...>
>>> MultiPoint([(0, 0), (1, 1)]).convex_hull
<shapely.geometry.linestring.LineString object at 0x...>
>>> MultiPoint([(0, 0), (1, 1), (1, -1)]).convex_hull
<shapely.geometry.polygon.Polygon object at 0x...>
(Source code, png, hires.png, pdf)
- object.envelope
Returns a representation of the point or smallest rectangular polygon (with sides parallel to the coordinate axes) that contains the object.
返回包含该对象的点或最小的外包矩形(边与坐标轴平行)。
>>> Point(0, 0).envelope
<shapely.geometry.point.Point object at 0x...>
>>> MultiPoint([(0, 0), (1, 1)]).envelope
<shapely.geometry.polygon.Polygon object at 0x...>
- object.minimum_rotated_rectangle
Returns the general minimum bounding rectangle that contains the object. Unlike envelope this rectangle is not constrained to be parallel to the coordinate axes. If the convex hull of the object is a degenerate (line or point) this degenerate is returned.
返回包含该对象的最小外接矩形。与envelope不同的是,这个矩形不需与坐标轴平行。如果对象的凸包是一个退化物(线或点),则返回该退化物(线或点)。
New in Shapely 1.6.0
>>> Point(0, 0).minimum_rotated_rectangle
<shapely.geometry.point.Point object at 0x...>
>>> MultiPoint([(0,0),(1,1),(2,0.5)]).minimum_rotated_rectangle
<shapely.geometry.polygon.Polygon object at 0x...>
(Source code, png, hires.png, pdf)
- object.parallel_offset(distance, side, resolution=16, join_style=1, mitre_limit=5.0)
Returns a LineString or MultiLineString geometry at a distance from the object on its right or its left side.
返回一个在其右边或左边与该对象有一定距离的LineString或MultiLineString几何对象。
The distance parameter must be a positive float value.
距离参数必须是一个正的浮动数。
The side parameter may be ‘left’ or ‘right’. Left and right are determined by following the direction of the given geometric points of the LineString. Right hand offsets are returned in the reverse direction of the original LineString or LineRing, while left side offsets flow in the same direction.
side参数可以是’左’或’右’。左和右是按照LineString的给定几何点的方向来确定的。右边的偏移量以原始LineString或LineRing的相反方向得到,而左边的偏移量则以相同的方向得到。
The resolution of the offset around each vertex of the object is parameterized as in the buffer()
method.
对象的每个顶点周围的偏移量的分辨率resolution 与buffer()方法中的参数一样。
The join_style is for outside corners between line segments. Accepted integer values are 1 (round), 2 (mitre), and 3 (bevel). See also shapely.geometry.JOIN_STYLE
.
join_style是用于设置线段之间的外角。接受的整数值是1(圆形)、2(斜角)和3(斜面)。另请参阅shapely.geometry.JOIN_STYLE。
Severely mitered corners can be controlled by the mitre_limit parameter (spelled in British English, en-gb). The corners of a parallel line will be further from the original than most places with the mitre join style. The ratio of this further distance to the specified distance is the miter ratio. Corners with a ratio which exceed the limit will be beveled.
精确的斜接角可以通过mitre_limit参数来控制。平行线的转角将比大多数斜接样式离原对象更远。这个更远的距离与指定距离的比率是斜接比率。比值超过极限的角将被斜接。
Note |
This method may sometimes return a MultiLineString where a simple LineString was expected; for example, an offset to a slightly curved LineString. 该方法有时可能会返回一个MultiLineString,而非预期的一个简单的LineString;例如,一个稍微弯曲的LineString的偏移。 |
Note |
This method is only available for LinearRing and LineString objects. 该方法仅适用于LinearRing和LineString对象。 |
(Source code, png, hires.png, pdf)
The effect of the mitre_limit parameter is shown below.
mitre_limit参数的效果如下所示。
(Source code, png, hires.png, pdf)
- object.simplify(tolerance, preserve_topology=True)
Returns a simplified representation of the geometric object.
返回几何对象的简化表示。
All points in the simplified object will be within the tolerance distance of the original geometry. By default a slower algorithm is used that preserves topology. If preserve topology is set to False
the much quicker Douglas-Peucker algorithm [6] is used.
简化对象中的所有点都将在原始几何体的容错距离之内。默认情况下,会使用一种较慢的算法来保留拓扑结构。如果保留拓扑结构被设置为 “False”,就会使用更快的Douglas-Peucker算法。
>>> p = Point(0.0, 0.0)
>>> x = p.buffer(1.0)
>>> x.area
3.1365484905459389
>>> len(x.exterior.coords)
66
>>> s = x.simplify(0.05, preserve_topology=False)
>>> s.area
3.0614674589207187
>>> len(s.exterior.coords)
17
(Source code, png, hires.png, pdf)
Note |
Invalid geometric objects may result from simplification that does not preserve topology and simplification may be sensitive to the order of coordinates: two geometries differing only in order of coordinates may be simplified differently. 不保留拓扑结构的简化可能导致无效的几何对象,而且简化可能对坐标顺序敏感:两个仅在坐标顺序上不同的几何体可能被简化为不同结果。 |