前回の続きです。
Problem 4: Creating LineStrings that represent the movements (5 points):
This problem continues where we left off after completing Problem 3.
The task is to:
- create a list lines (shapely.geometry.LineString) between each pair of origin and destination points, and
- calculate the over-all total_length of all those lines.
Store the list of lines in a variable called lines, and the sum of lengths in a variable called total_length.
Once you have working solutions for both tasks,
3. create functions for them so you can apply them to other similar data sets in the future (see instructions below).
Problems 3が終わっていることが前提でです。
Problems 3で作成したorigin_points
・destination_points
から、LineString
のリストを作って、各LineString
の長さの合計をtotal_length
に格納します。
今回は上記の処理を関数にすることで、再利用可能にします。
(1)・(2)・(3)まとめてやってしまいます。
create_od_lines
関数はLineString
のリストを作ります。
calculate_total_distance
関数はLineString
のリストからLineString
の長さの合計を出します。
from shapely.geometry import Point, LineString
def create_od_lines(origin_points: list[Point], destination_points: list[Point]) -> LineString:
return [LineString([origin, destination]) for origin, destination in zip(origin_points, destination_points)]
def calculate_total_distance(lines: list[LineString]) -> float:
return sum([line.length for line in lines])
今回は、リストの内包表記を使いました。
リスト内包表記はリストを生成する簡潔な手段を提供しています。主な利用場面は、あるシーケンスや iterable (イテレート可能オブジェクト) のそれぞれの要素に対してある操作を行った結果を要素にしたリストを作ったり、ある条件を満たす要素だけからなる部分シーケンスを作成することです。
(愚痴)リストの内包表記は、ネストさせることもできます。が、個人としてはやらないでほしいです。for loop の順番なんぞ覚えたくないです。
Lesson 1振り返り
Overview
Before we start to work with entire data sets next week, we first have to become familiar with basic geometric objects (primitives): in the centre of this week’s lesson stand points, lines, and polygons.
We will learn how to create and manipulate ‘simple features’ in Python, using the Python package shapely.
pointとかpolygonが何なのかわかって、Pythonのshapelyパッケージ使って自分で作れる→できました
Learning goals:
- find Python packages for GIS and spatial analysis,
- understand and describe how geometric objects (points, lines, polygons) are handled and stored by shapely, and
- create geometries given their coordinate values.
- GISや空間分析のPythonライブラリを見つける→できました
- shapelyパッケージ使ってgeometricなオブジェクトを扱う方法が説明できる→できました
- 座標からgeometryが作成できる→できました
次は Lession 2です。いわゆる「ここからが本番」です。