利用DotSpatial匯入ShapeFile至MS-SQL
接下來則分享如何利用DotSpatial從 MS-SQL匯出實體的ShapeFile檔案
匯出的程式碼如下:
//從MS-SQL匯出ShapeFile
public static string ExportShapefile(string SQLConnection, string TableName, string GeomCol, int EpsgCode, string ShapeFileName)
{
try
{
SqlCommand scSelect = new SqlCommand();
scSelect.CommandText = "select *," + GeomCol + ".STAsText() as GeomText from " + TableName;
System.Data.DataTable dtSelect = Cls_Datasource.getDataTable(SQLConnection, scSelect);
scSelect.Dispose();
string[] GeometryText = new string[dtSelect.Rows.Count];
string[] MetaData = new string[dtSelect.Rows.Count + 1];
int ColumnCount = dtSelect.Columns.Count - 2;
for (int j = 0; j < ColumnCount; j++)
{
MetaData[0] = MetaData[0] + dtSelect.Columns[j].ColumnName;
if (j < ColumnCount - 1) MetaData[0] = MetaData[0] + ",";
}
for (int i = 0; i < dtSelect.Rows.Count; i++)
{
GeometryText[i] = dtSelect.Rows[i]["GeomText"].ToString();
for (int j = 0; j < ColumnCount; j++)
{
MetaData[i + 1] = MetaData[i + 1] + dtSelect.Rows[i][j].ToString();
if (j < ColumnCount - 1) MetaData[i + 1] = MetaData[i + 1] + ",";
}
}
string strResult = "";
string tmpType = GeometryText[0];
if (tmpType.Substring(0, 5).Equals("POINT"))
{
strResult = Cls_ShapeFile.CreatePoint(ShapeFileName, EpsgCode, GeometryText, MetaData);
}
else if (tmpType.Substring(0, 10).Equals("MULTIPOINT"))
{
strResult = Cls_ShapeFile.CreateMultiPoint(ShapeFileName, EpsgCode, GeometryText, MetaData);
}
else if (tmpType.Substring(0, 10).Equals("LINESTRING"))
{
strResult = Cls_ShapeFile.CreateLine(ShapeFileName, EpsgCode, GeometryText, MetaData);
}
else if (tmpType.Substring(0, 15).Equals("MULTILINESTRING"))
{
strResult = Cls_ShapeFile.CreateMultiLine(ShapeFileName, EpsgCode, GeometryText, MetaData);
}
else if (tmpType.Substring(0, 7).Equals("POLYGON"))
{
strResult = Cls_ShapeFile.CreatePolygon(ShapeFileName, EpsgCode, GeometryText, MetaData);
}
else if (tmpType.Substring(0, 12).Equals("MULTIPOLYGON"))
{
strResult = Cls_ShapeFile.CreateMultiPolygon(ShapeFileName, EpsgCode, GeometryText, MetaData);
}
return strResult;
}
catch (Exception er)
{
return er.Message.ToString();
}
}
參數意義:
SQLConnection:連線MS-SQL字串
TableName:資料表名稱
GeomCol:Geometry的欄位名稱
EpsgCode:EpsgCode坐標編碼
ShapeFileName:儲存ShapeFile實體檔名(包含路徑)
呼叫方式:
ExportShapefile("Data Source=localhost;Initial Catalog=Test;Persist Security Info=True;User ID=aa;Password=123", "點", "Geom", 4326, @"點.shp");
ExportShapefile("Data Source=localhost;Initial Catalog=Test;Persist Security Info=True;User ID=aa;Password=123", "多點", "Geom", 4326, @"多點.shp");
ExportShapefile("Data Source=localhost;Initial Catalog=Test;Persist Security Info=True;User ID=aa;Password=123", "線", "Geom", 4326, @"線.shp");
ExportShapefile("Data Source=localhost;Initial Catalog=Test;Persist Security Info=True;User ID=aa;Password=123", "多線", "Geom", 4326, @"多線.shp");
ExportShapefile("Data Source=localhost;Initial Catalog=Test;Persist Security Info=True;User ID=aa;Password=123", "面", "Geom", 4326, @"面.shp");
ExportShapefile("Data Source=localhost;Initial Catalog=Test;Persist Security Info=True;User ID=aa;Password=123", "多面", "Geom", 4326, @"多面.shp");
原本在MS-SQL的屬性資料
在MS-SQL的圖資
轉出結果
基本上ShapeFile有的型態都已經包含了
希望對大家有幫助
請問你的Cls_ShapeFile這個函數要到哪裡取得?
回覆刪除另外是否有要using 那些Class?
感謝~
hi
回覆刪除這裡用到的
Cls_ShapeFile.CreatePoint
Cls_ShapeFile.CreateMultiPoint
Cls_ShapeFile.CreateLine
Cls_ShapeFile.CreateMultiLine
Cls_ShapeFile.CreatePolygon
Cls_ShapeFile.CreateMultiPolygon
其實就是對應前面幾篇分享到的文章喔
利用DotSpatial建立ShapeFile檔案 - Point
http://chihwai.blogspot.tw/2015/11/dotspatialshapefile-point.html
利用DotSpatial建立ShapeFile檔案 - MultiPoint
http://chihwai.blogspot.tw/2015/12/dotspatialshapefile-multipoint.html
利用DotSpatial建立ShapeFile檔案 - Line
http://chihwai.blogspot.tw/2015/11/dotspatialshapefile-line.html
利用DotSpatial建立ShapeFile檔案 - MultiLine
http://chihwai.blogspot.tw/2015/11/dotspatialshapefile-multiline.html
利用DotSpatial建立ShapeFile檔案 - Polygon
http://chihwai.blogspot.tw/2015/11/dotspatialshapefile-polygon.html
利用DotSpatial建立ShapeFile檔案 - MultiPolygon
http://chihwai.blogspot.tw/2015/11/dotspatialshapefile-multipolygon.html
所using的Class就是DotSpatial唷
非常感謝Will回覆解說..
回覆刪除我等有空再去你所說的網址去找。
3Q~
不客氣喔
刪除希望對你有幫助