PostGIS とは?

Install (Mac OS X 10.8)

  1. MacPorts? で、インストール
    $ sudo port install postgis2 pgadmin3
    インストール中に出てくるメッセージを覚えておく
    ...
    --->  Installing postgresql92-server @9.2.4_0
    --->  Activating postgresql92-server @9.2.4_0
    
    To create a database instance, after install do
     sudo mkdir -p /opt/local/var/db/postgresql92/defaultdb
     sudo chown postgres:postgres /opt/local/var/db/postgresql92/defaultdb
     sudo su postgres -c '/opt/local/lib/postgresql92/bin/initdb -D /opt/local/var/db/postgresql92/defaultdb'
    
    To tweak your DBMS, consider increasing kern.sysv.shmmax by adding an increased
    kern.sysv.shmmax .. to /etc/sysctl.conf
    
    --->  Cleaning postgresql92-server
    ...
  2. データベースを初期化
    [~]$ sudo mkdir -p /opt/local/var/db/postgresql92/defaultdb
    [~]$ sudo chown postgres:postgres /opt/local/var/db/postgresql92/defaultdb
    [~]$ sudo su postgres -c '/opt/local/lib/postgresql92/bin/initdb -D /opt/local/var/db/postgresql92/defaultdb'
    The files belonging to this database system will be owned by user "postgres".
    This user must also own the server process.
    
    The database cluster will be initialized with locale "ja_JP.UTF-8".
    The default database encoding has accordingly been set to "UTF8".
    initdb: could not find suitable text search configuration for locale "ja_JP.UTF-8"
    The default text search configuration will be set to "simple".
    
    fixing permissions on existing directory /opt/local/var/db/postgresql92/defaultdb ... ok
    creating subdirectories ... ok
    selecting default max_connections ... 20
    selecting default shared_buffers ... 1600kB
    creating configuration files ... ok
    creating template1 database in /opt/local/var/db/postgresql92/defaultdb/base/1 ... ok
    initializing pg_authid ... ok
    initializing dependencies ... ok
    creating system views ... ok
    loading system objects' descriptions ... ok
    creating collations ... ok
    creating conversions ... ok
    creating dictionaries ... ok
    setting privileges on built-in objects ... ok
    creating information schema ... ok
    loading PL/pgSQL server-side language ... ok
    vacuuming database template1 ... ok
    copying template1 to template0 ... ok
    copying template1 to postgres ... ok
    
    WARNING: enabling "trust" authentication for local connections
    You can change this by editing pg_hba.conf or using the option -A, or
    --auth-local and --auth-host, the next time you run initdb.
    
    Success. You can now start the database server using:
    
        /opt/local/lib/postgresql92/bin/postgres -D /opt/local/var/db/postgresql92/defaultdb
    or
        /opt/local/lib/postgresql92/bin/pg_ctl -D /opt/local/var/db/postgresql92/defaultdb -l logfile start
  3. ログディレクトリ作成
    $ sudo mkdir /var/log/postgresql92
    $ sudo chown postgres:postgres /var/log/postgresql92/
  4. 起動・停止
    $ sudo su postgres -c '/opt/local/lib/postgresql92/bin/pg_ctl -D /opt/local/var/db/postgresql92/defaultdb -l /var/log/postgresql92/logfile start'
    server starting
     
    $ sudo su postgres -c '/opt/local/lib/postgresql92/bin/pg_ctl -D /opt/local/var/db/postgresql92/defaultdb stop'
    waiting for server to shut down.... done
    server stopped
    server starting
    postgres コマンドで起動した場合には、foreground 実行になる
  5. 自動起動
    $ sudo port load postgresql92-server
    $ sudo launchctl list | grep macports
    2279	-	org.macports.postgresql92-server
    -	0	org.macports.frameworks.macports
  6. 自動起動解除
    $ sudo port unload postgresql92-server
    $ sudo launchctl list | grep macports
    -	0	org.macports.frameworks.macports

Postgresql の設定 (Mac OS X 10.8)

  1. postgre ユーザにパスワードを設定
    $ psql92 -U postgres
    psql92 (9.2.4)
    Type "help" for help.
    
    postgres-# \du
                                 List of roles
     Role name |                   Attributes                   | Member of
    -----------+------------------------------------------------+-----------
     postgres  | Superuser, Create role, Create DB, Replication | {}
    
    postgres=# alter user postgres with password 'postgres';
    ALTER ROLE
    postgres=# \q
  2. 認証方法をパスワードに変更する
    $ sudo diff -u /opt/local/var/db/postgresql92/defaultdb/pg_hba.conf.original  /opt/local/var/db/postgresql92/defaultdb/pg_hba.conf
    --- /opt/local/var/db/postgresql92/defaultdb/pg_hba.conf.original	2013-09-14 21:53:49.000000000 +0900
    +++ /opt/local/var/db/postgresql92/defaultdb/pg_hba.conf	2013-09-14 21:54:25.000000000 +0900
    @@ -81,11 +81,11 @@
     # TYPE  DATABASE        USER            ADDRESS                 METHOD 
    
     # "local" is for Unix domain socket connections only
    -local   all             all                                     trust
    +local   all             all                                     password
     # IPv4 local connections:
    -host    all             all             127.0.0.1/32            trust
    +host    all             all             127.0.0.1/32            password
     # IPv6 local connections:
    -host    all             all             ::1/128                 trust
    +host    all             all             ::1/128                 password
     # Allow replication connections from localhost, by a user with the
     # replication privilege.
     #local   replication     postgres                                trust
  3. 再起動
    $ sudo su postgres -c '/opt/local/lib/postgresql92/bin/pg_ctl -D /opt/local/var/db/postgresql92/defaultdb -l /var/log/postgresql92/logfile restart'
    waiting for server to shut down.... done
    server stopped
    server starting
  4. パスワードを聞かれることを確認
    $ psql92 -U postgres
    Password for user postgres:
    psql92 (9.2.4)
    Type "help" for help.
    
    postgres=# \q

Install (Scientific Linux 6.4)

  1. PostgreSQLの レポジトリを yum に登録する
    # yum -y install http://yum.postgresql.org/9.2/redhat/rhel-6-x86_64/pgdg-sl92-9.2-8.noarch.rpm
    /etc/yum.repos.d/sl.repo で、SL Linux のレポジトリにある PostgreSQL を使わないように設定
    [sl]
    
     ...
    
    exclude=postgresql*
    
    [sl-security]
    
     ...
    
    exclude=postgresql*
  2. 現在インストールされている PostgreSQL を削除する
    # rpm -e postgresql-server.
  3. EPEL のインストール (postgis や gdal から使われるライブラリが SL Linux のレポジトリにはない)
    • https://fedoraproject.org/wiki/EPEL
    • 同じパッケージがあった場合、SL Linux のレポジトリの方を優先するように設定する
      # yum -y install yum-priorities
    • /etc/yum.repos.d/sl.repo の優先度を 1 に設定
      [sl]
      
       ...
      
      priority=1
      exclude=postgresql*
      
      [sl-security]
      
       ...
      
      priority=1
      exclude=postgresql*
    • EPEL のインストール
      # rpm -ivh http://ftp.jaist.ac.jp/pub/Linux/Fedora/epel/6/i386/epel-release-6-8.noarch.rpm
      # yum -y update epel-release
  4. ようやく postgis2 のインストール
    # yum -y install postgis2_92
    PostgreSQL を含めて、もろもろインストール完了
    Installed:
     postgis2_92.x86_64 0:2.0.3-2.rhel6                                                                                                              
    Dependency Installed:
     cfitsio.x86_64 0:3.240-3.el6
     gdal.x86_64 0:1.7.3-15.el6
     geos.x86_64 0:3.3.8-1.rhel6
     hdf5.x86_64 0:1.8.5.patch1-7.el6
     json-c.x86_64 0:0.10-2.el6
     libdap.x86_64 0:3.11.0-1.el6
     libgeotiff.x86_64 0:1.2.5-5.el6
     libgfortran.x86_64 0:4.4.7-3.el6
     librx.x86_64 0:1.5-14.el6
     netcdf.x86_64 0:4.1.1-3.el6.3
     ogdi.x86_64 0:3.2.0-0.14.beta2.el6
     postgresql92.x86_64 0:9.2.4-1PGDG.rhel6
     postgresql92-libs.x86_64 0:9.2.4-1PGDG.rhel6
     proj.x86_64 0:4.8.0-2.rhel6
     unixODBC.x86_64 0:2.2.14-12.el6_3
     xerces-c.x86_64 0:3.0.1-20.el6
    ついで
    # yum -y install postgresql92-server
    # yum -y install pgadmin3
  5. データベース初期化
    # service postgresql-9.2 initdb
    データベースを初期化中:                                    [  OK  ]
  6. 起動・停止
    # /sbin/service postgresql-9.2 start
    postgresql-9.2 サービスを開始中:                           [  OK  ]
    # /sbin/service postgresql-9.2 stop
    postgresql-9.2 サービスを停止中:                           [  OK  ]
  7. 自動起動
    # /sbin/chkconfig --level 35 postgresql-9.2 on
    # /sbin/chkconfig --list | grep postgresql
    postgresql-9.2 	0:off	1:off	2:off	3:on	4:off	5:on	6:off
  8. 自動起動解除
    # /sbin/chkconfig postgresql-9.2 off
    # /sbin/chkconfig --list | grep postgresql
    postgresql-9.2 	0:off	1:off	2:off	3:off	4:off	5:off	6:off

Postgresql の設定 (Scientific Linux 6.4)

  1. postgre ユーザにパスワードを設定
    認証方法が peer (postgresqlユーザと同名のUnixユーザからアクセス可能) に設定されているので、postgres ユーザになる必要がある
    [kagyuu@app ~]$ su
    パスワード:
    [root@app kagyuu]# su postgres
    bash-4.1$ psql 
    psql (9.2.4)
    Type "help" for help.
    
    postgres=# alter user postgres with password 'postgres';
    ALTER ROLE
    postgres=# \q
  2. 認証方法をパスワードに変更する
    [root@app data]# pwd
    /var/lib/pgsql/9.2/data
    
    [root@app data]# diff -u pg_hba.conf.original pg_hba.conf
    --- pg_hba.conf.original	2013-09-24 21:18:08.739688440 +0900
    +++ pg_hba.conf	2013-09-24 21:27:45.344698197 +0900
    @@ -77,11 +77,12 @@
     # TYPE  DATABASE        USER            ADDRESS                 METHOD
     
     # "local" is for Unix domain socket connections only
    -local   all             all                                     peer
    +local   all             all                                     md5
     # IPv4 local connections:
    -host    all             all             127.0.0.1/32            ident
    +host    all             all             127.0.0.1/32            md5
     # IPv6 local connections:
    -host    all             all             ::1/128                 ident
    +host    all             all             ::1/128                 md5
     # Allow replication connections from localhost, by a user with the
     # replication privilege.
     #local   replication     postgres                                peer
  3. 再起動
    # /etc/init.d/postgresql-9.2 restart
    postgresql-9.2 サービスを停止中:                           [  OK  ]
    postgresql-9.2 サービスを開始中:                           [  OK  ]
  4. パスワードを聞かれることを確認
    [kagyuu@app ~]$ psql -U postgres
    ユーザ postgres のパスワード: 
    psql (9.2.4)
    "help" でヘルプを表示します.
    
    postgres=# 

template1 に Postgis を組み込む

  1. PostgreSQL の初期DB
    [~]$ psql92 -l -U postgres
    Password for user postgres:
                                      List of databases
       Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
    -----------+----------+----------+-------------+-------------+-----------------------
     postgres  | postgres | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 |
     template0 | postgres | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/postgres          +
               |          |          |             |             | postgres=CTc/postgres
     template1 | postgres | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/postgres          +
               |          |          |             |             | postgres=CTc/postgres
    (3 rows)
    • template0 : Database の基本構成。変更不可。
    • template1 : 新たに Database を作るときに、(特に指定しなければ) もととなる構成。通常は変更しない。
  2. template1 に Postgis を組み込むことにより、新たに業務 Database を作った場合、すぐに Postgis を使えるようにする。
    [~]$ /opt/local/lib/postgresql92/bin/createlang -U postgres plpgsql template1
    createlang: language "plpgsql" is already installed in database "template1"
    [~]$ psql92 -U postgres -d template1 -f /opt/local/share/postgresql92/contrib/postgis-2.1/postgis.sql
    [~]$ psql92 -U postgres -d template1 -f /opt/local/share/postgresql92/contrib/postgis-2.1/spatial_ref_sys.sql
    [~]$ psql92 -U postgres -d template1 -f /opt/local/share/postgresql92/contrib/postgis-2.1/postgis_comments.sql
    plpgsql は、template1 に設定済みのようだが念のため
  3. データベース作成
    [~]$ /opt/local/lib/postgresql92/bin/createdb -U postgres -E UTF-8 -e demo
    CREATE DATABASE demo ENCODING 'UTF-8';
  4. 構築確認
    [~]$ psql92 -U postgres -d demo
    Password for user postgres:
    psql92 (9.2.4)
    Type "help" for help.
    
    demo=# \d
                   List of relations
     Schema |       Name        | Type  |  Owner
    --------+-------------------+-------+----------
     public | geography_columns | view  | postgres
     public | geometry_columns  | view  | postgres
     public | spatial_ref_sys   | table | postgres
    (3 rows)
    
    demo=# SELECT postgis_version();
                postgis_version
    ---------------------------------------
     2.1 USE_GEOS=1 USE_PROJ=1 USE_STATS=1
    (1 row)
    
    demo=# \q

データの投入

  1. Natural Earth から、国境データをダウンロード
  2. SQL 文に変換
    $ /opt/local/lib/postgresql92/bin/shp2pgsql -D -i -I -s 4326 -W LATIN1 ne_10m_admin_0_countries.shp COUNTRIES_TABLE > countries.sql
    • 中身は、CREATE TABLE 文、INSERT 文、COMMIT
    • "-D" ダンプ形式 (投入が少し早くなる。未指定の場合 INSERT INTO countries_table(...) VALUES (...))
      COPY countries_table FROM stdin;
      3   Admin-0 country 5.00000 ... <CR>
    • "-i" : 整数を 32 bit に変換する (指定しないと 16bit)
    • "-I" : geometry 型の列に index を設定する ★INDEXは検索速度の面から必須★
      CREATE INDEX "countries_table_geom_gist" ON "countries_table" USING GIST ("geom");
    • "-s 4326" (SRID4326) : WGS84 投影座標系。未指定の場合 0 (未定義) になるので、必ず指定する必要あり。Natural Earth のデータは WGS84 http://www.naturalearthdata.com/features/
    • "-W LATIN1" : shape ファイルの文字コード
    • 国土交通省の「国土数値情報ダウンロードサービス http://nlftp.mlit.go.jp/ksj/ 」では、"-s 4612 -W cp932"
  3. PostgreSQL に格納
    $ psql92 -U postgres -d demo -f countries.sql
    Password for user postgres:
    SET
    SET
    BEGIN
    psql92:countries.sql:67: NOTICE:  CREATE TABLE will create implicit sequence "countries_table_gid_seq" for serial column "countries_table.gid"
    CREATE TABLE
    psql92:countries.sql:68: NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index "countries_table_pkey" for table "countries_table"
    ALTER TABLE
                            addgeometrycolumn
    -----------------------------------------------------------------
     public.countries_table.geom SRID:4326 TYPE:MULTIPOLYGON DIMS:2
    (1 row)
    
    CREATE INDEX
    COMMIT

検索して遊んでみる

qgis で、postgis に格納されているポリゴンをグラフィック表示

  1. PostGis?レイヤの追加
    qgis1.png
  2. テーブルを選択して [追加]
    qgis2.png
  3. 全データがグラフィック表示された
    qgis3.png
  4. フランスのみ (WHERE sovereignt = 'France') と ブラジルのみ (WHERE sovereignt = 'Brazil') のレイヤを追加。確かにフランスとブラジルは国境を接している
    qgis4.png

GIS


添付ファイル: fileqgis4.png 2054件 [詳細] fileqgis3.png 2204件 [詳細] fileqgis2.png 2043件 [詳細] fileqgis1.png 2142件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS   sitemap
Last-modified: 2013-09-24 (火) 21:45:16 (3867d)
Short-URL:
ISBN10
ISBN13
9784061426061