16.1. Lesson: PostGISの設定

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

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

ノート

ここではPostGISバージョン2.1を使用を前提としています. インストールとデータベースの設定方法は以前のバージョンと異なりますが, この資料の残りの部分はまだ有効です. インストールおよびデータベースの設定については, お使いのプラットフォームのドキュメントを参照してください.

16.1.1. Ubuntuでのインストール

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

$ sudo apt-get install postgis
$ sudo apt-get install postgresql-9.1-postgis

本当に簡単です...

ノート

使用しているUbuntuのバージョンや設定されているリポジトリによって, PostGIS 1.5もしくは2.xがインストールされます. psqlや他のツールで select PostGIS_full_version(); クエリを発行すれば、インストールされたバージョンを知ることができます.

PostGIS最新版をインストールするには, 以下のコマンドが使用できます.

$ sudo apt-add-repository ppa:sharpie/for-science
$ sudo apt-add-repository ppa:sharpie/postgis-nightly
$ sudo apt-get update
$ sudo apt-get install postgresql-9.1-postgis-nightly

16.1.2. Windowsでのインストール

Windowsへのインストールは少し複雑ですが、それでも大変ではありません. PostGISのスタックをインストールするには, オンラインである必要があることに注意してください.

まず このダウンロードページ を参照します.

そして このガイド に従ってください.

Windowsへのインストールに関する詳細な情報は PostGISのウェブサイト に掲載されています.

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

The PostGIS website download has information about installing on other platforms including MacOSX and on other linux distributions

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

Once PostGIS is installed, you will need to configure your database to use the extensions. If you have installed PostGIS version > 2.0, this is as simple as issuing the following command with psql using the address database from our previous exercise.

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

ノート

If you are using PostGIS 1.5 and a version of PostgreSQL lower than 9.1, you will need to follow a different set of steps in order to install the postgis extensions for your database. Please consult the PostGIS Documentation for instructions on how to do this. There are also some instructions in the previous version of this manual.

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

PostGIS can be thought of as a collection of in-database functions that extend the core capabilities of PostgreSQL so that it can deal with spatial data. By ‘deal with’, we mean store, retrieve, query and manipulate. In order to do this, a number of functions are installed into the database.

Our PostgreSQL address database is now geospatially enabled, thanks to PostGIS. We are going to delve a lot deeper into this in the coming sections, but let’s give you a quick little taster. Let’s say we want to create a point from text. First we use the psql command to find functions relating to point. If you are not already connected to the address database, do so now. Then run:

\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プロンプトから入力されたデータで,

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

The resulting row is in the OGC format called ‘Well Known Binary’ (WKB). We will look at this format in detail in the next section.

To get the results back as text, we can do a quick scan through the function list for something that returns text:

\df *text

The query we’re looking for now is st_astext. Let’s combine it with the previous query:

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

結果:

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

Here, we entered the string POINT(1,1), turned it into a point using st_pointfromtext(), and turned it back into a human-readable form with st_astext(), which gave us back our original string.

本当にPostGISの使用の詳細に入る前の最後の例

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

What did that do? It created a buffer of 1 degree around our point, and returned the result as text.

16.1.6. 空間参照系

In addition to the PostGIS functions, the extension contains a collection of spatial reference system (SRS) definitions as defined by the European Petroleum Survey Group (EPSG). These are used during operations such as coordinate reference system (CRS) conversions.

We can inspect these SRS definitions in our database as they are stored in normal database tables.

First, let’s look at the schema of the table by entering the following command in the psql prompt:

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

You can use standard SQL queries (as we have learned from our introductory sections), to view and manipulate this table - though its not a good idea to update or delete any records unless you know what you are doing.

One SRID you may be interested in is EPSG:4326 - the geographic / lat lon reference system using the WGS 84 ellipsoid. Let’s take a look at it:

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

The srtext is the projection definition in well known text (you may recognise this from .prj files in your shapefile collection).

16.1.7. In Conclusion

You now have PostGIS functions installed in your copy of PostgreSQL. With this you’ll be able to make use of PostGIS’ extensive spatial functions.

16.1.8. What’s Next?

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