PostgreSQL shapefile 导入导出

Shapefile文件组介绍
一个Shapefile由一组多个文件组成(其中.shp, .dbf, .shx三个是必须的,其他文件时可选的):
.shp: 必须的。图形格式,用于保存元素的几何实体。
.dbf: 必须的。属性数据格式,以dBase IV的数据表格式存储每个几何形状的属性数据。
.shx: 必须的。图形索引格式,几何体位置索引,记录每一个几何体在.shp文件中的位置,能够加快向前或向后搜索一个几何体的效率。

下面文件时可选的:
.prj: 可选的。投影格式,用于保存地理坐标系统与投影信息,投影描述文本文件。
.sbn & .sbx: 可选的。几何体的空间索引。
.fbn & .fbx: 可选的。只读的Shapefiles的几何体的空间索引。
.cpg: 可选的。用于描述.dbf文件的代码页,指明其使用的字符编码。
.ain & .aih: 可选的。列表中活动字段的属性索引。
.ixs & .mxs: 可选的。可读写Shapefile文件的地理编码索引,后者为ODB格式的地理编码索引。
.atx: .dbf文件的属性索引,其文件名格式为shapefile.columnname.atx (ArcGIS 8及之后的版本)。
.shp.xml: 可选的。以XML格式保存元数据。

1.pgsql2shp & shp2pgsql 命令行工具方式导入导出 shapefile

1.1)PostgreSQL 空间数据表导出为 shapefile
进入 $PG_HOME/bin 目录下执行脚本 pgsql2shp 
[root@pgmaster ~]# cd /usr/pgsql-12/bin/
[root@pgmaster bin]#

查看命令参数详情
[root@pgmaster bin]# pgsql2shp --help
pgsql2shp: illegal option -- -
RELEASE: 3.0.5 (df5f8b7)
USAGE: pgsql2shp [<options>] <database> [<schema>.]<table>
       pgsql2shp [<options>] <database> <query>

OPTIONS:
  -f <filename>  Use this option to specify the name of the file to create.
  -h <host>  Allows you to specify connection to a database on a
     machine other than the default.
  -p <port>  Allows you to specify a database port other than the default.
  -P <password>  Connect to the database with the specified password.
  -u <user>  Connect to the database as the specified user.
  -g <geometry_column> Specify the geometry column to be exported.
  -b Use a binary cursor.
  -r Raw mode. Do not assume table has been created by the loader. This would
     not unescape attribute names and will not skip the 'gid' attribute.
  -k Keep PostgreSQL identifiers case.
  -m <filename>  Specify a file containing a set of mappings of (long) column
     names to 10 character DBF column names. The content of the file is one or
     more lines of two names separated by white space and no trailing or
     leading space. For example:
         COLUMNNAME DBFFIELD1
         AVERYLONGCOLUMNNAME DBFFIELD2
  -? Display this help screen.

将 PostgreSQL 数据库中的 geometry 表导出为 shapefile
导出 testdb01 数据库的 public 模式下的 shapefile 表
[root@pgmaster ~]# cd /usr/pgsql-12/bin/
[root@pgmaster bin]# mkdir -p "/opt/geom_exp/shape/geom_test01_point"
[root@pgmaster bin]# pgsql2shp -f "/opt/geom_exp/shape/geom_test01_point/geom_test01_point.shp" -h 127.0.0.1 -u test -p 5432 -P 1q2w3e testdb01 public.geom_test01_point
Initializing... 
Done (postgis major version: 3).
Output shape: Point
Dumping: X [2 rows].
[root@pgmaster bin]# 
[root@pgmaster bin]# ls -l /opt/geom_exp/shape/geom_test01_point/
total 20
-rw-r--r-- 1 root root   5 Jun  6 16:45 geom_test01_point.cpg
-rw-r--r-- 1 root root 631 Jun  6 16:45 geom_test01_point.dbf
-rw-r--r-- 1 root root 256 Jun  6 16:45 geom_test01_point.prj
-rw-r--r-- 1 root root 156 Jun  6 16:45 geom_test01_point.shp
-rw-r--r-- 1 root root 116 Jun  6 16:45 geom_test01_point.shx
[root@pgmaster bin]# 
导出成功后,自动生成了5个相关文件:.cpg,.dbf,.prj,.shp,.shx

1.2)shapefil 导入到 PostgreSQL 空间数据库中

将 geom_test02_linestr.shp 导入导 testdb01 数据库的 test_schema 模式中(自动生成表 geom_test02_linestr )
[root@pgmaster ~]# cd /usr/pgsql-12/bin/
[root@pgmaster bin]# 
[root@pgmaster bin]# shp2pgsql -help
shp2pgsql: illegal option -- h
RELEASE: 3.0.5 (df5f8b7)
USAGE: shp2pgsql [<options>] <shapefile> [[<schema>.]<table>]
OPTIONS:
  -s [<from>:]<srid> Set the SRID field. Defaults to 0.
      Optionally reprojects from given SRID.
 (-d|a|c|p) These are mutually exclusive options:
     -d  Drops the table, then recreates it and populates
         it with current shape file data.
     -a  Appends shape file into current table, must be
         exactly the same table schema.
     -c  Creates a new table and populates it, this is the
         default if you do not specify any options.
     -p  Prepare mode, only creates the table.
  -g <geocolumn> Specify the name of the geometry/geography column
      (mostly useful in append mode).
  -D  Use postgresql dump format (defaults to SQL insert statements).
  -e  Execute each statement individually, do not use a transaction.
      Not compatible with -D.
  -G  Use geography type (requires lon/lat data or -s to reproject).
  -k  Keep postgresql identifiers case.
  -i  Use int4 type for all integer dbf fields.
  -I  Create a spatial index on the geocolumn.
  -m <filename>  Specify a file containing a set of mappings of (long) column
     names to 10 character DBF column names. The content of the file is one or
     more lines of two names separated by white space and no trailing or
     leading space. For example:
         COLUMNNAME DBFFIELD1
         AVERYLONGCOLUMNNAME DBFFIELD2
  -S  Generate simple geometries instead of MULTI geometries.
  -t <dimensionality> Force geometry to be one of '2D', '3DZ', '3DM', or '4D'
  -w  Output WKT instead of WKB.  Note that this can result in
      coordinate drift.
  -W <encoding> Specify the character encoding of Shape's
      attribute column. (default: "UTF-8")
  -N <policy> NULL geometries handling policy (insert*,skip,abort).
  -n  Only import DBF file.
  -T <tablespace> Specify the tablespace for the new table.
      Note that indexes will still use the default tablespace unless the
      -X flag is also used.
  -X <tablespace> Specify the tablespace for the table's indexes.
      This applies to the primary key, and the spatial index if
      the -I flag is used.
  -?  Display this help screen.

An argument of `--' disables further option processing.
  (useful for unusual file names starting with '-')
[root@pgmaster bin]#

导入语法:
shp2pgsql -s SRID -a -W GBK 文件名.shp 命名空间名.表名 | psql -h localhost -U postgres -d 数据库名-p 5432

执行导入:

[root@pgmaster bin]# shp2pgsql -s 4326 -c -W UTF-8 "/opt/geom_exp/shape/geom_test01_point/geom_test01_point.shp" test_schema.geom_test01_point | psql -h 127.0.0.1 -U test -d testdb01 -p 5432                 
Field id is an FTDouble with width 11 and precision 0
Shapefile type: Point
Postgis type: POINT[2]
Password for user test: 
SET
SET
BEGIN
CREATE TABLE
ALTER TABLE
                        addgeometrycolumn                        
-----------------------------------------------------------------
 test_schema.geom_test01_point.geom SRID:4326 TYPE:POINT DIMS:2 
(1 row)

INSERT 0 1
INSERT 0 1
COMMIT
ANALYZE
[root@pgmaster bin]#

注意:
1)导入时,需要输入数据库连接密码
2)如果表已经存在、要用插入模式,则参数 -c 换成 -a;
3)如果表已存在、想要先删除已存在的表,重新创建表和导入,则 -c 换成 -d
4)导入时请指定坐标系 -s 参数,例如: -s 4326 或者 -s 3587。如果是目的表已经存在、采用append写入的场景,srid必须设置成和目的表的空间字段的srid一致,否则无法写入

下面我把shp2pgsql导入执行的sql语句保存到sql文件、进行内容差异对比:

[root@pgmaster bin]# cd /opt/geom_exp/

[root@pgmaster geom_exp]# mkdir -p /opt/geom_exp/shp2sql/

[root@pgmaster geom_exp]# shp2pgsql -s 4326 -c -W UTF-8 "/opt/geom_exp/shape/geom_test01_point/geom_test01_point.shp" test_schema.geom_test01_point > ./shp2sql/geom_test01_point.4326.sql  
Field id is an FTDouble with width 11 and precision 0
Shapefile type: Point
Postgis type: POINT[2]
[root@pgmaster geom_exp]# 
[root@pgmaster geom_exp]# 
[root@pgmaster geom_exp]# 
[root@pgmaster geom_exp]# shp2pgsql -s 3857 -c -W UTF-8 "/opt/geom_exp/shape/geom_test01_point/geom_test01_point.shp" test_schema.geom_test01_point > ./shp2sql/geom_test01_point.3857.sql    
Field id is an FTDouble with width 11 and precision 0
Shapefile type: Point
Postgis type: POINT[2]
[root@pgmaster geom_exp]# 
[root@pgmaster geom_exp]# 
[root@pgmaster geom_exp]# shp2pgsql  -c -W UTF-8 "/opt/geom_exp/shape/geom_test01_point/geom_test01_point.shp" test_schema.geom_test01_point > ./shp2sql/geom_test01_point.noSRID.sql       
Field id is an FTDouble with width 11 and precision 0
Shapefile type: Point
Postgis type: POINT[2]
[root@pgmaster geom_exp]#

SRID=4326方式导入的数据的内容如下:

testdb01=> select id,name,st_asewkt(geom)  from test_schema.geom_test01_point ;
 id | name |               st_asewkt               
----+------+---------------------------------------
  1 | test | SRID=4326;POINT(-0.125 52.5)
  2 | test | SRID=4326;POINT(27.9116248 -33.01532)
(2 rows)

testdb01=> select *  from test_schema.geom_test01_point ;
 gid | id | name |                        geom                        
-----+----+------+----------------------------------------------------
   1 |  1 | test | 0101000020E6100000000000000000C0BF0000000000404A40
   2 |  2 | test | 0101000020E6100000F8382E3E60E93B40C47C7901F68140C0
(2 rows)

2. QGIS工具导入导出 shapefile
我本地安装的是QGIS Desktop 3.24.0
2.1)数据库导出shapefile步骤:
1)Browser->PostgreSQL->右键 New Connection 创建数据库连接。填写对应参数:Name,Host,Port,Database,以及认证配置(Authentication)->Basic,配置数据库连接账号(User name)和密码(Password)


2)选择想要导出的schema下的包含geometry类型的表,右键导出(Export Layer -> To File),也可以先双击表(双击会默认执行"Add Layer to Project")、再在Layers中勾选右键导出


3)导出参数配置:
   Format:ESRI Shapefile,默认为该格式,如果想要导出其他格式(GeoJSON、CSV等)则选择对应的格式导出
   File name:指定导出文件名,一般设置为何表名相同。默认导出路径在执行文件同路径下(C:\Program Files\QGIS 3.24.0\bin),如果要指定其他路径导出,则需要选择导出目录+指定导出文件名(文件名指定为路径+文件名)
   CRS: 指定导出坐标系,默认 EPSG:4326 - WGS84
   Encoding: UTF-8 导出生成.cpg文件,用于描述.dbf文件的代码页,指明其使用的字符编码
   Select fields to export and their export options:导出字段选择和其他参数配置,默认全选
   其他参数默认配置


4)点击“OK”导出shapefile文件

2.2)shapefile导入PostgreSQL数据库中步骤:
1)菜单:Database->DB Manager


2)Providers 选择 PostGIS 连接(如果未配置则先配置)
3)展开选择对应的schema
4)点击菜单栏下方的"Import Layer/File..."按钮
5)配置导入参数:
    Input:选择要导入的shapefile(xxx.shp)
    Output table: 
        schema: 导入的目的schema(默认填充)
        Table: 导入的目的表(默认填充)
    Option:其他选项,根据事情情况选择是否勾选。
    注意:Geometry column 如果不勾选配置,默认导入后的表的空间字段名称为 geom,如果需要修改,则勾选并指定列名

3.OGC/EPSG/SRS/CRS 等相关概念:

OGC: Open Geospatial Consortium (OGC) 开放地理空间信息联盟,它是一个非盈利的、国际化的、自愿协商的标准化组织,制定了一套标准,各种大厂商都几乎执行这些标准,使得不同厂商、不同产品之间可以通过统一的接口进行互操作。

EPSG: European Petroleum Survey Group (EPSG) 是一个协会,现在已经改名叫 OGP(Internation Association of Oil & Gas Producers)。它负责维护并发布坐标参考系统的数据集参数,以及坐标转换描述,该数据集被广泛接受并使用。例如:CGCS2000、WGS84坐标系在 EPSG 中都有定义,并有对应的code。查询官网:https://spatialreference.org/
SRS & CRS: 是同一个东西,都是坐标系。例如:WGS84。

EPSG: 4326 (WGS84) 是目前最流行的地理坐标系统。国际上会为每个坐标系统都会被分配一个EPSG代码,EPSG: 4326 就是 WGS84 的代码。GPS是基于WGS84的,所以通常我们得到的坐标数据都是 WGS84 的。
EPSG: 3857 (Pseudo-Mercator) 伪墨卡托投影,也被称为球体墨卡托,Web Mercator。它是基于墨卡托投影的,把 WGS84 坐标系投影到正方形上。
EPSG: 2436 (北京范围的投影坐标系) 北京的东(E)西(W)范围

查询与北京有关的坐标系命令:
select * from spatial_ref_sys where srtext ~* 'china';

SRID: 是坐标系在OGC标准中的唯一ID,和EPSG中的code是一致的。

PostgreSQL shapefile 导入导出相关推荐

  1. PostgreSQL COPY 导入/导出数据

    COPY 命令可以快速的导入数据到 PostgreSQL 中,文件格式类似CVS之类.适合批量导入数据,比 \i 和恢复数据表快. 导出表数据到文件或 STDOUT : COPY tablename ...

  2. postgresql数据库导入导出

    导出数据库 1.定位到postgresql的安装目录bin下 :# cd  /usr/local/postgresql-9.4.8/bin 2.执行导出命令:# ./pg_dump -U userna ...

  3. 使用pgAdmin对Postgresql数据库导入导出

    1.安装Postgresql数据库 2.打开pgAdmin界面 3.右键选择想要导出的数据库,这里选择mydatabase,之后选择backup(备份) 4.选择了备份按钮之后,弹出如下提示框,其中提 ...

  4. Postgresql 批量导入导出表数据

    postgresql没有自带的批量导入导出成CSV的指令,但是有一个调用执行文件的指令(\i),这个指令可以执行编写的脚本.例如 一个a.txt文件,里面内容是 #将xxx表数据导出xxx文件 \co ...

  5. PostgreSQL 统计信息pg_statistic格式及导入导出dump_stat - 兼容Oracle

    标签 PostgreSQL , dump_stat , 统计信息 , 导出导入 背景 <PostgreSQL 规格评估 - 微观.宏观.精准 多视角估算数据库性能(选型.做预算不求人)> ...

  6. postgis导入导出shp文件

    postgis导入导出shp文件,主要有两种方式: 一.借助PostGIS shapefile工具 具体操作可参考     PostGIS教程四:加载空间数据 https://blog.csdn.ne ...

  7. postgis 导出 栅格_postgis常见的空间数据的导入导出

    本片文章简单记录了postgis涉及的常用数据导入导出的使用方法. 1.将osm数据导入postgis osm2pgsql -c -d osm -l -E 4326 -S /usr/share/osm ...

  8. postgres 数据导入导出

    1.使用容器启动postgres数据库 docker run -d -p 5432:5432 --restart=always --name pgsql -e LANG="C.UTF-8&q ...

  9. mysql navicat导入bcp_SQL Server中BCP导入导出用法详解

    bcp命令是SQL Server提供的一个快捷的数据导入导出工具.使用它不需要启动任何图形管理工具就能以高效的方式导入导出数据.bcp是SQL Server中负责导入导出数据的一个命令行工具,它是基于 ...

  10. 数据库Mysql的学习(八)-储存过程和事务和导入导出

    储存过程 DELIMITER // CREATE PROCEDURE pro1() BEGIN SELECT book_id,book_name,category FROM bookinfo t1 J ...

最新文章

  1. usaco wormhole(看了官方视频题解)
  2. 3dmax Vray建筑可视化入门学习教程
  3. g4900 win7 核显驱动_9代酷睿跑WIN7 你要的主板来了
  4. 渗透测试web未设置http头 Strict Transport Security
  5. springboot 定时器
  6. 3d游戏编程大师技巧 源代码_C/C++编程入门基础系列:俄罗斯方块小游戏制作,直接源代码分享...
  7. 【Spring】Spring boot 可以通过集成jolokia来使用HTTP形式访问mbean
  8. oracle 039 00 039,python+robot+oracle:执行脚本时中文sql报错:UnicodeEncodeError: #039;ascii#039; codec can#...
  9. Mapx自带的工具的理解
  10. sed的选项与命令简要
  11. 前后端利用accessToken与refreshToken无感刷新
  12. 【精华贴】一键启动bat脚本——Anaconda Jupyter Notebook
  13. windows10安装双系统后,删除linux,开机进入grub怎么办
  14. 对计算机排版的分析,针对计算机排版技术问题的解决措施分析
  15. 迅捷路由虚拟服务器设置,迅捷 FWR310 无线路由器端口映射设置指南
  16. 酷比魔方 iwork10 pe重装系统 解决红屏 只需一个u盘
  17. python英语词汇读音_利用PYTHON 爬虫爬出自己的英语单词库
  18. Linux下aMule安装教程
  19. 浅析配音的类别以及不同
  20. 人机融合智能的再思考

热门文章

  1. GoComb.com搜索引擎问世:改进搜索体验
  2. MATLAB线性回归方程与非线性回归方程的相关计算
  3. 关于4442卡的读密码问题
  4. 【Python技能树共建】验证码实操2案例
  5. 通信原理ami码c语言实现,通信原理AMI码型变换实验
  6. 撬动百亿台设备,让物联网“造”起来!
  7. 华硕k5555l拆解图解_华硕k50id的拆机过程详解【图文教程】
  8. RS-485半双工延时问题
  9. 《软件管理沉思录》读书笔记
  10. 【地理信息技术】 上机02 制作上海市行政区划图