首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 數據庫知識 >  >> SQL Server 資料庫 >> SQL Server 2005

shp2sqlserver 工具使用指南:Shapefile 匯入 SQL Server 空間資料

     編輯:SQL Server 2005

工具簡介

shp2sqlserver 是命令列工具,將 ESRI Shapefile (.shp) 直接載入 Microsoft SQL Server 2008+ 空間資料類型(geometry/geography)。模仿 PostGIS 的 shp2pgsql,但直接寫入資料庫而非輸出 SQL。

專案頁面:https://code.google.com/p/cumberland/wiki/shp2sqlserver

基本語法

shp2sqlserver.exe [選項] "連接字串" "shapefile路徑"

常用選項

選項說明預設值
-s, --srid空間參考系統識別碼 (SRID)-1
-g, --geometry_column幾何欄位名稱geom
-t, --table_name目標資料表名稱shapefile 檔名
-k, --key_column主鍵身分識別欄位名稱gid
-i, --index建立空間索引不建立
-l, --latlong使用 geography 類型(經緯度)而非 geometrygeometry
-a, --append附加資料至現有表(不重建)重建表
-h, --help顯示說明-

實戰範例

範例 1:基本匯入(Geometry 類型,建立空間索引)

shp2sqlserver -g="SHAPE" -s="4326" -i "Data Source=.;Initial Catalog=LargeSite;Integrated Security=false;user id=sa;password=123456;" "D:\data\xyz.shp"
  • -g="SHAPE":幾何欄位命名為 SHAPE
  • -s="4326":SRID 4326 (WGS 84 經緯度)
  • -i:自動建立空間索引

範例 2:匯入為 Geography 類型(地球曲面計算)

shp2sqlserver -l -s="4326" "Server=.\SQLExpress;Database=SpatialDB;Trusted_Connection=yes;" "D:\gis\taiwan_county.shp"
  • -l:使用 geography 類型,支援大地測量計算

範例 3:附加模式匯入多個 Shapefile 至同一表

shp2sqlserver -a -t="AllRoads" "Server=ProdDB;Database=GIS;Integrated Security=SSPI;" "D:\roads\highway.shp"
shp2sqlserver -a -t="AllRoads" "Server=ProdDB;Database=GIS;Integrated Security=SSPI;" "D:\roads\local.shp"

連接字串參數說明

  • Data Source / Server:伺服器名稱或 IP,具名執行個體格式 Server\Instance
  • Initial Catalog / Database:目標資料庫
  • Integrated Security:true/SSPI 使用 Windows 驗證;false 需指定 user id/password

常見問題排查

問題解決方案
SRID 不匹配錯誤確認 Shapefile .prj 檔案定義的坐標系統,指定正確 -s 參數
幾何驗證失敗資料含無效幾何(自相交、環未閉合),先用 GIS 軟體修復
權限不足連接帳戶需具備目標資料庫 CREATE TABLE、INSERT、CREATE INDEX 權限
大檔案效能分批匯入、暫時移除索引匯入完再建立、調整交易記錄模式

進階應用

搭配 SQL Server 空間查詢功能(STDistance、STIntersects、STBuffer),可直接對匯入資料進行鄰近分析、範圍查詢、緩衝區分析等 GIS 運算。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved