16.1. Lesson: PostGISの設定

PostGISの関数を設定することで, PostgreSQLの中から空間関数にアクセス可能になります.

このレッスンの目的: 空間関数をインストールし, それらの効果を簡単にデモする.

注釈

We will assume the use of PostGIS version 2.1 or newer in this exercise. The installation and database configuration are different for older versions, but the rest of this material in this module will still work. Consult the documentation for your platform for help with installation and database configuration.

16.1.1. Ubuntuでのインストール

PostGISはaptから簡単にインストールできます.

$ sudo apt install postgresql
$ sudo apt install postgis

本当に簡単です...

注釈

The exact versions that will be installed depend on which version of Ubuntu you are using and which repositories you have configured. After installing you can check the version by issuing a select PostGIS_full_version(); query with psql or another tool.

To install a specific version (eg, PostgreSQL version 13 and PostGIS 3), you can use the following commands.

$ sudo apt install wget ca-certificates
$ sudo lsb_release -a
$ wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
$ sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list'
$ sudo apt-get update
$ sudo apt install postgis postgresql-13-postgis-3

16.1.2. Windowsでのインストール

Installing on Windows can be done from binary packages using a normal Windows installation dialogs.

First Visit the download page. Then follow this guide.

More information about installing on Windows can be found on the PostGIS website.

16.1.3. その他のプラットフォームへのインストール

The PostGIS website download has information about installing on other platforms including macOS and on other Linux distributions

16.1.4. PostGISを使用するためのデータベースの設定

一度PostGISがインストールされたら、拡張機能を使用するようにデータベースを設定する必要があります。PostGISのバージョン> 2.0をインストールしている場合、これが前の演習からアドレスデータベースを使用してpsqlで次のコマンドを発行するのと同じくらい簡単です。

$ psql -d address -c "CREATE EXTENSION postgis;"

注釈

Depending on your version, you could find more instructions on how to spatially enable a database at https://postgis.net/docs/postgis_administration.html#create_spatial_db.

16.1.5. インストールされたPostGIS関数を見る

PostGISは、空間データを扱えるようにPostgreSQLのコア機能を拡張する、データベース機能の集まりと考えることができます。「扱える」とは、格納、検索、クエリおよび操作を意味します。これを行うために、多数の機能が、データベースにインストールされています。

私たちのPostgreSQLの address住所 データベースはPostGISのおかげで地理空間的に有効になりました。次のセクションではこれについてもっと深く掘り下げていきますが、ここでちょっと味見しましょう。テキストからポイントを作成したいとしましょう。最初に、psqlコマンドを使ってポイントに関連する関数を見つけます。address住所 データベースにまだ接続していない場合は、今すぐ実行してください。次に、

\df *point*

This is the command we're looking for: st_pointfromtext. To page through the list, use the down arrow, then press Q to quit back to the psql shell.

次のコマンドを実行してみてください:

select st_pointfromtext('POINT(1 1)');

結果は:

st_pointfromtext
--------------------------------------------
0101000000000000000000F03F000000000000F03F
(1 row)

注目すべき3点:

  • POINT(1 1), を使って、ポイントの位置を 1,1 (EPSG:4326 を想定)と定義しました。

  • SQL文を実行しましたが、どのテーブル上でもなく、SQLプロンプトから入力されたデータで,

  • 結果の行はあまり意味がありません。

得られた行は、「よく知られているバイナリ」(WKB)と呼ばれるOGCフォーマットです。私たちは、次のセクションで詳細にこのフォーマットを見ていきます。

結果をテキストとして戻すために、テキストを返す何かを探して関数リストをすばやくスキャンできます:

\df *text

私たちが探しているクエリは st_astext です。以前のクエリと組み合わせてみましょう:

select st_astext(st_pointfromtext('POINT(1 1)'));

結果は:

 st_astext
------------
  POINT(1 1)
  (1 row)

ここでは、文字列 POINT(1,1) を入力し、 st_pointfromtext() を使用してポイントに変え、 st_astext() で人間が読める形式に戻します。これは元の文字列を返しました。

実際にPostGISの使い方の詳細に入る前の最後の例は:

select st_astext(st_buffer(st_pointfromtext('POINT(1 1)'),1.0));

それは何をしましたか?それは私たちのポイントを中心に1度のバッファを作成し、テキストとして結果を返しました。

16.1.6. 空間参照系

PostGISの機能だけでなく、この拡張は欧州石油調査グループ(EPSG)によって定義された空間参照システム(SRS)の定義のコレクションを含んでいます。これらは、座標参照系(CRS)変換などの操作中に使用されます。

通常のデータベーステーブルに格納されているので、データベース中のこれらのSRSの定義を調べることができます。

まず、psqlプロンプトで次のコマンドを入力して、テーブルのスキーマを見てみましょう。

\d spatial_ref_sys

結果はこうなります:

Table "public.spatial_ref_sys"
   Column   |          Type           | Modifiers
 -----------+-------------------------+-----------
  srid      | integer                 | not null
  auth_name | character varying(256)  |
  auth_srid | integer                 |
  srtext    | character varying(2048) |
  proj4text | character varying(2048) |
  Indexes:
"spatial_ref_sys_pkey" PRIMARY KEY, btree (srid)

(入門セクションから学んだように)このテーブルを表示および操作するために、標準のSQLクエリを使用できます - 何をしているか知らないならば、任意のレコードを更新または削除するのは良いアイデアではない。

興味のあるSRIDは、EPSG:4326 - WGS 84楕円体を使用した地理/緯度経度参照系です。それを見てみましょう:

select * from spatial_ref_sys where srid=4326;

結果は:

srid      | 4326
auth_name | EPSG
auth_srid | 4326
srtext    | GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS
84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],TOWGS84[0,
0,0,0,0,0,0],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,
AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,
AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]
proj4text | +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs

srtext はよく知られているテキスト(WKT)での投影の定義です(シェープファイルのコレクションにある.prjファイルからこれを認識してもよい)。

16.1.7. In Conclusion

今PostGISの機能はPostgreSQLのコピーにインストールされています。これによりPostGISのさまざまな空間関数を使用できるでしょう。

16.1.8. What's Next?

次はデータベースにおける空間地物の表現方法について学習しましょう。