前回の続き。
Lines
LineString properties and methods
前回の復習。複数の点から線を形成したので、線の座標=複数の点です。
# Get coordinate tuples
list(line.coords)
単一の点ではありません。単一の点では線にならない、という当たり前の話です。
x座標とy座標を分けることもできます。 xy[0]がx座標、xy[1]がyz座標です。
また、線なので長さが存在します。lengthに格納されています。
さて、この長さですが、a:点間距離の合計、b:始点→終点の直線距離なのでしょうか?
2.82842... ≒sqrt(2)*2なので、a:点間距離の合計でした。
line3 = LineString([(1, 1), (2,2), (3,1)])
line3.length
あと、線の重心はcentroidで求められます。
分かりやすくするために、先ほど作成したline3の重心を求めます。
Polygon
なお、この章は何かいっぱい書いてありますが、大半は help(Polygon)の出力のコピペです。。。
Creating a polygon geometry follows the same logic as creating a point or line geometry. However, as discussed above the rules for what constitutes a polygon are more complex: It is constructed of exactly one linear ring forming its exterior (perimeter), and any number of additional linear rings forming holes that are cut out of the exterior shell.
Consequently, the shapely.geometry.Polygon constructor function accepts two parameter: the first one, shell, is a list of coordinate tuples, a list of points, or a LinearRing, and will form the outer hull of the new polygon. The second, optional, parameter holes can be a list of holes to cut out of shell (the items in the list can be the same data types as shell).
ざっくりいうと、ポリゴン=外周と内周からなる多角形(内周のリングは必須ではない)かと思います。
ひとまず、必須の外周を引数に指定します。
from shapely.geometry import Polygon
# Create a Polygon from the coordinates
polygon1 = Polygon([(2.2, 4.2), (7.2, -25.1), (9.26, -2.456)])
LineStringと同じように、Pointのリストから作成することもできます。
polygon2 = Polygon([point1, point2, point3])
座標を見ましょう。
list(polygon1.coords)
エラーになりました。
NotImplementedError: Component rings have coordinate sequences, but the polygon does not
先ほどふれたとおり、ポリゴン=外周と内周からなる多角形なので、coords(座標)だけだと、どっち?という話になります。
なので、外周は、exterior.coordsで。
list(polygon1.exterior.coords)
なお、外周はexteriorで単数ですが、内周はinteriorsで複数です。
help(Polygon)の出力のコピペが役に立ちましたね。
なお、単純に座標をみたければ、printしてしまえばよいです。
またしても中途半端ですが、いったんここまでとします。