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 類型(經緯度)而非 geometry | geometry |
-a, --append | 附加資料至現有表(不重建) | 重建表 |
-h, --help | 顯示說明 | - |
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:自動建立空間索引shp2sqlserver -l -s="4326" "Server=.\SQLExpress;Database=SpatialDB;Trusted_Connection=yes;" "D:\gis\taiwan_county.shp"
-l:使用 geography 類型,支援大地測量計算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\InstanceInitial 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 運算。