天天看點

在ArcEngine中建立記憶體圖層

在ArcEngine中建立記憶體圖層[轉] 2008-04-29 10:43

        arcEngine9.2中新增了一個新的特征,允許建立記憶體工作空間。當你需要一個這樣的圖層時,這個圖層的屬性資料或者幾何資料需要頻繁的更新,而又不希望是以而帶來效率上的問題,你就可以考慮使用記憶體圖層。

        記憶體圖層的建立分為五步:

      1、設定屬性字段

      2、設定空間參考和幾何字段

      3、建立記憶體工作空間

      4、在記憶體工作空間中建立特征類

      5、建立特征圖層,并将上一步建立的特征類設定為這個特征的圖層的特征類

請看以下完整的示例代碼

        /// <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;

        }

      傳回來的這個特征圖層就是我們所需要的結果,操作這個記憶體圖層方式和操作實體圖層方式完全一樣。接下要做的事情就不必我說了吧。

聯結:http://hi.baidu.com/murphy1314/blog/item/b4cdfa113710847bca80c42c.html

繼續閱讀