shape数据导入postgis的方法

Shapfile文件为ESRI公司的文件存储格式,并且得到了业界广泛的支持。Shapfile格式是一种简单的,用非拓朴关系的形式存储几何位置和地理特征的属性信息的格式。虽然GeoServer采用Shapfile文件可以快速的创建网上地图服务,但它的缺点确很明显:

  1、Shapefile只支持一个图层,在实际中没有意义。

  2、直接保用SHP文件不安全,Shapfile文件很容易被病毒或其他原因误删除。

  3、GeoServer中用Shapfile文件作数据源的效率是很低的。

  4、Shapfile中的汉字GeoServer不能解析,会出现乱码。

  5、数据库可以方便的对地理信息进行查询。

 用PostGIS管理空间数据

         PostGIS支持GIST空间索引(附录1)、规范窗体,能很大的提高处理效率。

         OGC格式只提供二维的几何体,且相关联的SRID从未深入的用于输入输出请求,PostGIS支持OpenGIS组织”Simple Features for SQL”规范指定的所有GIS对象和函数,并进行了扩展,格式是EWKB、EWKT,其中增加了对3DZ,3DM和4D 坐标系的支持(当然三维、四维数据的OGC标准还未完全制定),深入引进了SRID信息。

  空间数据表结构:PostGIS中存在两个必需的元数据表格, SPATIAL_REF_SYS(空间参考表格) 和 GEOMETRY_COLUMNS(几何体属性列),两个表用于存储空间数据库使用的坐标系统数字ID和文本描述。

  PostGIS的shp2pgsql命令可以将Shapfile直接导入到数据库中也可以导出为SQL文件,推荐先导出为SQL文件再将此文件在SQL运行窗口中执行可将数据导入数据库。代码如下:


Shapfile到SQL语句:

  shp2pgsql    路径/shp数据文件名 新建的数据表名 > 路径/SQL文件名.sql

Shapfile直接入库:

  shp2pgsql -c 路径/shp数据文件名 新建的数据表名 数据库名|psql -d 数据库名

举例说明:

  如将一Shapfile文件“c:/road.shp”导入到数据表“road”中,数据库为“sjzmap”。

  1、运行“命令提示符”。

  2、切换至PostgreSQL数据库安装目录中的bin目录下。

  3、执行此目录下的shp2pgsql命令:“shp2pgsql c:/road.shp road > c:/road.sql”。

  4、如将此文件直接导入数据库(不推荐):“shp2pgsql -c c:/road.shp road sjzmap | psql -d sjzmap”。

      5、使用pgAdmin3 选择数据库,再导入表。

注:

-d

Drops the database table before creating a new table with the data in the Shape file.

-a

Appends data from the Shape file into the database table. Note that to use this option to load multiple files, the files must have the same attributes and same data types.

-c

Creates a new table and populates it from the Shape file. This is the default mode.

-p

Only produces the table creation SQL code, without adding any actual data. This can be used if you need to completely separate the table creation and data loading steps.

-D

Use the PostgreSQL “dump” format for the output data. This can be combined with -a, -c and -d. It is much faster to load than the default “insert” SQL format. Use this for very large data sets.

-s <SRID>

Creates and populates the geometry tables with the specified SRID.

-k

Keep identifiers’ case (column, schema and attributes). Note that attributes in Shapefile are all UPPERCASE.

-i

Coerce all integers to standard 32-bit integers, do not create 64-bit bigints, even if the DBF header signature appears to warrant it.

-I

Create a GiST index on the geometry column.

-w

Output WKT format, for use with older (0.x) versions of PostGIS. Note that this will introduce coordinate drifts and will drop M values from shapefiles.

-W <encoding>

Specify encoding of the input data (dbf file). When used, all attributes of the dbf are converted from the specified encoding to UTF8. The resulting SQL output will contain a SET CLIENT_ENCODING to UTF8 command, so that the backend will be able to reconvert from UTF8 to whatever encoding the database is configured to use internally.

转载自:https://blog.csdn.net/GreenOfBamboo/article/details/3976316

You may also like...