PostGISを使ってみる

移転しました。

仕事の関係でPostGISを使うことになったので、インストールや使い方などをメモっておく。

PostGIS参考URL
http://kokogiko.net/m/archives/001631.html
http://www.ne.jp/asahi/free/hiroro/postgis/manual/ 日本語マニュアル。古そう。。
http://postgis.refractions.net/ 本家。
http://bubble.atnifty.com/modules/bwiki/index.php?BuildGISServ インストール方法あり!

Debian インストール

インストール

Debian4.0上に構築する。

インストールは以下を見て無事終了。ちなみにデスクトップ環境は選択しなかった。

http://www.debian.or.jp/using/quick-etch/

ネットワーク設定
# vi /etc/network/interfaces

-----
iface lo inet loopback
auto lo

iface eth0 inet static
address 192.168.0.3
network 192.168.0.0
netmask 255.255.255.0
broadcast 192.168.0.255
gateway 192.168.0.1
auto eth0
-----

# /etc/init.d/networking restart
各種ソフトインストール

まずはデフォルトだとapt-getをした際に「CD-ROMを入れろ」というメッセージ

Media Change: Please insert the disc labeled 'Debian …' in the drive '/cdrom/' and press enter

を表示してしまうため、サーバー上を参照するように以下を変更

# cp /etc/apt/sources.list /etc/apt/sources.list.orig
# vi /etc/apt/sources.list

-----
↓コメント化する
#deb cdrom:[Debian GNU/Linux 4.0 r1 _Etch_ - Official i386 NETINST Binary-1 20070820-20:21]/ etch contrib main
-----

インストールする

# apt-get update
# apt-get install ssh gcc make g++ bzip2 gawk

Postgresインストール

グループ&ユーザー作成
# addgroup postgres
# mkdir /home/postgres
# adduser --home /home/postgres --ingroup postgres postgres

Warning: The home dir you specified already exists.
Adding user `postgres' ...
Adding new user `postgres' (1001) with group `postgres' ...
The home directory `/home/postgres' already exists.  Not copying from `/etc/skel'.
adduser: Warning: that home directory does not belong to the user you are currently creating.
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for postgres
Enter the new value, or press ENTER for the default
        Full Name []:
        Room Number []:
        Work Phone []:
        Home Phone []:
        Other []:
Is the information correct? [y/N] y

# cd 
# cp -ap .bash* /home/postgres
# cp -ap .profile /home/postgres
# cd /home
# chown -R postgres.postgres postgres
インストール

事前にreadlineとzlibなどをインストールしておく。
これをインストールしておかないと、configure時に怒られる。
またbisonやflexをもWARNINGが発生するため入れておく。

こんなWARNING。

configure: WARNING:
*** Without Flex you will not be able to build PostgreSQL from CVS or
*** change any of the scanner definition files.  You can obtain Flex from
*** a GNU mirror site.  (If you are using the official distribution of
*** PostgreSQL then you do not need to worry about this because the Flex
*** output is pre-generated.)
# mkdir /usr/local/pgsql825
# apt-get install zlib-bin zlib1g zlib1g-dev
# apt-get install libreadline5-dev bison flex
# su - postgres
$ mkdir src
$ cd src

src に postgres8.2.5をあげる

$ tar zxvf postgresql-8.2.5.tar.gz
$ cd postgresql-8.2.5
$LDFLAGS="-s -lstdc++" \
>./configure \
>--prefix=/usr/local/pgsql825
$ make
$ su 
# make install
# cd /usr/local

シンボリックリンク作成。バージョンごとの対応。

# ln -s pgsql825 pgsql
# su - postgres
$ vi .bashrc

-----
以下を最後の行に追加
export PATH=$PATH:/usr/local/pgsql/bin
export POSTGRES_HOME=/usr/local/pgsql
export PGLIB=$POSTGRES_HOME/lib
export PGDATA=/home/postgres/data
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$PGLIB
-----

$ exit
DB初期化
$ initdb --encoding=UNICODE --no-locale
設定ファイル編集
$ cd ~/postgres/data
$ cp -ap postgresql.conf postgresql.conf.orig
$ vi postgresql.conf

-----
全ての外部ホストからアクセスを許可する。
listen_addresses = '*'
-----

$ vi pg_hba.conf

-----
アクセスするPCのIPアドレスを許可する
host    all         all         192.168.1.120/32      trust
-----
起動&確認
$ pg_ctl -D data -l postmaster.log start
$ psql -l 
        List of databases
   Name    |  Owner   | Encoding
-----------+----------+----------
 postgres  | postgres | UTF8
 template0 | postgres | UTF8
 template1 | postgres | UTF8
(3 rows)

PostGISインストール

必要があるソフトをインストールする

# apt-get install proj // Proj.4 再投影や座標交換に用いる

geosはapt-getからみつからなかったのでソースから  // GEOS ジオメトリ型の操作や解析機能
# su - postgres
$ cd src
$ wget http://geos.refractions.net/geos-3.0.0rc4.tar.bz2
$ tar xjvf geos-3.0.0rc4.tar.bz2
$ cd geos-3.0.0rc4
$ LDFLAGS="-s" // LDFLAGSは必要??
>./configure
$ make
$ su
# make install

共有ライブラリ設定
# vi /etc/ld.so.conf
-----
include /etc/ld.so.conf.d/*.conf
/usr/local/lib
/usr/local/pgsql/lib
-----
# ldconfig

実際にpostGISをインストールする

# su - postgres
$ cd src
$ wget http://postgis.refractions.net/download/postgis-1.3.1.tar.gz
$ cp -ap postgis-1.3.1.tar.gz postgresql-8.2.5/contrib/
$ cd postgresql-8.2.5/contrib/
$ tar zxvf postgis-1.3.1.tar.gz
$ ./configure
$ make 
$ su - 
# cd /home/postgres/src/postgresql-8.2.5/contrib/postgis-1.3.1
# make install 

DBを作成してみて、動作確認を行う。

# su - postgres
$ pg_ctl -D data -l postmaster.log start
$ createdb testgis --encoding=UTF8
$ createlang plpgsql testgis
$ psql -d testgis -f /usr/local/pgsql/share/lwpostgis.sql
$ psql -d testgis -f /usr/local/pgsql/share/spatial_ref_sys.sql