/// <summary> /// 为数据集创建拓扑 /// </summary> /// <param name="pFeatureDataset">数据集</param> /// <param name="topoName">拓扑名称</param> /// <param name="outError">异常</param> /// <returns>拓扑</returns> public ITopology CreateTopo(IFeatureDataset pFeatureDataset, string topoName, double mTolerence, out Exception outError) { outError = null; //topoName =topoName + "_Topology"; ITopology pTopo = null; try { ITopologyContainer2 pTopoContainer = pFeatureDataset as ITopologyContainer2; IWorkspace2 pWS2 = pFeatureDataset.Workspace as IWorkspace2; if (pWS2 == null) { return(null); } if (pWS2.get_NameExists(esriDatasetType.esriDTTopology, topoName)) { //若已经存在拓扑,则删除该拓扑 RemoveTopo(pFeatureDataset, topoName, out outError); //ITopologyContainer pTopoCon = pFeatureDataset as ITopologyContainer; //pTopo = pTopoCon.get_TopologyByName(topoName); } //else //{ //try //{ // ITopology tempTopo = pTopoContainer.get_TopologyByName(topoName); // if (tempTopo != null) // { // RemoveTopo(pFeatureDataset, topoName, out outError); // } //} //catch //{ } if (mTolerence == 0) { mTolerence = pTopoContainer.DefaultClusterTolerance; } pTopo = pTopoContainer.CreateTopology(topoName, mTolerence, -1, ""); //} } catch (Exception ex) { ////********************************************* ////guozheng 2010-12-24 平安夜 added 系统异常日志 //if (ModData.SysLog == null) ModData.SysLog = new clsWriteSystemFunctionLog(); //ModData.SysLog.Write(ex); ////********************************************** outError = ex; } return(pTopo); }
/// <summary> /// 创建拓扑集成方法 /// </summary> /// <param name="TopoName"></param> /// <param name="pFeatureDS"></param> /// <param name="FeatureClassList"></param> /// <param name="TopoDT"></param> /// <returns></returns> public ITopology2 CreateToplolgy(string TopoName, IFeatureDataset pFeatureDS, List <IFeatureClass> FeatureClassList, DataTable TopoDT) { ISchemaLock pSchemaLock = (ISchemaLock)pFeatureDS; try { pFeatureClassList = FeatureClassList; pSchemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); //创建拓扑 ITopologyContainer2 pTopoContainer = pFeatureDS as ITopologyContainer2; ITopology2 pTopology = pTopoContainer.CreateTopology(TopoName, pTopoContainer.DefaultClusterTolerance, -1, "") as ITopology2; //添加要素 foreach (IFeatureClass pFeatureClass in FeatureClassList) { pTopology.AddClass(pFeatureClass, 5, 1, 1, false); } //添加规则 for (int i = 0; i < TopoDT.Rows.Count; i++) { string s = TopoDT.Rows[i][0].ToString(); if (TopoDT.Rows[i][2].ToString() == "") { AddTopoRules(pTopology, TopoDT.Rows[i][0].ToString(), TopoDT.Rows[i][1].ToString()); } else { AddTopoRules(pTopology, TopoDT.Rows[i][0].ToString(), TopoDT.Rows[i][1].ToString(), TopoDT.Rows[i][2].ToString()); } } //验证拓扑 IGeoDataset pGeoDataset = pTopology as IGeoDataset; IEnvelope pEnvelope = pGeoDataset.Extent; VaildateTopo(pTopology, pEnvelope); return(pTopology); } catch (COMException COMEx) { MessageBox.Show(COMEx.Message); return(null); } finally { pSchemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); } }
/// <summary> /// 创建拓扑 /// </summary> /// <param name="workspaceName">被检查文件的路径名称</param> public void CreateTopology(string workspaceName) { Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory"); IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType); IWorkspace workspace = workspaceFactory.OpenFromFile(Application.StartupPath + @"\Convert\GeoDataBase.gdb", 0); IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace; IFeatureDataset featureDataset = featureWorkspace.OpenFeatureDataset("ContourData"); ITopologyContainer2 topologyContainer = (ITopologyContainer2)featureDataset; IFeatureClass CurrContour = null; try { CurrContour = featureWorkspace.OpenFeatureClass("CurrContour"); ITopology topology = topologyContainer.get_TopologyByName("eeee"); IDataset fDataset = (IDataset)topology; fDataset.Delete(); } catch (Exception) { // throw; } if (CurrContour != null) { IDataset fDataset = (IDataset)CurrContour; fDataset.Delete(); } Geoprocessor gp = new Geoprocessor(); FeatureClassToFeatureClass pFFeatureClassToFeatureClass = new FeatureClassToFeatureClass(); pFFeatureClassToFeatureClass.in_features = Application.StartupPath + "\\Convert\\ContourResult\\" + workspaceName + "\\Contour.shp"; pFFeatureClassToFeatureClass.out_path = Application.StartupPath + "\\Convert\\GeoDataBase.gdb\\ContourData"; pFFeatureClassToFeatureClass.out_name = "CurrContour"; IGeoProcessorResult geoProcessorResult = (IGeoProcessorResult)gp.Execute(pFFeatureClassToFeatureClass, null); CurrContour = featureWorkspace.OpenFeatureClass("CurrContour"); ISchemaLock schemaLock = (ISchemaLock)featureDataset; try { schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); // Create the topology. ITopology topology = topologyContainer.CreateTopology("eeee", topologyContainer.DefaultClusterTolerance, -1, ""); // Add feature classes and rules to the topology. topology.AddClass((IClass)CurrContour, 5, 1, 1, false); AddRuleToTopology(topology, esriTopologyRuleType.esriTRTLineNoIntersection, "must not intersect", CurrContour); int ErrorCount = 0; // Get an envelope with the topology's extents and validate the topology. IGeoDataset geoDataset = (IGeoDataset)topology; ESRI.ArcGIS.Geometry.IEnvelope envelope = geoDataset.Extent; ValidateTopology(topology, envelope); IGeoDataset geoDS = topology as IGeoDataset; IErrorFeatureContainer errorContainer = topology as IErrorFeatureContainer; IEnumTopologyErrorFeature eErrorFeat; eErrorFeat = errorContainer.get_ErrorFeaturesByRuleType(geoDS.SpatialReference, esriTopologyRuleType.esriTRTAreaNoGaps, null, true, false); ITopologyErrorFeature topoError; topoError = eErrorFeat.Next(); while (topoError != null) { ErrorCount++; topoError = eErrorFeat.Next(); } if (ErrorCount == 0) { MessageBox.Show("无拓扑错误!", "Sunz", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("拓扑错误" + "" + ErrorCount + "" + "个!"); } } catch (COMException comExc) { throw new Exception(String.Format( "Error creating topology: {0} Message: {1}", comExc.ErrorCode, comExc.Message), comExc); } finally { schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); } }