1. 程式人生 > >c#+ArcGIS Engine-向量要素賦值

c#+ArcGIS Engine-向量要素賦值

上一篇介紹了向量結構的建立方法,只是得到了空的向量結構,裡面的要素為空,現在介紹新增向量要素並賦值的方法,以建立面圖層為例。

 /// <summary>
        /// 新增要素並賦值
        /// </summary>
        /// <param name="pGrids">資料來源</param>
        /// <param name="pFeatureClass"></param>
        public void CreatGridShp(List<GridClass> pGrids, IFeatureClass pFeatureClass)
        {
            IDataset cDataset = pFeatureClass as
IDataset; IWorkspace cWorkspace = cDataset.Workspace; IWorkspaceEdit workspaceEdit = (IWorkspaceEdit)cWorkspace; workspaceEdit.StartEditing(true); workspaceEdit.StartEditOperation(); if (pGrids != null) { for (int
i = 0; i < pGrids.Count; i++) { IPointCollection pPolygonColletion = GetPointCollection(pGrids[i]); if (pPolygonColletion != null) { IGeometry pGeometry = GeometryOperate.ConstructPolygon(pPolygonColletion); if
(pGeometry != null) { try { IFeature pFeature = null; IFeatureLayer pEditLayer = new FeatureLayerClass(); pEditLayer.FeatureClass = pFeatureClass; GeometryOperate.CreateFeature(pGeometry, pEditLayer, out pFeature); if (pFeature != null ) { SetValues(pFeature, pGrids[i]); } } catch (System.Exception ex) { return; } } } } } workspaceEdit.StopEditOperation(); workspaceEdit.StopEditing(true); }

根據型別建立點集合的方法

  /// <summary>
        /// 生成點的集合
        /// </summary>
        /// <returns></returns>
        private IPointCollection GetPointCollection(GridClass pGrid)
        {
            IPointCollection pResultColletion = new PolygonClass(); ;
            if (pGrid == null) return null;
            object missing1 = Type.Missing;
            object missing2 = Type.Missing;
            IPoint pPoint1 = new PointClass();
            pPoint1.X = Math.Round(pGrid.xMin, 4);
            pPoint1.Y = Math.Round(pGrid.yMin, 4);
            pResultColletion.AddPoint(pPoint1, missing1, missing2);
            IPoint pPoint2 = new PointClass();
            pPoint2.X = Math.Round(pGrid.xMax, 4);
            pPoint2.Y = Math.Round(pGrid.yMin, 4);
            pResultColletion.AddPoint(pPoint2, missing1, missing2);
            IPoint pPoint3 = new PointClass();
            pPoint3.X = Math.Round(pGrid.xMax, 4);
            pPoint3.Y = Math.Round(pGrid.yMax, 4);
            pResultColletion.AddPoint(pPoint3, missing1, missing2);
            IPoint pPoint4 = new PointClass();
            pPoint4.X = Math.Round(pGrid.xMin, 4);
            pPoint4.Y = Math.Round(pGrid.yMax, 4);
            pResultColletion.AddPoint(pPoint4, missing1, missing2);
            return pResultColletion;
        }

要素自動賦值

 /// <summary>
        /// 欄位賦值
        /// </summary>
        /// <param name="pFeature"></param>
        /// <param name="pGrid"></param>
        private void SetValues(IFeature pFeature, GridClass pGrid)
        {
            if (pFeature != null && pGrid != null)
            {
                for (int i = 0; i < pFeature.Fields.FieldCount; i++)
                {
                    if (pFeature.Fields.get_Field(i).Name.ToUpper().Equals("GRIDNUM"))
                    {
                        pFeature.set_Value(pFeature.Fields.FindField(pFeature.Fields.get_Field(i).Name), pGrid.gridNum.ToString());
                    }
                    if (pFeature.Fields.get_Field(i).Name.ToUpper().Equals("GRIDROWNUM"))
                    {
                        pFeature.set_Value(pFeature.Fields.FindField(pFeature.Fields.get_Field(i).Name), pGrid.rowNum.ToString());
                    }
                    if (pFeature.Fields.get_Field(i).Name.ToUpper().Equals("GRIDCOLNUM"))
                    {
                        pFeature.set_Value(pFeature.Fields.FindField(pFeature.Fields.get_Field(i).Name), pGrid.colNum.ToString());
                    }
                }
            }
            pFeature.Store();
        }