PostgreSQL shapfile 导入导出
1.pgsql2shp & shp2pgsql 命令行工具方式导入导出 shapfile
1.1)PostgreSQL 空间数据表导出为 shafile
进入 $PG_HOME/bin 目录下执行脚本 pgsql2shp
[root@pgmaster ~]# cd /usr/pgsql-12/bin/
[root@pgmaster bin]#
查看命令参数详情 OPTIONS: 将 geom_test02_linestr.shp 导入导 testdb01 数据库的 test_schema 模式中(自动生成表 geom_test02_linestr ) An argument of `--' disables further option processing. 导入语法: [root@pgmaster bin]# shp2pgsql -s 4326 -c -W UTF-8 "/opt/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 INSERT 0 1 注意: 2. QGIS工具导入导出 shapfile 2.2)shapfile导入PostgreSQL数据库中步骤: 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/ EPSG: 4326 (WGS84) 是目前最流行的地理坐标系统。国际上会为每个坐标系统都会被分配一个EPSG代码,EPSG: 4326 就是 WGS84 的代码。GPS是基于WGS84的,所以通常我们得到的坐标数据都是 WGS84 的。 查询与北京有关的坐标系命令: SRID: 是坐标系在OGC标准中的唯一ID,和EPSG中的code是一致的。
[root@pgmaster bin]# pgsql2shp --help
pgsql2shp: illegal option -- -
RELEASE: 3.0.5 (df5f8b7)
USAGE: pgsql2shp [
pgsql2shp [
-f
-h
machine other than the default.
-p
-P
-u
-g
-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
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 表导出为 shapfile
导出 testdb01 数据库的 public 模式下的 shapfile 表
[root@pgmaster ~]# cd /usr/pgsql-12/bin/
[root@pgmaster bin]# mkdir -p "/opt/shape/geom_test01_point"
[root@pgmaster bin]# pgsql2shp -f "/opt/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/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 空间数据库中
[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:
-s [
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
(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
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
-w Output WKT instead of WKB. Note that this can result in
coordinate drift.
-W
attribute column. (default: "UTF-8")
-N
-n Only import DBF file.
-T
Note that indexes will still use the default tablespace unless the
-X flag is also used.
-X
This applies to the primary key, and the spatial index if
the -I flag is used.
-? Display this help screen.
(useful for unusual file names starting with '-')
[root@pgmaster bin]#
shp2pgsql -s 3857 -a -W GBK 文件名.shp 命名空间名.表名 | psql -h localhost -U postgres -d 数据库名-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
COMMIT
ANALYZE
[root@pgmaster bin]#
1)导入时,需要输入数据库连接密码
2)如果表已经存在、要用插入模式,则参数 -c 换成 -a;
3)如果表已存在、想要先删除已存在的表,重新创建表和导入,则 -c 换成 -d
4)如果导入时想要转坐标系,则 -s 指定新的坐标系,例如: -s 3857
我本地安装的是QGIS Desktop 3.24.0
2.1)数据库导出shapfile步骤:
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 Shapfile,默认为该格式,如果想要导出其他格式(GeoJSON、CSV等)则选择对应的格式导出
File name:指定导出文件名,一般设置为何表名相同。默认导出路径在执行文件同路径下(C:Program FilesQGIS 3.24.0bin),如果要指定其他路径导出,则需要选择导出目录+指定导出文件名(文件名指定为路径+文件名)
CRS: 指定导出坐标系,默认 EPSG:4326 - WGS84
Encoding: UTF-8 导出生成.cpg文件,用于描述.dbf文件的代码页,指明其使用的字符编码
Select fields to export and their export options:导出字段选择和其他参数配置,默认全选
其他参数默认配置
4)点击“OK”导出shapfile文件
1)菜单:Database->DB Manager
2)Providers 选择 PostGIS 连接(如果未配置则先配置)
3)展开选择对应的schema
4)点击菜单栏下方的"Import Layer/File..."按钮
5)配置导入参数:
Input:选择要导入的shapfile(xxx.shap)
Output table:
schema: 导入的目的schema(默认填充)
Table: 导入的目的表(默认填充)
Option:其他选项,根据事情情况选择是否勾选。
注意:Geometry column 如果不勾选配置,默认导入后的表的空间字段名称为 geom,如果需要修改,则勾选并指定列名
SRS & CRS: 是同一个东西,都是坐标系。例如:WGS84。
EPSG: 3857 (Pseudo-Mercator) 伪墨卡托投影,也被称为球体墨卡托,Web Mercator。它是基于墨卡托投影的,把 WGS84 坐标系投影到正方形上。
EPSG: 2436 (北京范围的投影坐标系) 北京的东(E)西(W)范围
select * from spatial_ref_sys where srtext ~* 'china';Cocos2d-x相关栏目本月热门文章



