GISっ子/コンテナでshp2pgsql

はじめに

空間データベース · GIS実習オープン教材
「PostGIS x.x Shapefile and DBF Loader Exporter」を使わずに shp2qgsql で代用する手順です

PosgGIS自体もコンテナで起動してしまいます

PostGIS, pgAdmin, (と Adminer)を立ち上げる

PostGISワークショップをコンテナで試す の手順をそのまま使います

# Pod(コンテナ)起動
wget -qO docker-compose.yml 'https://gist.github.com/kemasoft-gist/4195ef7d8dd2861e9fe8a8d583cfe113/raw'
podman-compose up -d

id / pw はいずれも postgres / example です

ここまで来たら 空間データベース · GIS実習オープン教材 の「空間解析機能の追加」までを手順どおりに進めます

PostGISに接続するコンテナを起動

ホストのファイルを操作できるよう、ここではカレントディレクトリをコンテナ内の /mnt にマウントして起動します

# ホストのカレントディレクトリに shp ファイルがある
$ ls
README.md    station.csv  station.prj  station.shp  tokyo.cpg  tokyo.prj  tokyo.shp
station.cpg  station.dbf  station.qpj  station.shx  tokyo.dbf  tokyo.qpj  tokyo.shx


# コンテナを起動し、/mnt のファイルを確認
$ podman run --rm -it -v .:/mnt postgis:12-3.0 bash
root@c119a1ba1f99:/# ls /mnt
README.md    station.csv  station.prj  station.shp  tokyo.cpg  tokyo.prj  tokyo.shp
station.cpg  station.dbf  station.qpj  station.shx  tokyo.dbf  tokyo.qpj  tokyo.shx

shp2pgsqlコマンドをインストール

shp2pgsql は postgis パッケージにあります

root@c119a1ba1f99:/# apt update
Get:1 http://deb.debian.org/debian buster InRelease [121 kB]
(略)
All packages are up to date.

root@c119a1ba1f99:/# apt -y install postgis
Reading package lists... Done
(略)
Setting up postgis (3.0.1+dfsg-4.pgdg100+1) ...
Setting up postgis-doc (3.0.1+dfsg-4.pgdg100+1) ...

shpファイルを新しいテーブルにインポート

# /mnt に移動
root@c119a1ba1f99:/# cd /mnt

# tokyo.shp をインポート
#   -h 192.168.10.151 : PosgGISのIPアドレス(=母艦のIPアドレス)
#   -d tokyo          : データベース名(GIS実習オープン教材の場合は tokyo)
root@c119a1ba1f99:/mnt# shp2pgsql -s 2451 tokyo.shp   public.tokyo   | psql -h 192.168.10.151 -d tokyo -U postgres
Field area is an FTDouble with width 11 and precision 3
Field density is an FTDouble with width 11 and precision 3
Shapefile type: Polygon
Postgis type: MULTIPOLYGON[2]
SET
SET
BEGIN
CREATE TABLE
ALTER TABLE
                   addgeometrycolumn
-------------------------------------------------------
 public.tokyo.geom SRID:2451 TYPE:MULTIPOLYGON DIMS:2
(1 row)

INSERT 0 1
INSERT 0 1
(略)
INSERT 0 1
COMMIT
ANALYZE

# station.shp をインポート
#   -h 192.168.10.151 : PosgGISのIPアドレス(=母艦のIPアドレス)
#   -d tokyo          : データベース名(GIS実習オープン教材の場合は tokyo)
root@c119a1ba1f99:/mnt# shp2pgsql -s 2451 station.shp public.station | psql -h 192.168.10.151 -d tokyo -U postgres
Shapefile type: Point
Postgis type: POINT[2]
SET
SET
BEGIN
CREATE TABLE
ALTER TABLE
                addgeometrycolumn
--------------------------------------------------
 public.station.geom SRID:2451 TYPE:POINT DIMS:2
(1 row)

INSERT 0 1
INSERT 0 1
(略)
INSERT 0 1
COMMIT
ANALYZE

# コンテナ停止
root@c119a1ba1f99:/mnt# exit
exit

一気に流す方法

podman run でシェルを起動し、ヒアドキュメントでシェルスクリプトを流せばOK

podman run --rm -i -v .:/mnt postgis:12-3.0 bash -x <<'...'
apt update
apt -y install postgis
shp2pgsql -s 2451 /mnt/tokyo.shp   public.tokyo   | psql -h 192.168.10.151 -d tokyo -U postgres
shp2pgsql -s 2451 /mnt/station.shp public.station | psql -h 192.168.10.151 -d tokyo -U postgres
...

おまけ:new_geomカラムの追加

空間データベース · GIS実習オープン教材の「空間結合」の冒頭部分でエラーになると思います

CREATE INDEX new_geom ON station USING GiST(new_geom);を実行し、空間インデックスを作成する。

これを回避するには、予め new_geom カラムを追加すればOKです

ALTER TABLE station ADD new_geom geometry(Point,2451);

その後 CREATE INDEX new_geom ON station USING GiST(new_geom); します


トップ   編集 凍結 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2020-07-29 (水) 14:05:51