2011年4月23日土曜日

14 Open Street Map 1 - PostGIS に登録

14-1 OSM ダウンロード

1 GeoServer and OpenStreetMap ページ
http://blog.geoserver.org/2009/01/30/geoserver-and-openstreetmap/

の 「download sections of the data set」リンクをクリックします。
2 Cloudmade の Mappers の Download ページ
http://downloads.cloudmade.com/
の Last maps update: 03 February 2011 の「Asia」リンクをクリックします。
3 Last maps update: 03 February 2011 の「World > Asia」の「Japan」リンクをクリックします。
4 「japan.shapefiles.zip」をクリックし、「japan.shapefiles.zip を開く」で「ファイルを保存」ボタンをクリックしてダウンロードします。

5 japan.shapefiles.zip を解凍します。

user@deb-vmw:~/ダウンロード$ mkdir japan_shapefiles
user@deb-vmw:~/ダウンロード$ mv japan.shapefiles.zip japan_shapefiles/
user@deb-vmw:~/ダウンロード$ cd japan_shapefiles/
user@deb-vmw:~/ダウンロード/japan_shapefiles$ ls
japan.shapefiles.zip
user@deb-vm:~/ダウンロード/japan_shapefiles$ unzip japan.shapefiles.zip
Archive: japan.shapefiles.zip
inflating: japan_natural.dbf
inflating: japan_administrative.dbf
inflating: japan_water.prj
inflating: japan_highway.prj
inflating: japan_coastline.shx
inflating: japan_location.prj
inflating: japan_poi.shp
inflating: japan_administrative.shx
inflating: japan_coastline.dbf
inflating: japan_water.dbf
inflating: japan_natural.prj
inflating: readme.txt
inflating: japan_poi.shx
inflating: japan_administrative.shp
inflating: japan_coastline.shp
inflating: japan_water.shp
inflating: japan_poi.prj
inflating: japan_highway.shx
inflating: japan_coastline.prj
inflating: japan_water.shx
inflating: japan_poi.dbf
inflating: japan_location.shx
inflating: japan_location.dbf
inflating: japan_administrative.prj
inflating: japan_highway.shp
inflating: japan_natural.shp
inflating: japan_natural.shx
inflating: japan_highway.dbf
inflating: japan_location.shp

japan_administrative
japan_coastline
japan_highway
japan_location
japan_natural
japan_poi
japan_water
readme.txt

内容:道路、タイプ:polyline、属性:highway type,name,一方通行

14-2 PostGIS に japan_highway.shp を登録します。

1 template_postgisをテンプレートとして、オーナーが user で osm というデータベースを作成します。

user@deb6-vmw:~/ダウンロード/japan_shapefiles$ su - postgres
パスワード:
postgres@deb6-vmw:~$ createdb -T template_postgis -O user osm
postgres@deb6-vmw:~$ psql -l

データベース一覧
名前 | 所有者 | エンコーディング | 照合順序 | Ctype(変換演算子) | アクセス権
------------------+----------+------------------+-------------+-------------------+-----------------------
nippon_bmi | user | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 |
nippon_dnli | user | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 |
osm | user | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 |
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
template_postgis | postgres | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 |
(stdin):q

osm データベースの geometry_column テーブルと spatial_ref_sys テーブルのオーナーを user に変更します。

postgres@deb6-vmw:~$ psql osm
psql (8.4.7)
"help" でヘルプを表示します.

osm-# \d

リレーションの一覧
スキーマ | 名前 | 型 | 所有者
----------+-------------------+----------+----------
public | geography_columns | ビュー | postgres
public | geometry_columns | テーブル | postgres
public | spatial_ref_sys | テーブル | postgres
(3 行)

osm=# ALTER TABLE geometry_columns OWNER TO user;
ALTER TABLE
osm=# ALTER TABLE spatial_ref_sys OWNER TO user;
ALTER TABLE
osm=# \d

リレーションの一覧
スキーマ | 名前 | 型 | 所有者
----------+-------------------+----------+----------
public | geography_columns | ビュー | postgres
public | geometry_columns | テーブル | user
public | spatial_ref_sys | テーブル | user
(3 行)

osm=# \q


2 shp2pgsqlコマンドを使ってシェイプファイルを sql ファイルを作成します。
一般ユーザでシェイプファイルのあるディレクトリに移動して、次のように入力します。

postgres@deb-vmw:~$ exit
logout
user@deb6-vmw:~$ cd ダウンロード/japan_shapefiles
user@deb6-vmw:~/ダウンロード/japan_shapefiles$ shp2pgsql japan_highway japan_highway > japan_highway.sql
Shapefile type: Arc
Postgis type: MULTILINESTRING[2]
Unable to convert field value "南牧場線" to UTF-8: iconv reports "Invalid or incomplete multibyte or wide character"

sql ファイルが作成できなかったので、utf-8 エンコードを utf-8 にするという(?)オプションを追加しました。

user@deb6-vmw:~/ダウンロード/japan_shapefiles$ shp2pgsql -W utf-8 japan_highway japan_highway > japan_highway.sql
Shapefile type: Arc
Postgis type: MULTILINESTRING[2]

psqlコマンドを使ってデータベースに登録します。
一般ユーザで sql ファイルのあるディレクトリに移動して、次のように入力します。

user@deb6-vmw:~/Download/japan_shapefiles$ psql -d osm -f japan_highway.sql

メッセージが流れて、終了するまでに時間がかかります。
データベースを確かめてみます。
user@deb6-vmw:~/ダウンロード/japan_shapefiles$ psql osm
psql (8.4.7)
"help" でヘルプを表示します.

osm=> \dt


List of relations
Schema | Name | Type | Owner
--------+------------------+-------+-------
public | geometry_columns | table | user
public | japan_highway | table | user
public | spatial_ref_sys | table | user
(3 rows)

osm=> \d japan_highway

テーブル "public.japan_highway"
カラム | 型 | 修飾語

----------+------------------------+----------------------------------------------------------------
gid | integer | not null デフォルト nextval('japan_highway_
gid_seq'::regclass) type | character varying(24) |
name | character varying(133) |
oneway | character varying(11) |
lanes | numeric(11,0) |
the_geom | geometry |
インデックス:
"japan_highway_pkey" PRIMARY KEY, btree (gid)CHECK 制約:
"enforce_dims_the_geom" CHECK (st_ndims(the_geom) = 2)
"enforce_geotype_the_geom" CHECK (geometrytype(the_geom) = 'MULTILINESTRING'::text OR the_geom IS NULL)
"enforce_srid_the_geom" CHECK (st_srid(the_geom) = (-1))
~
(stdin):q

osm=> \q

続く ...

0 件のコメント: