接續前篇
除了單一個線之外
還有MultiLine的情況
程式碼如下:
public static string CreateMultiLine(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)));
}
//產生多個Polygon
for (int i = 0; i < Points.GetLength(0); i++)
{
//設定點位
Coordinate[] coord = new Coordinate[Points[i].GetLength(0)];
for (int j = 0; j < Points[i].GetLength(0); j++)
{
coord[j] = new Coordinate(Points[i][j][0], Points[i][j][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[i + 1, j];
}
feature.DataRow.EndEdit();
}
fs.SaveAs(SaveFileName, true);
return "Success";
}
參數意義:
SaveFileName:要建立的檔案名稱含路徑
EpsgCode:要建立檔案的EpsgCode
Points:點的陣列資料
MetaData:屬性陣列資料
呼叫方式:
//多個線
double[][] line1 = new double[3][] { new double[2] { 100, 200 }, new double[2] { 150, 340 }, new double[2] { 330, 470 } };
double[][] line2 = new double[4][] { new double[2] { 10, 20 }, new double[2] { 15, 34 }, new double[2] { 33, 47 }, new double[2] { 0, 5 } };
Cls_ShapeFile.CreateMultiLine("test.shp", 3826, new double[][][] { line1, line2 }, new string[3, 4] { { "A", "B", "C", "D" }, { "a1", "b1", "c1", "d1" }, { "1", "2", "3", "4" } });
實作結果畫面:
MeatData畫面
希望對大家有幫助喔
更新更好的寫法,簡化輸入參數的複雜程度
回覆刪除改以Geometry的格式去描述點位資訊
public static string CreateMultiLine(string SaveFileName, int EpsgCode, string[] GeometryText, string[] MetaData)
{
try
{
DotSpatial.Data.FeatureSet fs = new DotSpatial.Data.FeatureSet();
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("MULTILINESTRING ", "");
string[] Geometry = strGeometry.Trim().Split(new string[] { ")," }, StringSplitOptions.RemoveEmptyEntries);
string[] ColumnData = MetaData[i + 1].Split(',');
//設定點位
DotSpatial.Topology.LineString[] ls = new DotSpatial.Topology.LineString[Geometry.Length];
for (int j = 0; j < Geometry.Length; j++)
{
string[] strLine = Geometry[j].Split(',');
DotSpatial.Topology.Coordinate[] coord = new DotSpatial.Topology.Coordinate[strLine.Length];
for (int k = 0; k < strLine.Length; k++)
{
string tmp = strLine[k].Trim();
tmp = tmp.Replace("(", "");
tmp = tmp.Replace(")", "");
string[] Points = tmp.Split(' ');
coord[k] = new DotSpatial.Topology.Coordinate(Convert.ToDouble(Points[0]), Convert.ToDouble(Points[1]));
}
ls[j] = new DotSpatial.Topology.LineString(coord);
}
DotSpatial.Topology.MultiLineString geom = new DotSpatial.Topology.MultiLineString(ls);
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[1];
GeometryText[0] = "MULTILINESTRING ((120.674172658635 24.1101156784629, 120.669897953672 24.1081119105116, 120.667025886275 24.105774181235), (120.667760601191 24.1010319304168, 120.671100214443 24.1019002298624, 120.672436059744 24.1067760652107))";
string[] MetaData = new string[2];
MetaData[0] = "A,B,C";
MetaData[1] = "1,11,111";
strResult = Cls_ShapeFile.CreateMultiLine(@"多線.shp", 4326, GeometryText, MetaData);