Shapely 2.0.0 中文文档

  1. 主页
  2. 文档
  3. Shapely 2.0.0 中文文档
  4. User Manual
  5. Spatial Analysis Methods(空间分析方法)
  6. Constructive Methods(构造新对象的方法)

Constructive Methods(构造新对象的方法)

Shapely geometric object have several methods that yield new objects not derived from set-theoretic analysis.

Shapely的几何对象有几种方法可以构建新对象,而不是基于集合论的分析方法。

  • object.buffer(distancequad_segs=16cap_style=1join_style=1mitre_limit=5.0single_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.BufferCapStyle (see below).

端头的样式是由整数值指定的。1(圆),2(平),3(方)。这些值也被shapely.BufferCapStyle对象所枚举(见下文)。

The 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.BufferJoinStyle (see below).

偏移段之间的连接方式由整数值指定。1(圆形)、2(斜角)和3(斜面)。这些值也被shapely.BufferJoinStyle对象所枚举(见下文)。

  • shapely.BufferCapStyle
AttributeValue
round1
flat2
square3
  • shapely.BufferJoinStyle
AttributeValue
round1
mitre2
bevel3
>>> from shapely import BufferCapStyle, BufferJoinStyle
>>> BufferCapStyle.flat.value
2
>>> BufferJoinStyle.bevel.value
3

A positive distance has an effect of dilation; a negative distance, erosion. The optional quad_segs argument determines the number of segments used to approximate a quarter circle around a point.

正的距离有扩张的效果;负的距离有腐蚀的效果。可选的quad_segs 参数决定了用于近似包围一个点的四分之一圆的段数,即结果的光滑程度。

>>> 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 codepnghires.pngpdf)

Figure 9. Dilation of a line (left) and erosion of a polygon (right). New object is shown in blue.

The default (quad_segs of 16) buffer of a point is a polygonal patch with 99.8% of the area of the circular disk it approximates.

点的默认缓冲区是一个多边形图斑(quad_segs  参数为16),其面积为它所近似的圆形的99.8%。

>>> p = Point(0, 0).buffer(10.0)
>>> len(p.exterior.coords)
65
>>> p.area
313.6548490545941

With a quad_segs of 1, the buffer is a square patch.

在quad_segs 参数为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选项实现。

The 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 codepnghires.pngpdf)

Figure 10. Single sided buffer of 0.5 left hand (left) and of 0.3 right hand (right).图10. 0.5缓冲距离(左)和0.3缓冲距离(右)生成的单面缓冲区。

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 BufferCapStyle.flat.

点状几何要素的单边缓冲区与普通缓冲区相同。单面缓冲区的端部样式总是设定为BufferCapStyle.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
<MULTIPOLYGON (((0 0, 0 2, 1 1, 0 0)), ((1 1, 2 2, 2 0, 1 1)))>
>>> 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
<POINT (0 0)>
>>> MultiPoint([(0, 0), (1, 1)]).convex_hull
<LINESTRING (0 0, 1 1)>
>>> MultiPoint([(0, 0), (1, 1), (1, -1)]).convex_hull
<POLYGON ((1 -1, 0 0, 1 1, 1 -1))>

(Source codepnghires.pngpdf)

Figure 11. Convex hull (blue) of 2 points (left) and of 6 points (right).图11. 2个点(左)和6个点(右)的凸包(蓝色)。
  • 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
<POINT (0 0)>
>>> MultiPoint([(0, 0), (1, 1)]).envelope
<POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))>
  • 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
<POINT (0 0)>
>>> MultiPoint([(0,0),(1,1),(2,0.5)]).minimum_rotated_rectangle
<POLYGON ((2 0.5, 1.824 1.206, -0.176 0.706, 0 0, 2 0.5))>

(Source codepnghires.pngpdf)

Figure 12. Minimum rotated rectangle for a multipoint feature (left) and a linestring feature (right).图12. 多点要素(左)和线要素(右)的最小旋转矩形。
  • object.parallel_offset(distancesideresolution=16join_style=1mitre_limit=5.0)

Returns a LineString or MultiLineString geometry at a distance from the object on its right or its left side.

返回一个在其右边或左边与该对象有一定距离的LineString或MultiLineString几何对象。

Older alternative method to the offset_curve() method, but uses resolution instead of quad_segs and a side keyword (‘left’ or ‘right’) instead of sign of the distance. This method is kept for backwards compatibility for now, but is is recommended to use offset_curve() instead.

offset_curve()方法的旧版替代方法,但使用分辨率而不是quad_segs,使用侧面关键字(‘left’ or ‘right’)而不是距离的符号。为了向后兼容,目前保留了这个方法,但建议使用offset_curve()来代替。

  • object.offset_curve(distancequad_segs=16join_style=1mitre_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 float value.

距离参数必须是一个浮点数。

The side is determined by the sign of the distance parameter (negative for right side offset, positive for left side offset). Left and right are determined by following the direction of the given geometric points of the LineString.

side参数可以是’左’或’右’。左和右是按照LineString的给定几何点的方向来确定的。右边的偏移量以原始LineString或LineRing的相反方向得到,而左边的偏移量则以相同的方向得到。

The resolution of the offset around each vertex of the object is parameterized as in the buffer() method (using quad_segs).

对象的每个顶点周围的偏移量的分辨率resolution 与buffer()方法中的参数一样(使用quad_segs参数)。

The join_style is for outside corners between line segments. Accepted integer values are 1 (round), 2 (mitre), and 3 (bevel). See also shapely.BufferJoinStyle.

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 codepnghires.pngpdf)

Figure 13. Three styles of parallel offset lines on the left side of a simple line string (its starting point shown as a circle) and one offset on the right side, a multipart.图13. 简单线要素三种左侧平行偏移结果(其起点显示为圆圈)和右侧偏移结果(多部件)。

The effect of the mitre_limit parameter is shown below.

mitre_limit参数的效果如下所示。

(Source codepnghires.pngpdf)

Figure 14. Large and small mitre_limit values for left and right offsets.图14. 左右边移的大和小斜度限制值。
  • object.simplify(tolerancepreserve_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.1365484905459398
>>> len(x.exterior.coords)
65
>>> s = x.simplify(0.05, preserve_topology=False)
>>> s.area
3.061467458920719
>>> len(s.exterior.coords)
17

(Source codepnghires.pngpdf)

Figure 15. Simplification of a nearly circular polygon using a tolerance of 0.2 (left) and 0.5 (right).图15. 使用0.2(左)和0.5(右)的容差值对一个近似圆形的多边形进行简化。
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.
不保留拓扑结构的简化可能导致无效的几何对象,而且简化可能对坐标顺序敏感:两个仅在坐标顺序上不同的几何体可能被简化为不同结果。
标签 ,
这篇文章对您有用吗?

我们要如何帮助您?

欢迎留下您的宝贵建议

Please enter your comment!
Please enter your name here