ogr2ogrコマンドがうまく動かない問題
目次
こんなことを書く
- ShapefileをPost gisデータベースにInsertするためのコマンドがある。ogr2orgという。
- ogr2ogrコマンドがうまく動かない。
- バージョン問題だった。
前提
- 地理情報の入力はShapefile
- 地理情報の保存先はPost GIS
そもそもogr2ogrとは?
地理情報ファイル形式をよしなに変換してくれるコマンド。
GDALというパッケージ群の一部として存在してる。
今回のようにShapefile -> PostGISにしたいときもogr2ogrコマンドでOK
バージョン問題で困った話
org2ogrコマンドを書いて、実行してみる。エラーが出る。
1 |
$ ogr2ogr -f "PostgreSQL" PG:"host=localhost port=5432 user='postgis_test' password='devel' dbname='postgis_db'" /home/kensuke-mi/Downloads/shape_file -nln cities_city -append |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
ERROR 1: ERROR: column s.consrc does not exist LINE 1: ...nrelid = c.oid AND a.attnum = ANY (s.conkey) AND (s.consrc L... ^ HINT: Perhaps you meant to reference the column "s.conkey" or the column "s.conbin". ERROR 1: ERROR: column s.consrc does not exist LINE 1: ...nrelid = c.oid AND a.attnum = ANY (s.conkey) AND (s.consrc L... ^ HINT: Perhaps you meant to reference the column "s.conkey" or the column "s.conbin". ERROR 1: ERROR: current transaction is aborted, commands ignored until end of transaction block ERROR 1: INSERT command for new feature failed. ERROR: current transaction is aborted, commands ignored until end of transaction block Command: INSERT INTO "cities_city" ("geometry" , "name") VALUES ('0101000020E6100000B0E8773385EB4CC0C0307F9B703D41C0'::GEOMETRY, 'Colonia del Sacramento') RETURNING "id" ERROR 1: Unable to write feature 0 from layer ne_10m_populated_places. ERROR 1: Terminating translation prematurely after failed translation of layer ne_10m_populated_places (use -skipfailures to skip errors) |
いまいちエラーの意味をわからない。
とりあえずInsert先のPostGISテーブルを確認してみる。
1 2 3 4 |
postgis_db=# select * from cities_city; id | name | geometry ----+------+---------- (0 rows) |
エラーメッセージの言うとおり、 s.consrc は存在していない。では、そもそも s.consrc ってなんだ?
QGISのエディタでShapefileの中身を確認してみる。

エラーに該当するフィールド名がそもそも存在しない。
結局はバージョンが問題だった
調べてみると、こんな記事があった。
1 2 |
$ ogrinfo --version GDAL 2.4.2, released 2019/06/28 |
condaでインストールすることがもっとも早いらしい。condaのインストールを試してみる。
バージョンが最新になった。これでOK
1 2 |
$ ogrinfo --version GDAL 3.1.4, released 2020/10/20 |
これでコマンドが通った。
GDALを自分でインストールした記憶がない。おそらくQGISをインストールした時に、付属してインストールされたのだろう。
(参考までに)構文エラーで困った話
ogr2ogrコマンドの構文をよくわからなかった。それで、何度か失敗した話
1 |
<code>ogr2ogr -f "PostgreSQL" PG:"dbname=postgis_db user=postgis_testpassword=devel" <shapefile> -nln cities_city -append</code> |
エラーが出る。メッセージから判断をすると、どうもPGに接続するためのドライバが問題らしい。
1 |
ERROR 1: PQconnectdb failed.FATAL: Peer authentication failed for user "postgis_test"<br>FAILURE:Unable to open datasource `PG:dbname=postgis_db user=postgis_test password=devel' with the following drivers. -> `PCIDSK' -> `netCDF' -> `JP2OpenJPEG' -> `PDF' -> `ESRI Shapefile' -> `MapInfo File' -> `UK .NTF'<br> |
問題は表記法にあった。シングルクオートが抜けていた。あとhostを指定していなかった。
よって、正しい構文は次の通り。
1 |
ogr2ogr -f "PostgreSQL" PG:"host=localhost port=5432 user='postgis_test' password='devel' dbname='postgis_db'" cities/ne_10m_populated_places.shp -nln cities_city -append |
ディスカッション
コメント一覧
まだ、コメントがありません