前回の続きです。
Download and visualise OpenStreetMap data with OSMnx
A useful feature of OSMnx is its easy-to-use tools to download OpenStreetMap data via the project’s OverPass API. In this section, we will learn how to download and visualise the street network and additional data from OpenStreetMap covering an area of interest.
OSMXの有用な特徴は、Overpass APIを通して、オープンストリートマップのデータをダウンロードする、という点において、使いやすいツールであることです。このセクションでは、ストリートネットワークと関心のあるエリアをカバーする追加データをOpenStreetMapからダウンロードし、可視化する方法を学びます。
Street network
The
osmnx.graph
module https://osmnx.readthedocs.io/en/stable/osmnx.html#module-osmnx.graph__ downloads data to construct a routable road network graph, based on an user-defined area of interest. This area of interest can be specified, for instance, using a place name, a bounding box, or a polygon. Here, we will use a placename for fetching data covering the Kamppi area in Helsinki, Finland.In the place name query, OSMnx uses the Nominatim Geocoding API. This means that place names should exist in the OpenStreetMap database (run a test search at openstreetmap.org or nominatim.openstreetmap.org).
osmnx.graph
モジュール は、ルーティングができる道路ネットワークグラフを作るためのデータをダウンロードします。例えば、地名、バウンディングボックス、ポリゴンを使うことで、関心のあるエリアを特定できます。ここでは、ヘルシンキのカンッピ(Kamppi)エリアのデータを取得するために、地名を使います。
地名のクエリは、OSMnxはNominatim GeocodingのAPIを使います。これは、その地名がオープンストリートマップのデータベースに存在すべきである、ということを意味します。(試しに openstreetmap.org か nominatim.openstreetmap.org で地名検索をしてみてください。)
We will read an OSM street network using OSMnx’s graph_from_place() function:
OSMnxのgraph_from_place()関数をついあって、オープンストリートマップのストリートネットワークを読み込みます。
import osmnx
PLACE_NAME = "Kamppi, Helsinki, Finland"
graph = osmnx.graph_from_place(PLACE_NAME)
エラーになりました。
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
/tmp/ipython-input-1914007169.py in <cell line: 0>()
----> 1 import osmnx
2
3 PLACE_NAME = "Kamppi, Helsinki, Finland"
4 graph = osmnx.graph_from_place(PLACE_NAME)
ModuleNotFoundError: No module named 'osmnx'
---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.
To view examples of installing some common dependencies, click the
"Open Examples" button below.
---------------------------------------------------------------------------
ModuleNotFoundErrorなので、いつものパターンです。pip install
でosmnxをインストールします。
そして、再実行。エラーは無くなりました。
Check the data type of the graph:
データの型を確認してください。
type(graph)
What we have here is a
networkx.MultiDiGraph <https://networkx.org/documentation/stable/reference/classes/multidigraph.html>
__ object.OSMnx’s graphs do not have a built-in method to plot them, but the package comes with a function to do so:
データの型は、networkx.MultiDiGraph
です。
OSMnxのグラフ自身は、プロット用の組み込み関数がありません。しかし、パッケージの関数に関数があります:
figure, ax = osmnx.plot_graph(graph)
Just as its GeoPandas and Pandas equivalents, osmnx.plot_graph() uses matplotlib. The function returns a (figure, axes) tuple, that can be used to modify the figure using all matplotlib functions we already got to know.
We can see that our graph contains nodes (the points) and edges (the lines) that connects those nodes to each other.
GeoPandas
やPandas
と同じように、osmnx.plot_graph()
関数はmatplotlibを使います。この関数は(figure, axes)
というタプルを返します。このタプルは、私たちがすでに見知ったすべてのmatplotlibの関数を使って、figure
を修正するために使われます。
グラフがノード(点)とノード同士を結合する縁(線分)を含んでいることがわかります。
Convert a graph to GeoDataFrames
The street network we just downloaded is a graph, more specifically a
networkx.MultiDiGraph
. Its main purpose is to represent the topological relationships between nodes and the links (edges) between them. Sometimes, it is more convenient to have the underlying geodata in geopandas.GeoDataFrames. OSMnx comes with a convenient function that converts a graph into two geo-data frames, one for nodes, and one for edges:osmnx.graph_to_gdfs() <https://osmnx.readthedocs.io/en/stable/osmnx.html#osmnx.utils_graph.graph_to_gdfs>
__.
ダウンロードしたストリートネットワークはグラフであり、一つのnetworkx.MultiDiGraph
です。
ノードとそのリンク(エッジ)の間のトポロジーの関連性を示します。土台となるgeopandas.GeoDataFrame
のジオデータがあると、都合がよいときもあります。
OSMnxには、グラフを2つのジオデータフレーム(1つはノード、もう一つはエッジ)に変換する便利な関数:osmnx.graph_to_gdfs()
があります。
nodes, edges = osmnx.graph_to_gdfs(graph)
まずはノードから。
nodes.head()
よくわからないですね。
次はエッジです。
edges.head()
こっちはなんとなくわかりますね。
Nice! Now, as we can see, we have our graph as GeoDataFrames and we can plot them using the same functions and tools as we have used before.
いいですね!見ての通り、1つのグラフを複数のGeoDataFrameとして保持して、すでに使ったことがある関数やツールを使って、プロットすることができます。
Place polygon
Let’s also plot the polygon that represents our area of interest (Kamppi, Helsinki). We can retrieve the polygon geometry using the [osmnx.geocode_to_gdf()](https://osmnx.readthedocs.io/en/stable/osmnx.html?highlight=geocode_to_gdf(#osmnx.geocoder.geocode_to_gdf) function.
興味があるエリア(ヘルシンキのカンッピ(Kamppi)エリア)を表示するポリゴンをプロットしてみましょう。osmnx.geocode_to_gdf()
関数を使って、ポリゴンを取得できます。
# Get place boundary related to the place name as a geodataframe
area = osmnx.geocode_to_gdf(PLACE_NAME)
As the name of the function already tells us, it returns a GeoDataFrame object based on the specified place name query. Let’s still verify the data type:
関数名からわかるように、この関数は、特定の地名のクエリに基づくGeoDataFrameのオブジェクトを返します。
データの型を確認しましょう:
# Check the data type
type(area)
Let’s also have a look at the data:
データを見てみましょう。
# Check data values
area
area.plot()
グーグルマップで、Kamppiで地図を検索すると以下のようになります。
ポリゴンの形と大体同じですね。
Building footprints
Besides network data, OSMnx can also download any other data contained in the OpenStreetMap database. This includes, for instance, building footprints, and different points-of-interests (POIs). To download arbitrary geometries, filtered by OSM tags and a place name, use
osmnx.features_from_place() <https://osmnx.readthedocs.io/en/stable/osmnx.html#osmnx.features_from_place>
__ [geometries is now deprecated]. The tag to retrieve all buildings is building = True.
ネットワークのデータに加えて、OSMnxはオープンストリートマップのデータベースに含まれる他のデータもダウンロードできます。例えば、道路の足跡や異なる興味のある地点(POIs)です。
任意のジオメトリをダウンロードするために、osmnx.features_from_place()
を使って
、OSMタグや地名でフィルターします。
buildings = osmnx.features_from_place(
PLACE_NAME,
{"building": True},
)
len(buildings)
buildings.head()
以下はグーグルマップ(衛星画像)です。上記のプロットが建物の輪郭に沿っているように見えます。
buildings.columns
As you can see, there are several columns in buildings. Each column contains information about a specific tag that OpenStreetMap contributors have added. Each tag consists of a key (the column name), and a values (for example building=yes or building=school). Read more about tags and tagging practices in the OpenStreetMap wiki.
見ての通り、buildings
にはいくつかの列があります。各々の列はオープンストリートマップの貢献者たちが追加した特定のタグについての情報です。各々のタグはキー(列名)と値(例えば、building=yes
、building=school
)を持っています。タグとタグ付けの理論や知識については、OpenStreetMap wikiを参照ください。
長くなったので、ここまでにします。