2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Automating GIS Processes 2024 写経 Lesson 3(Exercise 3、Problem 1)

Posted at

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

image.png

では、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()

image.png

結果確認です。期待通りAssertionエラー無しです。

assert isinstance(shopping_centres, pandas.DataFrame)
for column in ("id", "name", "addr"):
    assert column in shopping_centres.columns

image.png

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()

image.png

戻り値geocoded_addressesgeopandas.GeoDataFrame型になります。

image.png

geopandas.GeoDataFrame型のgeocoded_addressespandas.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()

image.png

(余談)
変数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)に再投影します。

image.png

その際、inplace=Trueとすることで、shopping_centres変数を上書きします。

image.png

shopping_centres.to_crs(epsg=3879,inplace=True)
shopping_centres.head()

image.png

再投影により、geometry列の値が変わっていることが確認できます。
(例)1行目:POINT (25.08282 60.21165) から POINT (25504591.772 6677656.345)

結果確認です。期待通りAssertionエラー無しです。

import pyproj
assert shopping_centres.crs == pyproj.CRS("EPSG:3879")

image.png

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")

念のため、保存されていることを確認します。

image.png

おまけとして、保存したshopping_centres.gpkgファイルを読みだして、プロットしています。ショッピングセンターの違いが分かるように、plot()メソッドの引数に、column="name"legend=Trueを追加しています。

gpd.read_file(DATA_DIRECTORY/"shopping_centres.gpkg").plot(
    column="name",
    legend=True
)

image.png

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?