我想使用ogr2ogr以便在postgis数据库中导入shapefile。我已经成功安装了ogr2ogr,并且从pgsql运行了以下命令:
ogr2ogr -f "PostgreSQL" PG:"host=localhost user=user_1 password=***** dbname=imports" world_boundaries.shp
我得到的是一条错误消息:
Unable to open datasource `world_boundaries.shp' with the following drivers: --a list of drivers follows (ESRI Shape File etc.)
我也尝试定义了shapefile的完整路径,但得到了相同的消息。
我也尝试运行:
ogrinfo world_boundaries.shp
一样。
解决文件权限问题后,出现以下错误:
ERROR 1: AddGeometryColumn failed for layer world_boundaries, layer creation has failed.
ERROR 1: Terminating translation prematurely after failed
translation of layer world_boundaries (use -skipfailures to skip errors)
我也尝试通过GUI shp2pgsql导入它,并且出现以下错误:
ALTER TABLE "public".""
Failed in pgui_exec(): ERROR: permission denied for relation spatial_ref_sys
CONTEXT: SQL statement "SELECT SRID FROM spatial_ref_sys WHERE SRID = new_srid"
PL/pgSQL function addgeometrycolumn(character varying,character varying,character varying,character varying,integer,character varying,integer,boolean) line 50 at SQL statement
SQL statement "SELECT AddGeometryColumn('',$1,$2,$3,$4,$5,$6,$7)"
PL/pgSQL function addgeometrycolumn(character varying,character varying,character varying,integer,character varying,integer,boolean) line 5 at SQL statement
Shapefile import failed.
这次的问题是该数据库用户没有足够的权限。这修复了它:
GRANT ALL ON TABLE spatial_ref_sys TO my_user_name;
下一条错误消息是:
Warning 1: Geometry to be inserted is of type 3D Multi Polygon, whereas the layer geometry type is 3D Polygon.
Insertion is likely to fail
ERROR 1: INSERT command for new feature failed.
ERROR: Geometry type (MultiPolygon) does not match column type (Polygon)
所以看来我需要使用参数:-nlt MULTIPOLYGON但是,当我这样做时,我又遇到了另一个错误,这对我来说没有任何意义:
ERROR 1: PostgreSQL driver doesn't currently support database creation.
Please create database with the `createdb' command.
PostgreSQL driver failed to create PG:host=localhost user=my_user_name dbname=my_database password=password -nlt POLYGON
但是它是使用shp2pgsql GUI加载的。
@elrobis的评论使此方法最终得以工作。数据已正确加载到db中!
ogr2ogr -f "PostgreSQL" PG:"host=localhost user=geonode dbname=geonode_imports password=geonode" -nlt GEOMETRY wld_bnd_adm0_gaul_2015.shp
-nln layername
参数,也许一起-overwrite
看看是否涉及到生活。另外,如果我是你,我会sudo
与我的postgres超级用户一起运行ogr2ogr,以便超级确定排除权限和特权。一旦您的脚本是可靠的,那么您就可以查看讨厌的权限。:)