接續前篇
除了Polygon之外,還有MultiPolygon
接下就來實作建立 MultiPolygon 的 ShapeFile
程式碼如下:
public static string CreateMultiPolygon(string SaveFileName, int EpsgCode, double[][][] Points, string[,] MetaData)
{
FeatureSet fs = new FeatureSet(FeatureType.Polygon);
fs.Projection = ProjectionInfo.FromEpsgCode(EpsgCode);
//宣告MetaData欄位
for (int j = 0; j < MetaData.GetLength(1); j++)
{
fs.DataTable.Columns.Add(new DataColumn(MetaData[0, j], typeof(string)));
}
//產生多個Polygon
for (int i = 0; i < Points.GetLength(0); i++)
{
//設定點位
List<Coordinate> vertices = new List<Coordinate>();
for (int j = 0; j < Points[i].GetLength(0); j++)
{
vertices.Add(new Coordinate(Points[i][j][0], Points[i][j][1]));
}
Polygon geom = new Polygon(vertices);
IFeature feature = fs.AddFeature(geom);
//設定MetaData資料
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[][] polygon1 = new double[3][] { new double[2] { 100, 200 }, new double[2] { 150, 340 }, new double[2] { 330, 470 } };
double[][] polygon2 = 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.CreateMultiPolygon("test.shp", 3826, new double[][][] { polygon1, polygon2 }, new string[3, 4] { { "A", "B", "C", "D" }, { "a1", "b1", "c1", "d1" }, { "1", "2", "3", "4" } });
實作結果畫面:
MeatData畫面
希望對大家有幫助喔
更新更好的寫法,簡化輸入參數的複雜程度
回覆刪除改以Geometry的格式去描述點位資訊
public static string CreateMultiPolygon(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("MULTIPOLYGON ", "");
string[] Geometry = strGeometry.Trim().Split(new string[] { "))," }, StringSplitOptions.RemoveEmptyEntries);
string[] ColumnData = MetaData[i + 1].Split(',');
//設定點位
DotSpatial.Topology.Polygon[] geomPolygon = new DotSpatial.Topology.Polygon[Geometry.Length];
for (int j = 0; j < Geometry.Length; j++)
{
string[] strPolygon = Geometry[j].Split(',');
DotSpatial.Topology.Coordinate[] coord = new DotSpatial.Topology.Coordinate[strPolygon.Length];
for (int k = 0; k < strPolygon.Length; k++)
{
string tmp = strPolygon[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]));
}
geomPolygon[j] = new DotSpatial.Topology.Polygon(coord);
}
DotSpatial.Topology.MultiPolygon geom = new DotSpatial.Topology.MultiPolygon(geomPolygon);
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] = "MULTIPOLYGON (((120.674172658635 24.1101156784629, 120.667025886275 24.105774181235, 120.669897953672 24.1081119105116, 120.674172658635 24.1101156784629)), ((120.667760601191 24.1010319304168, 120.672436059744 24.1067760652107, 120.671100214443 24.1019002298624, 120.667760601191 24.1010319304168)))";
string[] MetaData = new string[2];
MetaData[0] = "A,B,C";
MetaData[1] = "1,11,111";
strResult = Cls_ShapeFile.CreateMultiPolygon(@"多面.shp", 4326, GeometryText, MetaData);
在MultiPolygon需特別注意的是每個Polygon點位繞的順序必須要一致
回覆刪除必須都是順時針才會正確