GIG

赴くままに技術を。

PostGISをMac OS X 10.7.5にインストールする

地理空間検索機能を仕事で利用するため、調査がてらインストールした。

インストールスクリプト

インストールするにあたり、シェルスクリプトを書いてみた。 (本当はChefで書いて、serverspecでテストしたいが...)

postgis.sh

#! /bin/bash
#################################################
# describe : set up postgis environment.
#
# Program    version notes
# -----------------------------------------------
# PostgresSQL 9.2.4  http://www.postgresql.org/ftp/source/
# GDAL        1.9.2  http://download.osgeo.org/gdal/
# GEOS        3.3.8  http://download.osgeo.org/geos/
# Proj4       4.9.0b1 http://download.osgeo.org./proj/
# JSON-C      master https://github.com/json-c/json-c 
# PostGIS     2.0.3  http://postgis.net/source
# -----------------------------------------------
# author   : yuri.woof.ohno@gmail.com
# modified : 2013/08/10(Sat)
#################################################

usage() {
    echo 'Usage: '$0' [--help|-h] [--postgre] [--gdal] [--geos] [--proj] [--jsonc] [--postgis]'
    exit 1;
}

message() {

    printf "\E[0;33;40m"
    echo $1
    printf "\E[0m"

}

# TODO : add checking the excuter.
setup() {

     INSTALL_DIR="/opt/postgis"
     POSTGRE_VER="9.2.4"
     POSTGRE_PORT="5432"
     GDAL_VER="1.9.2"
     GEOS_VER="3.3.7"
     PROJ_VER="4.9.0b1"
     PROJ_VER_DIR="4.9.0"
     JSONC_VER="0.11"
     POSTGIS_VER="2.0.3"

     # create & change dir
     if [ ! -d $INSTALL_DIR ]; then
     mkdir $INSTALL_DIR
     fi
     cd $INSTALL_DIR

 }

 install_postgre() {

     # send message
     message "[START] Install PostgreSQL."

     # get source file
     wget http://ftp.postgresql.org/pub/source/v$POSTGRE_VER/postgresql-$POSTGRE_VER.tar.bz2
     tar -jxf postgresql-$POSTGRE_VER.tar.bz2

     # install postgresql
    cd postgresql-$POSTGRE_VER && ./configure \
    --prefix=$INSTALL_DIR/psgre$POSTGRE_VER \
    --with-pgport=$POSTGRE_PORT \
    --with-perl \
    --with-python
    make && make install

    # create symbolic link
    cd $INSTALL_DIR
    ln -s psgre$POSTGRE_VER psgre

    # remove work file
    rm -rf postgresql-$POSTGRE_VER*

    # send message
    message "[END] Install PostgreSQL."

 }

 install_gdal() {

      # send message
     message "[START] Install GDAL."

      # get source file
      wget http://download.osgeo.org/gdal/gdal-$GDAL_VER.tar.gz
      tar -xzf gdal-$GDAL_VER.tar.gz

      # install gdal
      cd gdal-$GDAL_VER && ./configure \
      --prefix=$INSTALL_DIR/gdal$GDAL_VER
      make && make install

      # create symbolic link
      cd $INSTALL_DIR
      ln -s gdal$GDAL_VER gdal

      # remove work file
      rm -rf gdal-$GDAL_VER*

     # send message
     message "[END] Install GDAL."

}

 install_geos() {

     # send message
     message "[START] Install GEOS."

     # get source file
     wget http://download.osgeo.org/geos/geos-$GEOS_VER.tar.bz2
     tar -xjf geos-$GEOS_VER.tar.bz2

     # install geos
     cd geos-$GEOS_VER && ./configure \
     --prefix=$INSTALL_DIR/geos$GEOS_VER \
     CC=/usr/bin/clang \
     CXX=/usr/bin/clang++
     make && make install

     # create symbolic link
     cd $INSTALL_DIR
     ln -s geos$GEOS_VER geos

     # remove work file
     rm -rf geos-$GEOS_VER*

    # send message
    message "[END] Install GEOS."

}

install_proj() {

    # send message
    message "[START] Install Proj."

    # get source file
    wget http://download.osgeo.org/proj/proj-$PROJ_VER.tar.gz
    tar -xf proj-$PROJ_VER.tar.gz

    # install proj
    cd proj-$PROJ_VER_DIR && ./configure \
    --prefix=$INSTALL_DIR/proj$PROJ_VER_DIR
    make && make install

    # create symbolic link
    cd $INSTALL_DIR
    ln -s proj$PROJ_VER_DIR proj

    # remove work file
   rm -rf proj-$PROJ_VER* proj-$PROJ_VER_DIR*

    # send message
   message "[END] Install Proj."

}

# TODO : check the way of installing this package besides GitHub.
install_json() {

    # send message
   message "[START] Install json-c."

    # get source file
    # git clone https://github.com/json-c/json-c.git
    wget https://s3.amazonaws.com/json-c_releases/releases/json-c-$JSONC_VER.tar.gz
    tar -xf json-c-$JSONC_VER.tar.gz

    # install json-c
    cd json-c-$JSONC_VER && ./configure --prefix=$INSTALL_DIR/json-c$JSONC_VER
    make && make check && make install

    # create symbolic link
    cd $INSTALL_DIR
    ln -s json-c$JSONC_VER json-c

    # remove work file
    rm -rf json-c-$JSONC_VER*

    # send meaage
   message "[END] Install json-c."

 }

install_postgis() {

    # send message
    message "[START] Install PostGIS."

    # get source file
    wget http://download.osgeo.org/postgis/source/postgis-$POSTGIS_VER.tar.gz
    tar -xzf postgis-$POSTGIS_VER.tar.gz

    # install postgis
    cd postgis-$POSTGIS_VER
    ./configure \
# --prefix=$INSTALL_DIR/postgis$POSTGIS_VER \
--with-pgconfig=$INSTALL_DIR/psgre/bin/pg_config \
--with-gdalconfig=$INSTALL_DIR/gdal/bin/gdal-config \
--with-geosconfig=$INSTALL_DIR/geos/bin/geos-config \
--with-projdir=$INSTALL_DIR/proj \
--with-jsondir=$INSTALL_DIR/json-c \
--with-raster \
--with-topology
    make && make install

    # remove work file
    rm -rf postgis-$POSTGRE_VER*

    # send message
    message "[END] Install PostGis."

 }

 # setup
 setup

 # fire each function
 case $1 in
    --postgre)
    install_postgre
    ;;
    --gdal)
    install_gdal
    ;;
    --geos)
    install_geos
    ;;
    --proj)
    install_proj
    ;;
    --jsonc)
    install_json
    ;;
    --postgis)
    install_postgis
    ;;
    *)
    usage $0
    ;;
esac

要改善点

  • PostGISのインストール時にPostgreSQLとGEOSのファイル(*config)がないと怒られる。

    checking for geos-config... no configure: error: could not find geos-config within the current path. You may need to try
    re-running configure with a --with-geosconfig parameter. ./postgis.sh: line 203: --with-pgconfig=/opt/postgis/psgre/bin/pg_config: No such file or
    directory

しかし、PostgreSQLもGEOSもインストール済みで、パスの間違いは見当たらなかった。 ちなみに上のスクリプトを利用せず、コンソールから以下のコマンド(上と同等)を入力すると、問題なくインストールできた。

# ./configure \
 --prefix=/opt/postgis/postgis2.0.3 \
 --with-pgconfig=/opt/postgis/psgre/bin/pg_config \
--with-gdalconfig=/opt/postgis/gdal/bin/gdal-config \
--with-geosconfig=/opt/postgis/geos/bin/geos-config \ 
--with-projdir=/opt/postgis/proj \
--with-jsondir=/opt/postgis/json-c \
--with-raster \
--with-topology

よくわからん。 OSが関係してるのかなー?

インストールの確認

流れとしては、PostgreSQLを起動し、PostGISに合わせたデータベースを作成する。

PostgreSQLを起動

データベース格納領域を初期化する。 この格納領域をデータベースクラスタと呼び、ここに複数のデータベースを作る。

$ mkdir /opt/postgis/data
$ /opt/postgis/psgre/bin/initdb -D /opt/postgis/data

PostgreSQLをバックグラウンドで起動する。-oでオプションを渡す(-iはリモートから利用するため)

$ mkdir /opt/postgis/logs
$ touch /opt/postgis/logs/logfile
 $ /opt/postgis/psgre/bin/pg_ctl -D /opt/postgis/data -o "-i" -l /opt/postgis/logs/postgresql.log start

空間データベースを作成

例として、testgisというデータベースを作る。

$ /opt/postgis/psgre/bin/createdb testgis -E UTF-8
$ /opt/postgis/psgre/bin/createlang plpgsql testgis
$ /opt/postgis/psgre/bin/psql testgis -f    /opt/postgis/psgre/share/postgresql/contrib/postgis-2.0/postgis.sql
$ /opt/postgis/psgre/bin/psql testgis -f /opt/postgis/psgre/share/postgresql/contrib/postgis-2.0/spatial_ref_sys.sql

PostgreSQL 9.1以降は、下記の方法で作成することが推奨されている。 (参考:http://www.finds.jp/docs/pgisman/2.0.0/postgis.html#templatepostgisの2.6参照)

$ /opt/postgis/psgre/bin/createdb testgis -E UTF-8
$ /opt/postgis/psgre/bin/psql testgis
testgis=# CREATE EXTENSION postgis;
CREATE EXTENSION

以下を確認する。

$ /opt/postgis/psgre/bin/psql testgis
psql (9.2.4)
Type "help" for help.

testgis=# select * from postgis_version();
        postgis_version            
---------------------------------------
2.0 USE_GEOS=1 USE_PROJ=1 USE_STATS=1        <=GEOS, PROJが利用されていることを確認
(1 row)

データベースから出る

 testgis=# \q