#author("2020-07-29T14:03:37+09:00","default:nobuoki","nobuoki") #author("2020-07-29T14:07:26+09:00","default:nobuoki","nobuoki") * はじめに [#d7dd2c42] [[空間データベース · GIS実習オープン教材>https://gis-oer.github.io/gitbook/book/materials/08/08.html#%E3%82%B8%E3%82%AA%E3%82%B3%E3%83%BC%E3%83%87%E3%82%A3%E3%83%B3%E3%82%B0]]で 「PostGIS x.x Shapefile and DBF Loader Exporter」を使わずに shp2qgsql で代用する手順です PosgGIS自体もコンテナで起動してしまいます * PostGIS, pgAdmin, (と Adminer)を立ち上げる [#ne13ae5f] [[PostGISワークショップをコンテナで試す>GISっ子/PostGISワークショップをコンテナで試す]] の手順をそのまま使います #prism(bash){{{ # Pod(コンテナ)起動 wget -qO docker-compose.yml 'https://gist.github.com/kemasoft-gist/4195ef7d8dd2861e9fe8a8d583cfe113/raw' podman-compose up -d }}} - 5432/tcp: PosgGIS - 8000/tcp: pgAdmin - 8080/tcp: Adminer id / pw はいずれも postgres / example です ここまで来たら [[空間データベース · GIS実習オープン教材>https://gis-oer.github.io/gitbook/book/materials/08/08.html#%E3%82%B8%E3%82%AA%E3%82%B3%E3%83%BC%E3%83%87%E3%82%A3%E3%83%B3%E3%82%B0]] の「空間解析機能の追加」までを手順どおりに進めます * PostGISに接続するコンテナを起動 [#hb8060f4] ホストのファイルを操作できるよう、ここではカレントディレクトリをコンテナ内の /mnt にマウントして起動します #prism(bash){{{ # ホストのカレントディレクトリに 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コマンドをインストール [#fea37848] shp2pgsql は postgis パッケージにあります #prism(bash){{{ 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ファイルを新しいテーブルにインポート [#fedab580] #prism(bash){{{ # /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 }}} * 一気に流す方法 [#h892f629] podman exec でシェルを起動し、ヒアドキュメントでシェルスクリプトを流せばOK podman run でシェルを起動し、ヒアドキュメントでシェルスクリプトを流せばOK #prism(bash){{{ 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カラムの追加 [#mc4c54fd] [[空間データベース · GIS実習オープン教材>https://gis-oer.github.io/gitbook/book/materials/08/08.html#%E3%82%B8%E3%82%AA%E3%82%B3%E3%83%BC%E3%83%87%E3%82%A3%E3%83%B3%E3%82%B0]]の「空間結合」の冒頭部分でエラーになると思います CREATE INDEX new_geom ON station USING GiST(new_geom);を実行し、空間インデックスを作成する。 これを回避するには、予め new_geom カラムを追加すればOKです #prism(SQL){{{ ALTER TABLE station ADD new_geom geometry(Point,2451); }}} その後 CREATE INDEX new_geom ON station USING GiST(new_geom); します