前回の続き
Polygon
内周もあるポリゴンを作ってみましょう。Polygon()
の第二引数で内周のリストを指定します。
from shapely.geometry import LinearRing, Polygon
# define the exterior
outer = LinearRing([(-180, 90), (-180, -90), (180, -90), (180, 90)])
# define a hole:
hole = LinearRing([(-170, 80), (-100, -80), (100, -80), (170, 80)])
polygon_with_hole = Polygon(outer, [hole])
polygon_with_hole
Polygon properties and methods
Polygon
はいろいろな属性を持っています。print文の内容は教材からすこし変更しています。
print(f"重心: {polygon_with_hole.centroid}")
print(f"面積: {polygon_with_hole.area}")
print(f"minx, miny, maxx, maxy: {polygon_with_hole.bounds}")
print(f"exterior: {polygon_with_hole.exterior}")
print(f"exteriorr.coords: {list(polygon_with_hole.exterior.coords)}")
print(f"周囲(長さ): {polygon_with_hole.exterior.length}")
Check your understanding
多角形のお絵かきができます。
なお、円(真円)を描きたいときは、Polygon
ではなく、Point
を使います。
# Circle (using a buffer around a point)
point = Point((0,0))
point.buffer(1)
ちょっとだけ変形。結果は同じです。さて、distance
の1はどういう意味なのでしょうか。後ほど出てくるとかもですが。
# Circle (using a buffer around a point)
point = Point((0,0))
point.buffer(distance=1)
Geometry collections (optional)
optionalなので省略します。
まあ、Point
とかLinestring
とかPolygon
を複数まとめて管理できる、ということです。
Convex hull and envelope
Convex hull:凸包(convex hull)とは, 与えられた点をすべて包含する最小の凸多角形(凸多面体)のこと
凸多角形:単純な多角形であって、その内部または境界にある任意の二点間を結ぶ線分が、その多角形の外に出ることがないもの
とのことです。私もあまりよくわかっていませんが。
convex_hull
属性を取得しています。確かに、与えられた点をすべて包含する最小の凸多角形ですね。
次にenvelope
属性を取得します。convex_hullとは異なった値です。
envelopeについては、よくわかりませんでした。。。
Validity of geometries
Point
Linestring
Polygon
などは、それを定義しうる要件があります。その要件を満たすことをチェックする関数is_valid
が用意されています。
いったんここまでとします。次回はExercise 1です。