基于ArcEngine的网格计算相关(1)

(1)创建内存图层

/// <summary>
        /// 创建内存图层,使用该图层创建网格
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            ILayer pLayer = DataManage.GetLayer(mapControl, "计算单元网格");  
            if (pLayer != null && pLayer is IFeatureLayer)
            {
                mapControl.Map.DeleteLayer(pLayer);
            }
            ISpatialReference pSpatRef = mapControl.SpatialReference;
            IFields pFields = new FieldsClass();
            IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;
            IField pFeild = new FieldClass();
            IFieldEdit pFeildEdit = pFeild as IFieldEdit;
            pFeildEdit.Length_2 = 8;
            pFeildEdit.Name_2 = "OBJECTID";
            pFeildEdit.Type_2 = esriFieldType.esriFieldTypeOID;
            pFieldsEdit.AddField(pFeild);
            pFeild = new FieldClass();
            pFeildEdit = pFeild as IFieldEdit;
            pFeildEdit.Length_2 = 8;
            pFeildEdit.Name_2 = "pvalue";
            pFeildEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
            pFieldsEdit.AddField(pFeild);
            IFeatureLayer pFeatureLayer = Utility.CreateFeatureLayerInmemeory("计算单元网格", "计算单元网格", pSpatRef, esriGeometryType.esriGeometryPolygon, pFields);

           mapControl.AddLayer(pFeatureLayer, 0);

}

public class Utility
    {
        /// <summary>
        /// 在内存中创建图层
        /// </summary>
        /// <param name="DataSetName">数据集名称</param>
        /// <param name="AliaseName">别名</param>
        /// <param name="SpatialRef">空间参考</param>
        /// <param name="GeometryType">几何类型</param>
        /// <param name="PropertyFields">属性字段集合</param>
        /// <returns>IfeatureLayer</returns>
        public static IFeatureLayer CreateFeatureLayerInmemeory(string DataSetName, string AliaseName, ISpatialReference SpatialRef, esriGeometryType GeometryType, IFields PropertyFields)
        {
            IWorkspaceFactory workspaceFactory = new InMemoryWorkspaceFactoryClass();
            ESRI.ArcGIS.Geodatabase.IWorkspaceName workspaceName = workspaceFactory.Create("", "MyWorkspace", null, 0);
            ESRI.ArcGIS.esriSystem.IName name = (IName)workspaceName;
            ESRI.ArcGIS.Geodatabase.IWorkspace inmemWor = (IWorkspace)name.Open();

            IField oField = new FieldClass();
            IFields oFields = new FieldsClass();
            IFieldsEdit oFieldsEdit = null;
            IFieldEdit oFieldEdit = null;
            IFeatureClass oFeatureClass = null;
            IFeatureLayer oFeatureLayer = null;

            try
            {
                oFieldsEdit = oFields as IFieldsEdit;
                oFieldEdit = oField as IFieldEdit;

                for (int i = 0; i < PropertyFields.FieldCount; i++)
                {
                    oFieldsEdit.AddField(PropertyFields.get_Field(i));
                }

                IGeometryDef geometryDef = new GeometryDefClass();
                IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;
                geometryDefEdit.AvgNumPoints_2 = 5;
                geometryDefEdit.GeometryType_2 = GeometryType;
                geometryDefEdit.GridCount_2 = 1;
                geometryDefEdit.HasM_2 = false;
                geometryDefEdit.HasZ_2 = false;
                geometryDefEdit.SpatialReference_2 = SpatialRef;

                oFieldEdit.Name_2 = "SHAPE";
                oFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
                oFieldEdit.GeometryDef_2 = geometryDef;
                oFieldEdit.IsNullable_2 = true;
                oFieldEdit.Required_2 = true;
                oFieldsEdit.AddField(oField);

                oFeatureClass = (inmemWor as IFeatureWorkspace).CreateFeatureClass(DataSetName, oFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", "");
                (oFeatureClass as IDataset).BrowseName = DataSetName;

                oFeatureLayer = new FeatureLayerClass();
                oFeatureLayer.Name = AliaseName;
                oFeatureLayer.FeatureClass = oFeatureClass;
            }
            catch
            {
            }
            finally
            {
                try
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(oField);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(oFields);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(oFieldsEdit);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(oFieldEdit);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(name);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(workspaceFactory);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(workspaceName);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(inmemWor);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(oFeatureClass);
                }
                catch { }

                GC.Collect();
            }
            return oFeatureLayer;
        }

    }

 

 

(2)导出内存图层到MDB

private void button4_Click(object sender, EventArgs e)
        {
            IQueryFilter pFilter = new QueryFilterClass();
            pFilter.WhereClause = "";
            IWorkspaceFactory pWSFactory = new AccessWorkspaceFactoryClass();
            IWorkspace pWorkspace = pWSFactory.OpenFromFile(@"F:/HGN/NewCode/LandGis/LGIS/LGIS/bin/Debug/data/下瑭镇/djdata/test.mdb", 0);
           
           
            IFeatureWorkspace pFWS = pWorkspace as IFeatureWorkspace;
            IFeatureClass pFeatureClass = pFWS.OpenFeatureClass("计算单元网格");
            if (pFeatureClass != null)
            {
                IFeatureWorkspaceManage pFWSM = (IFeatureWorkspaceManage)pFWS;
                IWorkspaceName workspaceName = new WorkspaceNameClass();
                workspaceName.PathName = pWorkspace.PathName;
                workspaceName.WorkspaceFactoryProgID = "esriDataSourcesGDB.AccessWorkspaceFactory";
                IFeatureClassName featureClassName = new FeatureClassNameClass();
                IDatasetName datasetName = (IDatasetName)featureClassName;
                datasetName.Name = pFeatureClass.AliasName;
                datasetName.WorkspaceName = workspaceName;
                pFWSM.DeleteByName((datasetName));
            }

            ILayer pLayer = DataManage.GetLayer((IMapControl3)axMapControl.Object, "计算单元网格");
            IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;
            pFeatureClass = pFeatureLayer.FeatureClass;
            ConvertFeatureClass(pFeatureClass, pWorkspace, "计算单元网格", pFilter);
            MessageBox.Show("OK");
           
        }
        public void ConvertFeatureClass(IFeatureClass inFeatureClass, IWorkspace outWorkspace, string LayerName, IQueryFilter pFilter)
        {
            //Get input FeatureClassName and Workspace
            try
            {
                IDataset dataset = (IDataset)inFeatureClass;
                IFeatureClassName inFeatureClassName = (IFeatureClassName)dataset.FullName;
                IWorkspace inWorkspace = dataset.Workspace;
                //Set output Workspace Name
                dataset = (IDataset)outWorkspace;
                IWorkspaceName outWorkspaceName = (IWorkspaceName)dataset.FullName;
                //Set output FeatureClassName
                IFeatureClassName outFeatureClassName = new FeatureClassNameClass();
                IDatasetName datasetName = (IDatasetName)outFeatureClassName;
                datasetName.Name = LayerName;
                datasetName.WorkspaceName = outWorkspaceName;
                //Get fields for input feature class and run them through field checker
                IFieldChecker fieldChecker = new FieldCheckerClass();
                IFields inFields = inFeatureClass.Fields;
                IFields outFields;

                IEnumFieldError enumFieldError;
                fieldChecker.InputWorkspace = inWorkspace;
                fieldChecker.ValidateWorkspace = outWorkspace;
                fieldChecker.Validate(inFields, out enumFieldError, out outFields);
                //Convert the data
                IFeatureDataConverter featureDataConverter = new FeatureDataConverterClass();
                featureDataConverter.ConvertFeatureClass(inFeatureClassName, pFilter, null, outFeatureClassName, null, outFields, "", 100, 0);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(fieldChecker);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(featureDataConverter);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(outWorkspace);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(inFeatureClass);
            }

        }

 

引用参考:

http://www.cnblogs.com/gispeng/archive/2008/05/27/1208249.html

 

 

 

你可能感兴趣的:(exception,String,null,button,dataset,网格)