搜尋此網誌

2015年11月29日 星期日

利用DotSpatial建立ShapeFile檔案 - Polygon

除了點、線之外
當然最常用的就是Polygon
接下就來實作建立 Polygon 的 ShapeFile

程式碼如下:

public static string CreatePolygon(string SaveFileName, int EpsgCode, double[][] Points, string[,] MetaData)
        {
            FeatureSet fs = new FeatureSet(FeatureType.Polygon);
            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)));
            }

            //設定點位
            List<Coordinate> vertices = new List<Coordinate>();
            for (int i = 0; i < Points.GetLength(0); i++)
            {
                vertices.Add(new Coordinate(Points[i][0], Points[i][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[1, j];
            }
            feature.DataRow.EndEdit();

            fs.SaveAs(SaveFileName, true);

            return "Success";
        }

參數意義:
SaveFileName:要建立的檔案名稱含路徑
EpsgCode:要建立檔案的EpsgCode
Points:點的陣列資料
MetaData:屬性陣列資料

呼叫方式:
//面
 Cls_ShapeFile.CreatePolygon("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畫面


希望對大家有幫助喔

1 則留言:

  1. 更新更好的寫法,簡化輸入參數的複雜程度
    改以Geometry的格式去描述點位資訊

    public static string CreatePolygon(string SaveFileName, int EpsgCode, string[] GeometryText, string[] MetaData)
    {
    try
    {
    DotSpatial.Data.FeatureSet fs = new DotSpatial.Data.FeatureSet(DotSpatial.Topology.FeatureType.Polygon);
    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("POLYGON ", "");
    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.Polygon geom = new DotSpatial.Topology.Polygon(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[2];
    GeometryText[0] = "POLYGON ((120.675575296201 24.1067760652107, 120.681052261935 24.107243611066, 120.672970397864 24.1025013602478, 120.675575296201 24.1067760652107))";
    GeometryText[1] = "POLYGON ((120.675374919406 24.1000300464411, 120.683924329331 24.0996960851159, 120.678313779068 24.0942859116472, 120.673237566924 24.0958221337433, 120.675374919406 24.1000300464411))";
    string[] MetaData = new string[3];
    MetaData[0] = "A,B,C";
    MetaData[1] = "1,11,111";
    MetaData[2] = "2,22,222";
    strResult = Cls_ShapeFile.CreatePolygon(@"面.shp", 4326, GeometryText, MetaData);

    回覆刪除