I am Charmie

メモとログ

Python module: shapely

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
  1. 基本型 幾何学的オブジェクトの基本的な型は3種類. 各オブジェクトはinterior (内部),boundary (境界),exterior (外部)の3要素を持っている.3要素は互いに排他的で,全要素のunionは平面全体を表す.
    1. point (点)
      1. shape.geometry.Point
      2. interior: 1 (つまり,その点自身を指す)
      3. boundary: 0
      4. exterior: 自身以外の全ての点
      5. 次元数: 0
    2. curve (曲線)
      1. shape.geometry.LineString or shape.geometry.LinearRing
      2. interior: 少なくとも2点.曲線上に存在する点を無限に保持できる
      3. boundary: 2 (曲線の端点の2点)
      4. exterior: interiorとboundary以外の全ての点
      5. 次元数: 1
      6. 滑らかな曲線ではない
    3. surface (表面)
      1. shape.geometry.Polygon
      2. interior: ある領域内にある点を無限に保持できる
      3. boundary: 1つ以上のcurve
      4. exterior: interiorとboundary以外の全ての点.表面内に存在する穴も含める.
      5. 次元数: 2
  2. 基本型の集合 複数の基本型オブジェクトをまとめて扱える
    1. MultiPoint (複数のpoint)
    2. MultiLineString (複数のcurve)
    3. MultiPolygon (複数のsurface)
  3. 座標
    1. shapelyは座標システムの変換はサポートしていない.
    2. コンストラクタによって,指定された座標値はfloat型に変換される
  4. 幾何学的オブジェクト オブジェクトを生成する段階でz座標を指定することは可能だが,幾何学的解析は常にx-y平面で行われる.
  5. 一般的な属性と関数
    1. object.area objectの面積を返す
    2. object.bounds objectの境界(x座標の最小値,y座標の最小値,x座標の最大値,y座標の最大値)をfloatのtupleで返す
    3. object.length objectの長さを返す
    4. object.geom_type 型の名前(Point, LineStringなど)を文字列で返す
    5. object.distance(aother_object) objectとaother_objectの間の最小距離を返す
    6. object.representative_point() object内に存在すると保証される点を返す
  6. 各型について
    1. Points
      1. コンストラクタで座標値を指定 point = Point(0.0, 0.0) point = Point*1
      2. 面積と長さは0
      3. 全座標値はcoords,各座標の座標値はx,yでアクセス可能 point.coords point.x point.y
    2. LineStrings
      1. コンストラクタで順序付けられた2個以上の点のtupleを指定 line = LineString([(0,0), (1,1)])
      2. 面積は0,長さは非ゼロ
      3. coordsによって全点の座標値が得られる
      4. coordsはスライス可能 line.coords[1]
    3. LinearRings
      1. コンストラクタで順序付けられた点のtupleを指定 ring = LinearRing([(0,0), (1,1), (1,0),(0,0)])
      2. 面積は0,長さは非ゼロ
      3. coordsによって全点の座標値が得られる
      4. coordsはスライス可能 line.coords[1]
    4. Polygons
      1. コンストラクタで2つの引数を指定
        1. 順序付けられた点のtuple.LinearRingsのように指定 (必須)
        2. 順序付けられていない点の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])
      2. 面積,長さはどちらも非ゼロ
      3. coordsによって全点の座標値が得られる polygon.exterior.coords
    5. shapely.geometry.box(minx, miny, maxx, maxy, ccw=True)
      1. 長方形型polygon
      2. デフォルトで半時計回りに点の座標が記憶される
    6. Collections
    7. MultiPoint
      1. 複数の点
      2. コンストラクタで複数の点のtupleを指定
      3. 面積と長さは0
      4. points.boundsはx,y方向の最小値・最大値をtupleとして返す
      5. 各点へのアクセスはgeoms属性,もしくはin,list()を使ったiterator for p in points:     print p.x, p.y
    8. MultiLineString
      1. 複数のcurve
      2. コンストラクタで線のようなシーケンスかオブジェクトを複数指定
    9. MultiPolygon
      1. 複数のpolygon
      2. コンストラクタでexterior ringとholeのlistのtupleを指定 c = ... d = ... multi = MultiPolygon([[c, ], [d, ]])

 

  1. Predicates and relationships 上述した幾何学的オブジェクトは属性として標準predicatesを持っている. 全てのpredicateがTrueかFalseを返す.
    1. Unary predicates 単一の幾何学的オブジェクトに関する属性
      1. object.has_z z座標値を含む場合Trueを返す.
      2. object.is_ccw 座標系が右手座標系?だとTrueを返す. LinearRingオブジェクトのみが持つ属性
      3. object.is_empty objectinteriorboundary空集合ならTrueを返す.
      4. object.is_ring objectが閉じているならTrueを返す
      5. object.is_simple object自身が交差していなければTrueを返す.
      6. object.is_valid objectがvalidであればTrueを返す.validの定義はLinearRingやPolygonそれぞれで異なる.
    2. Binary predicates 二つの幾何学的オブジェクトに関する属性に関する関数. もう一つの幾何学的オブジェクトを引数とし,TrueFalseを返す関数.
      1. object.almost_equals(other[, decimal=6]) 2つのオブジェクトがだいたい同じならTrueを返す.
      2. object.contains(other) objectinteriorotherboundaryinteriorを含んでいればTrueを返す.
      3. object.crosses(other) objectinteriorotherinteriorと接する時,もしくはintersectionの次元がオブジェクトの次元より小さければTrueを返す.
      4. object.disjoint(other) objectboundaryinteriorotherboundaryinteriorと接しなければTrueを返す.
      5. object.equals(other) boundary, interior,exterior集合論的に同じならTrueを返す?
      6. object.intersects(other) objectboundaryinteriorotherboundaryinteriorintersectすればTrueを返す.
      7. object.touches(other) objectotherが少なくとも1点共有し,かつ両objectinteriorが共有点以外どこもintersectしない場合にTrueを返す.
      8. object.within(other) objectboundaryinteriorが,otherotherinteriorのみとintersectする時にTrueを返す.
  2. Spatial Analysis Methods 以下の属性・関数は,新しい幾何学的オブジェクトを返す.
    1. Set-theoretic Methods 集合論的手法
      1. object.boundary object集合論的boundaryを表す,より低次元なオブジェクトを返す. polygonboundaryline.lineboundarypointの集合.
      2. object.centroid object幾何学的中心を表す点を返す.
      3. object.difference(other) objectからobjectotherの共通部分を取り除いた幾何学的オブジェクトを返す.
      4. object.intersection(other) objectotherの積集合(intersection)を表すオブジェクトを返す.
      5. object.symmetric_difference(other) objectとother排他的論理和がTrueとなる領域を表すオブジェクトを返す.
      6. object.union(other) objectotherの和集合(unionを表すオブジェクトを返す.
    2. Constructive Methods 集合論的手法とな異なる方法で新しい幾何学的オブジェクトを生成する方法.
      1. object.buffer(distance, resolution=16, cap_style=1, join_style=1,mitre_limit=1.0) 全点がobjectから指定したdistance以内の距離を持つオブジェクトを返す. objectPointであれば,円になる.
      2. object.convex_hull 3点以上の点を含むオブジェクトに対して,全店を含む最小の凸形状を持つPolygonを返す. 2点に対してはLineStringを返す.
      3. object.simplify(tolerance, preserve_topology=True) objectを簡略化したオブジェクトを返す. 簡略化されたオブジェクトの全点は元のオブジェクトから距離がtolerance以内に離れた距離に含まれる. 例えば,円が多角形として簡略化される.
  3. Other Operations
    1. Merging Linear Features shapely.opsモジュールの関数を使うと,線が接している複数のMultiLineStringや複数のPolygonへ統合できる.
      1. shapely.ops.polygonize(lines)
      2. shapely.ops.polygonize_full(lines)
      3. shapely.ops.linemerge(lines)
    2. Cascading Unions union()を蓄積するより効率的に和集合を扱える.
      1. shapely.ops.cascaded_union(geoms) 与えられた幾何学的オブジェクトの和集合を返す. polygons = [Point(i, 0).buffer(0.7) for i in range(5)] cascaded_union(polygons)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

*1:0.0, 0.0