接續前篇
程式碼如下:
public static string CreateLine(string SaveFileName, int EpsgCode, double[][] Points, string[,] MetaData)
{
FeatureSet fs = new FeatureSet(FeatureType.Line);
fs.Projection = ProjectionInfo.FromEpsgCode(EpsgCode);
//宣告MetaData欄位
for (int i = 0; i < MetaData.GetLength(1); i++)
{
fs.DataTable.Columns.Add(new DataColumn(MetaData[0, i], typeof(string)));
}
//設定點位
Coordinate[] coord = new Coordinate[Points.GetLength(0)];
for (int i = 0; i < Points.GetLength(0); i++)
{
coord[i] = new Coordinate(Points[i][0], Points[i][1]);
}
LineString ls = new LineString(coord);
//設定MetaData資料
IFeature feature = fs.AddFeature(ls);
feature.DataRow.BeginEdit();
for (int j = 0; j < MetaData.GetLength(1); j++)
{
feature.DataRow[j] = MetaData[1, j];
}
feature.DataRow.EndEdit();
fs.SaveAs(SaveFileName, true);
return "Success";
}
參數意義:
SaveFileName:要建立的檔案名稱含路徑
EpsgCode:要建立檔案的EpsgCode
Points:點的陣列資料
MetaData:屬性陣列資料
呼叫方式:
Cls_ShapeFile.CreateLine("test.shp", 3826, new double[3][] { new double[2] { 100, 200 }, new double[2] { 150, 340 }, new double[2] { 330, 470 } }, new string[2, 4] { { "A", "B", "C", "D" }, { "a1", "b1", "c1", "d1" } });
實作結果畫面:
MeatData畫面
希望對大家有幫助喔
更新更好的寫法,簡化輸入參數的複雜程度
回覆刪除改以Geometry的格式去描述點位資訊
public static string CreateLine(string SaveFileName, int EpsgCode, string[] GeometryText, string[] MetaData)
{
try
{
DotSpatial.Data.FeatureSet fs = new DotSpatial.Data.FeatureSet(DotSpatial.Topology.FeatureType.Line);
fs.Projection = DotSpatial.Projections.ProjectionInfo.FromEpsgCode(EpsgCode);
//宣告MetaData欄位
string[] strColumn = MetaData[0].Split(',');
for (int i = 0; i < strColumn.Length; i++)
{
fs.DataTable.Columns.Add(new System.Data.DataColumn(strColumn[i], typeof(string)));
}
for (int i = 0; i < GeometryText.Length; i++)
{
string strGeometry = GeometryText[i].Replace("LINESTRING ", "");
strGeometry = strGeometry.Replace("(", "");
strGeometry = strGeometry.Replace(")", "");
string[] Geometry = strGeometry.Split(',');
string[] ColumnData = MetaData[i + 1].Split(',');
//設定點位
DotSpatial.Topology.Coordinate[] coord = new DotSpatial.Topology.Coordinate[Geometry.Length];
for (int j = 0; j < Geometry.Length; j++)
{
string[] Points = Geometry[j].Trim().Split(' ');
coord[j] = new DotSpatial.Topology.Coordinate(Convert.ToDouble(Points[0]), Convert.ToDouble(Points[1]));
}
DotSpatial.Topology.LineString geom = new DotSpatial.Topology.LineString(coord);
DotSpatial.Data.IFeature feature = fs.AddFeature(geom);
//設定MetaData資料
feature.DataRow.BeginEdit();
for (int j = 0; j < strColumn.Length; j++)
{
feature.DataRow[j] = ColumnData[j];
}
feature.DataRow.EndEdit();
}
fs.SaveAs(SaveFileName, true);
return "Success";
}
catch (Exception er)
{
return er.Message.ToString();
}
}
呼叫方式:
string[] GeometryText = new string[4];
GeometryText[0] = "LINESTRING (120.621540353779 24.1332925944336, 120.616597726166 24.1298193966513, 120.621072807924 24.1248767690379, 120.626616565923 24.1288175126756)";
GeometryText[1] = "LINESTRING (120.63055730956 24.1229397933516, 120.621540353779 24.1206688563401)";
GeometryText[2] = "LINESTRING (120.684057913862 24.1055738044399, 120.678781324923 24.1017666453323, 120.687197150319 24.1000300464411)";
GeometryText[3] = "LINESTRING (120.667025886275 24.105774181235, 120.667760601191 24.1010319304168, 120.671100214443 24.1019002298624, 120.672436059744 24.1067760652107)";
string[] MetaData = new string[5];
MetaData[0] = "A,B,C";
MetaData[1] = "1,11,111";
MetaData[2] = "2,22,222";
MetaData[3] = "3,33,333";
MetaData[4] = "4,44,444";
strResult = Cls_ShapeFile.CreateLine(@"線.shp", 4326, GeometryText, MetaData);