Problem 1: Geocode shopping centers (7 points)
The overall aim of problems 1-3 is to find out how many people live within a walking distance (1.5 km) from certain shopping centres in Helsinki.
Problem 1 concerns the locations of shopping centres: find their addresses and translate them into coordinates.
ショッピングセンターをジオコーディングします。
どのくらいの人数がヘルシンキのショッピングセンターの徒歩圏内(1.5km)に住んでいるのかを検出します。
Problem 1では、ショッピングセンターの場所が必要です。住所を取得し、それを座標に変換する必要があります。
a) Prepare an input file containing the addresses of shopping centres (2 point)
Find out the addresses of the following shopping centres (e.g., by using your favourite search engine), and collect them in a text file called shopping_centres.txt:
以下のショッピングセンターの住所をネット検索とかで探し出し、shopping_centres.txtファイルにテキストとして格納しましょう。
- Itis
- Forum
- Iso-omena
- Sello
- Jumbo
- REDI
- Tripla
The text file should be in semicolon-separated format (;) and include the following columns:
- id (integer) a unique identifier for each shopping centre (a
- name (string) of each shopping center
- addr (string) the address
テキストファイルは、セミコロン(;) 区切りで、以下の列とします。
- id(数値型): いわゆるID
- name(文字列型): ショッピングセンターの名前
- addr (文字列型) :ショッピングセンターの住所
See an example of how to format the text file in the lesson 3 materials. Remember to add, commit, and push the file to your git repository.
詳細は、以下を参照。
ということなので、shopping_centres.txtを用意しました。面倒ですね。
1;Itis;Itäkatu 1-7, 00930 Helsinki
2;Forum;Mannerheimintie 20, 00100 Helsinki
3;Iso-omena;Piispansilta 11, 02230 Espoo
4;Sello;Leppävaarankatu 3-9, 02600 Espoo
5;Jumbo;Vantaanportinkatu 3, 01510 Vantaa
6;REDI;Hermannin rantatie 5, 00580 Helsinki
7;Tripla;Fredikanterassi 1, 00520 Helsinki
b) Read the list of addresses (1 point)
Read the list of addresses you just prepared into a pandas.DataFrame called shopping_centres
pandas.DataFrame
型のshopping_centres
変数にshopping_centres.txtの中身を読み込みます。
まずは事前準備。
import pandas as pd
import geopandas as gpd
import pathlib
NOTEBOOK_PATH = pathlib.Path().resolve()
DATA_DIRECTORY = NOTEBOOK_PATH / "data"
shopping_centres.txtファイルも格納しました。
! cat ./data/shopping_centres.txt
では、pd.read_csv()
関数で読み込みます。
なお、引数names
で列名を指定します。shopping_centres.txtに列名の行がないためです。
shopping_centres = pd.read_csv(DATA_DIRECTORY / "shopping_centres.txt", sep = ";", header=None, names=["id", "name", "addr"])
shopping_centres.head()
結果確認です。期待通りAssertionエラー無しです。
assert isinstance(shopping_centres, pandas.DataFrame)
for column in ("id", "name", "addr"):
assert column in shopping_centres.columns
c) Geocode the addresses (3 points)
Geocode the addresses using the Nominatim geocoding service. Join the results with the input data, and store them in a geopandas.GeoDataFrame with the same name (shopping_centres).
Remember to define a custom user_agent string!
Nominatim geocoding serviceで住所情報をジオコーディングします。そして、ジオコーディングの元たデータと結合し、geopandas.GeoDataFrame
型の変数shopping_centres
に格納します。
カスタムのユーザーエージェントを使うことを忘れずに!
まずはジオコーディングし、その結果をgeocoded_addresses
に格納します。
geocoded_addresses = gpd.tools.geocode(
shopping_centres["addr"],
provider="nominatim",
user_agent="autogis2024",
timeout=10
)
geocoded_addresses.head()
戻り値geocoded_addresses
はgeopandas.GeoDataFrame
型になります。
geopandas.GeoDataFrame
型のgeocoded_addresses
とpandas.DataFrame
型のshopping_centres
を結合し、その結果を、geopandas.GeoDataFrame
型のshopping_centres
に格納します。
geocoded_addresses.join()
とすることで、geocoded_addresses
に対して、shopping_centres
をインデックスをベースに結合させることできます。
また、結合後に、列を必要なものだけにしています。(これはお好みで)
shopping_centres = geocoded_addresses.join(shopping_centres)[["id", "name", "addr", "geometry"]]
shopping_centres.head()
(余談)
変数shopping_centres
を違う型で再使用しています。あまり良くないやり方ですね。
結果確認です。期待通りAssertionエラー無しです。
assert isinstance(shopping_centres, gpd.GeoDataFrame)
for column in ("id", "name", "addr", "geometry"):
assert column in shopping_centres.columns
Check that the coordinate reference system of the geocoded result is correctly defined, and reproject the layer into ETRS GK-25 (EPSG:3879):
to_crs()
メソッドで、CRSをETRS GK-25 (EPSG:3879)に再投影します。
その際、inplace=True
とすることで、shopping_centres
変数を上書きします。
shopping_centres.to_crs(epsg=3879,inplace=True)
shopping_centres.head()
再投影により、geometry
列の値が変わっていることが確認できます。
(例)1行目:POINT (25.08282 60.21165)
から POINT (25504591.772 6677656.345)
結果確認です。期待通りAssertionエラー無しです。
import pyproj
assert shopping_centres.crs == pyproj.CRS("EPSG:3879")
d) Save the resulting vector data set (1 point)
Save shopping_centres as a GeoPackage named shopping_centres.gpkg:
shopping_centres
を、shopping_centres.gpkgファイルとして(GeoPackageとして)保存します。
shopping_centres.to_file(DATA_DIRECTORY/"shopping_centres.gpkg")
念のため、保存されていることを確認します。
おまけとして、保存したshopping_centres.gpkgファイルを読みだして、プロットしています。ショッピングセンターの違いが分かるように、plot()
メソッドの引数に、column="name"
とlegend=True
を追加しています。
gpd.read_file(DATA_DIRECTORY/"shopping_centres.gpkg").plot(
column="name",
legend=True
)