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.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章