提问者:小点点

从命名坐标的数据表构建二维多边形列表


我有一个数据表zct(区域/多边形坐标表),其列定义为:

private void buildZoneDataTable()
        {
            // Add columns to the DataTable.
            zct.Columns.Add("Zone Name", System.Type.GetType("System.String"));
            zct.Columns.Add("X_Coordinate", System.Type.GetType("System.Double"));
            zct.Columns.Add("Y_Coordinate", System.Type.GetType("System.Double"));
            coordDGV.DataSource = zct;
            foreach (DataGridViewColumn dgvcol in coordDGV.Columns)
            {
                dgvcol.SortMode = DataGridViewColumnSortMode.NotSortable;
            }

zct的每一行都是我稍后将呈现的多边形顶点的坐标,但现在我的目标是拥有所有命名区域/多边形及其多边形坐标的嵌套列表。

到目前为止,我已经创建了一个polyzone:

        private void BuildZoneList()
        {
            //iterate through all rows of the zct
            for (int i = 0; i < zct.Rows.Count; i++)
            {
                //if first row, or row I has a different name start new zone
                if(i == 0 || zct.Rows[i][0] != zct.Rows[i-1][0])
                {
                //create new polyzone named by the first column 
                 PolyZone pZone = new PolyZone(zct.Rows[i][0].ToString());
                }           
            }
        }

下面是我的polyzone类的一个片段:

    public class PolyZone
    {

        public string polyZoneName;
        List<Point3D> pointList = new List<Point3D>(0);
        Point3D polyCenter = new Point3D(0,0,0);

        public PolyZone(string zoneName)
        {
            polyZoneName = zoneName;
        }

        public void add(Point3D point3D)
        {
            pointList.Append(point3D);

        }

共1个答案

匿名用户

请尝试以下操作:

   class Program
    {
       static void Main(string[] args)
        {
           DataTable zct = new DataTable();
           zct.Columns.Add("Zone Name", System.Type.GetType("System.String"));
           zct.Columns.Add("X_Coordinate", System.Type.GetType("System.Double"));
           zct.Columns.Add("Y_Coordinate", System.Type.GetType("System.Double"));
           zct.Columns.Add("Z_Coordinate", System.Type.GetType("System.Double"));

           var zones = zct.AsEnumerable().GroupBy(x => x.Field<string>("Zone Name")).ToList();
           List<PolyZone> polyZones = new List<PolyZone>();
           foreach (var zone in zones)
           {
               PolyZone newZone = new PolyZone(zone.Key);
               polyZones.Add(newZone);

               foreach (DataRow row in zone)
               {
                   double x = double.Parse(row.Field<string>("X_Coordinate"));
                   double y = double.Parse(row.Field<string>("Y_Coordinate"));
                   double z = double.Parse(row.Field<string>("Z_Coordinate"));
                   newZone.add(new Point3D(x, y, z));
               }
           }


        }
 
    }
    public class PolyZone
    {

        public string polyZoneName;
        List<Point3D> pointList = new List<Point3D>(0);
        Point3D polyCenter = new Point3D(0, 0, 0);

        public PolyZone(string zoneName)
        {
            polyZoneName = zoneName;
        }

        public void add(Point3D point3D)
        {
            pointList.Add(point3D);

        }
    }