Shapely is a python module for manipulation and analysis of planar geometric objects.
Installation Execute a command from either of the followings: conda install -c scitools shapely pip install shapely
以下,ユーザマニュアルの要約をダラダラ書いていく. 以下のコマンドで和集合の面積が計算できる.これを使えば,劣モジュラ関数の実装が簡単にできるかも
from shapely.ops import cascaded_union polygons = [Point(i, 0).buffer(0.7) for i in range(5)] print 'area of the union is ', cascaded_union(polygons).area
- 基本型
幾何学的オブジェクトの基本的な型は3種類.
各オブジェクトはinterior (内部),boundary (境界),exterior (外部)の3要素を持っている.3要素は互いに排他的で,全要素のunionは平面全体を表す.
- point (点)
- shape.geometry.Point
- interior: 1 (つまり,その点自身を指す)
- boundary: 0
- exterior: 自身以外の全ての点
- 次元数: 0
- curve (曲線)
- shape.geometry.LineString or shape.geometry.LinearRing
- interior: 少なくとも2点.曲線上に存在する点を無限に保持できる
- boundary: 2 (曲線の端点の2点)
- exterior: interiorとboundary以外の全ての点
- 次元数: 1
- 滑らかな曲線ではない
- surface (表面)
- shape.geometry.Polygon
- interior: ある領域内にある点を無限に保持できる
- boundary: 1つ以上のcurve
- exterior: interiorとboundary以外の全ての点.表面内に存在する穴も含める.
- 次元数: 2
- point (点)
- 基本型の集合
複数の基本型オブジェクトをまとめて扱える
- MultiPoint (複数のpoint)
- MultiLineString (複数のcurve)
- MultiPolygon (複数のsurface)
- 座標
- shapelyは座標システムの変換はサポートしていない.
- コンストラクタによって,指定された座標値はfloat型に変換される
- 幾何学的オブジェクト オブジェクトを生成する段階でz座標を指定することは可能だが,幾何学的解析は常にx-y平面で行われる.
- 一般的な属性と関数
- object.area objectの面積を返す
- object.bounds objectの境界(x座標の最小値,y座標の最小値,x座標の最大値,y座標の最大値)をfloatのtupleで返す
- object.length objectの長さを返す
- object.geom_type 型の名前(Point, LineStringなど)を文字列で返す
- object.distance(aother_object) objectとaother_objectの間の最小距離を返す
- object.representative_point() object内に存在すると保証される点を返す
- 各型について
- Points
- LineStrings
- コンストラクタで順序付けられた2個以上の点のtupleを指定 line = LineString([(0,0), (1,1)])
- 面積は0,長さは非ゼロ
- coordsによって全点の座標値が得られる
- coordsはスライス可能 line.coords[1]
- LinearRings
- コンストラクタで順序付けられた点のtupleを指定 ring = LinearRing([(0,0), (1,1), (1,0),(0,0)])
- 面積は0,長さは非ゼロ
- coordsによって全点の座標値が得られる
- coordsはスライス可能 line.coords[1]
- Polygons
- コンストラクタで2つの引数を指定
- 順序付けられた点のtuple.LinearRingsのように指定 (必須)
- 順序付けられていない点のtuple.interiorの境界や穴 (オプション) polygon_ext = [(0, 0), (0, 2), (2, 2), (2, 0), (0, 0)] polygon_int = [(1, 0), (0.5, 0.5), (1, 1), (1.5, 0.5), (1, 0)] polygon = Polygon(polygon_ext, [polygon_int])
- 面積,長さはどちらも非ゼロ
- coordsによって全点の座標値が得られる polygon.exterior.coords
- コンストラクタで2つの引数を指定
- shapely.geometry.box(minx, miny, maxx, maxy, ccw=True)
- 長方形型polygon
- デフォルトで半時計回りに点の座標が記憶される
- Collections
- MultiPoint
- MultiLineString
- 複数のcurve
- コンストラクタで線のようなシーケンスかオブジェクトを複数指定
- MultiPolygon
- 複数のpolygon
- コンストラクタでexterior ringとholeのlistのtupleを指定 c = ... d = ... multi = MultiPolygon([[c, ], [d, ]])
- Predicates and relationships
上述した幾何学的オブジェクトは属性として標準predicatesを持っている.
全てのpredicateがTrueかFalseを返す.
- Unary predicates
単一の幾何学的オブジェクトに関する属性
- object.has_z z座標値を含む場合Trueを返す.
- object.is_ccw 座標系が右手座標系?だとTrueを返す. LinearRingオブジェクトのみが持つ属性
- object.is_empty objectのinteriorとboundaryが空集合ならTrueを返す.
- object.is_ring objectが閉じているならTrueを返す
- object.is_simple object自身が交差していなければTrueを返す.
- object.is_valid objectがvalidであればTrueを返す.validの定義はLinearRingやPolygonそれぞれで異なる.
- Binary predicates
二つの幾何学的オブジェクトに関する属性に関する関数.
もう一つの幾何学的オブジェクトを引数とし,TrueかFalseを返す関数.
- object.almost_equals(other[, decimal=6]) 2つのオブジェクトがだいたい同じならTrueを返す.
- object.contains(other) objectのinteriorがotherのboundaryとinteriorを含んでいればTrueを返す.
- object.crosses(other) objectのinteriorがotherのinteriorと接する時,もしくはintersectionの次元がオブジェクトの次元より小さければTrueを返す.
- object.disjoint(other) objectのboundaryとinteriorがotherのboundaryとinteriorと接しなければTrueを返す.
- object.equals(other) boundary, interior,exteriorが集合論的に同じならTrueを返す?
- object.intersects(other) objectのboundaryとinteriorがotherのboundaryとinteriorとintersectすればTrueを返す.
- object.touches(other) objectとotherが少なくとも1点共有し,かつ両objectのinteriorが共有点以外どこもintersectしない場合にTrueを返す.
- object.within(other) objectのboundaryとinteriorが,otherのotherのinteriorのみとintersectする時にTrueを返す.
- Unary predicates
単一の幾何学的オブジェクトに関する属性
- Spatial Analysis Methods
以下の属性・関数は,新しい幾何学的オブジェクトを返す.
- Set-theoretic Methods
集合論的手法
- object.boundary objectの集合論的boundaryを表す,より低次元なオブジェクトを返す. polygonのboundaryはline.lineのboundaryはpointの集合.
- object.centroid objectの幾何学的中心を表す点を返す.
- object.difference(other) objectからobjectとotherの共通部分を取り除いた幾何学的オブジェクトを返す.
- object.intersection(other) objectとotherの積集合(intersection)を表すオブジェクトを返す.
- object.symmetric_difference(other) objectとotherの排他的論理和がTrueとなる領域を表すオブジェクトを返す.
- object.union(other) objectとotherの和集合(unionを表すオブジェクトを返す.
- Constructive Methods
集合論的手法とな異なる方法で新しい幾何学的オブジェクトを生成する方法.
- object.buffer(distance, resolution=16, cap_style=1, join_style=1,mitre_limit=1.0) 全点がobjectから指定したdistance以内の距離を持つオブジェクトを返す. objectがPointであれば,円になる.
- object.convex_hull 3点以上の点を含むオブジェクトに対して,全店を含む最小の凸形状を持つPolygonを返す. 2点に対してはLineStringを返す.
- object.simplify(tolerance, preserve_topology=True) objectを簡略化したオブジェクトを返す. 簡略化されたオブジェクトの全点は元のオブジェクトから距離がtolerance以内に離れた距離に含まれる. 例えば,円が多角形として簡略化される.
- Set-theoretic Methods
集合論的手法
- Other Operations
- Merging Linear Features shapely.opsモジュールの関数を使うと,線が接している複数のMultiLineStringや複数のPolygonへ統合できる.
- Cascading Unions union()を蓄積するより効率的に和集合を扱える.
*1:0.0, 0.0