pgsal2shpを利用
PostGISでpgsql2shpというツールが提供されている。
shp2pgsql <shapeファイル> <DataBase名> > sqlファイル名 でsqlファイルが作成できる。
mesh_250m_osakaというポリゴンデータのテーブルを作成するスクリプトができる。
shp2pgsql mesh05_jgd_27.shp mesh_250m_osaka > mesh05_jgd_27.sql
できたファイルを見てみる。
more mesh05_jgd_27.sql
SET STANDARD_CONFORMING_STRINGS TO ON;
BEGIN;
CREATE TABLE "mesh_250m_osaka" (gid serial PRIMARY KEY,
"code" varchar(10));
SELECT AddGeometryColumn('','mesh_250m_osaka','the_geom','-1','MULTIPOLYGON',2);
INSERT INTO "mesh_250m_osaka" ("code",the_geom) VALUES ('5135302842',
...
カラムのcode, the_geomは何を意味するのだろう?
添付ファイルの意味だった。
まず、このshapeファイルは何か見てみる。
ogrinfo -summary mesh03_jgd_27_shp
INFO: Open of `mesh03_jgd_27_shp'
using driver `ESRI Shapefile' successful.
1: mesh03_jgd_27 (Polygon)
polygonデータか。
では、pointデータでもう一度
shp2pgsql transp_1_1.shp transp_all > transp_1_1.sql
Shapefile type: PointZ
Postgis type: POINT[4]
PostgreSQLへShapeファイルを登録する
Shapeファイルから直接データベースへ登録する。
ogr2ogr -f "PostgreSQL" PG:dbname=mydb transp_1_1.shp
SQLスクリプトを作って登録する方法
まず、PostGISをのAPIを使えるようにすることから始める。
PL/pgSQL言語を新たに作成したデータベースで有効にする
createlang plpgsql
PostGISオブジェクトと関数定義をデータベースにロードする。
psql -d mydb -f /usr/share/postgresql/8.4/contrib/postgis.sql
先ほど作成したtransp_1_1.sqlをロードする。
mydb=# \i /home/postgres/data_file/transp_1_1/transp_1_1.sql
結果はを見てみる
mydb-# \d
リレーションの一覧
スキーマ | 名前 | 型 | 所有者
----------+-------------------------+------------+----------
public | geometry_columns | テーブル | postgres
public | mesh_250m_osaka | テーブル | postgres
public | mesh_250m_osaka_gid_seq | シーケンス | postgres
public | mydb | テーブル | postgres
public | mydb_gid_seq | シーケンス | postgres
public | spatial_ref_sys | テーブル | postgres
public | transp_all | テーブル | postgres
public | transp_all_gid_seq | シーケンス | postgres
(8 行)
参考サイト