ArcGIS Engine基础(18)之拓扑检查(根据数据集创建拓扑、添加拓扑规则、添加参与拓扑图层、验证拓扑、输出错误信息等)

  /// <summary>
        /// 建立拓扑
        /// </summary>
        /// <param name="featureDataset">拓扑分析的数据集</param>
        /// <param name="TopologyName">拓扑名</param>
        /// <param name="insteadIfExit">取代原拓扑</param>
        /// <returns></returns>
        public ITopology CreateTopology(IFeatureDataset featureDataset, string TopologyName, bool insteadIfExit)
        {
            ITopologyContainer2 topologyContainer = (ITopologyContainer2)featureDataset;
            ITopology topology = null;

            try
            {
                topology = topologyContainer.get_TopologyByName(TopologyName);
            }
            catch
            {
            }

            if (topology != null && insteadIfExit == true)
                (topology as IDataset).Delete();

            try
            {
                topology = topologyContainer.CreateTopology(TopologyName,
                    topologyContainer.DefaultClusterTolerance, -1, "");
            }
            catch (Exception ex)
            {
                throw new ApplicationException("创建拓扑失败:\r\n");
            }

            return topology;
        }

        /// <summary>
        /// 向拓扑添加要素类
        /// </summary>
        /// <param name="pTopology"></param>
        /// <param name="pFeatureClass"></param>
        public void AddClass(ITopology pTopology, IFeatureClass pFeatureClass)
        {
            try
            {
                pTopology.AddClass(pFeatureClass, 1, 1, 1, false);
            }
            catch
            { throw new ApplicationException("拓扑分析,添加数据出错。"); }
        }


        /// <summary>
        /// 添加拓扑规则
        /// </summary>
        /// <param name="topology">拓扑</param>
        /// <param name="ruleType">拓扑规则类型</param>
        /// <param name="ruleName">规则名</param>
        /// <param name="originClass">源图层</param>
        /// <param name="destinationClass">目标图层</param>
        public void AddRuleToTopology(ITopology topology, esriTopologyRuleType ruleType,
            string ruleName, IFeatureClass originClass, IFeatureClass destinationClass)
        {
            ITopologyRule topologyRule = new TopologyRuleClass();
            topologyRule.TopologyRuleType = ruleType;
            topologyRule.Name = ruleName;
            topologyRule.OriginClassID = originClass.FeatureClassID;
            topologyRule.AllOriginSubtypes = true;

            if (destinationClass != null)
            {
                topologyRule.DestinationClassID = destinationClass.FeatureClassID;
                topologyRule.AllDestinationSubtypes = true;
            }

            ITopologyRuleContainer topologyRuleContainer = (ITopologyRuleContainer)topology;
            if (topologyRuleContainer.get_CanAddRule(topologyRule))
            {
                topologyRuleContainer.AddRule(topologyRule);
            }
            else
            {
                throw new ArgumentException(string.Format("不能添加{0}规则到拓扑里面,请检查此规则是否正确。",ruleName));
            }
        }


        /// <summary>
        /// 验证拓扑并输出错误信息
        /// </summary>
        /// <param name="topology"></param>
        /// <param name="featureDataset"></param>
        /// <returns></returns>
        public Dictionary<string,int> ValidateTopologyReport(ITopology topology, IFeatureDataset featureDataset)
        {
            Dictionary<string, int> errorInfo = new Dictionary<string, int>();
            IFeatureClassContainer featureClassContainer = featureDataset as IFeatureClassContainer;
            IGeoDataset geoDataset = topology as IGeoDataset;
            //验证拓扑
            ValidateTopology(topology, geoDataset.Extent);
            //获取错误信息
            List<ITopologyRule> topologyRuleList = GetAllesriTopologyRules(topology);
            string layerName;
            //string errorids ;
            int countTopo = 0;
            List<int> ids ;
            IFeatureClass originFeatureClass = null;
            foreach (ITopologyRule topologyRule in topologyRuleList)
            {
                layerName = "";
                countTopo = 0;
                ids = new List<int>();
                originFeatureClass = featureClassContainer.get_ClassByID(topologyRule.OriginClassID);
                layerName = (originFeatureClass as IDataset).Name;

                IEnumTopologyErrorFeature enumTopologyErrorFeature = GetErrorFeaturesForRule(topology, topologyRule);
                ITopologyErrorFeature topologyErrorFeature = enumTopologyErrorFeature.Next();

                while (topologyErrorFeature != null)
                {
                    if (ids.Contains(topologyErrorFeature.OriginOID)) 
                    {
                        topologyErrorFeature = enumTopologyErrorFeature.Next(); 
                        continue; 
                    }
                    countTopo++;
                    ids.Add(topologyErrorFeature.OriginOID);
                    topologyErrorFeature = enumTopologyErrorFeature.Next();
                }
                if (countTopo > 0) errorInfo.Add(layerName +","+ topologyRule.Name, countTopo);
                //ComObject.Release<IFeatureClass>(ref originFeatureClass);
            }

            return errorInfo;
        }

        /// <summary>
        /// 验证拓扑
        /// </summary>
        /// <param name="topology"></param>
        /// <param name="envelope"></param>
        public void ValidateTopology(ITopology topology, IEnvelope envelope)
        {
            IPolygon locationPolygon = new PolygonClass();
            ISegmentCollection segmentCollection = (ISegmentCollection)locationPolygon;
            segmentCollection.SetRectangle(envelope);
            IPolygon polygon = topology.get_DirtyArea(locationPolygon);

            if (!polygon.IsEmpty)
            {
                IEnvelope areaToValidate = polygon.Envelope;
                IEnvelope areaValidated = topology.ValidateTopology(areaToValidate);
            }
        }

        /// <summary>
        /// 获取所有拓扑规则
        /// </summary>
        /// <param name="topology"></param>
        /// <returns></returns>
        public List<ITopologyRule> GetAllesriTopologyRules(ITopology topology)
        {
            List<ITopologyRule> allEsriTopologyRulesList = new List<ITopologyRule>();
            ITopologyRuleContainer topologyRuleContainer = topology as ITopologyRuleContainer;
            IEnumRule enumRule = topologyRuleContainer.Rules;

            enumRule.Reset();
            IRule rule = null;
            while ((rule = enumRule.Next()) != null)
            {
                ITopologyRule topologyRule = (ITopologyRule)rule;
                allEsriTopologyRulesList.Add(topologyRule);
            }

            return allEsriTopologyRulesList;
        }

        /// <summary>
        /// 从拓扑规则获取拓扑错误要素
        /// </summary>
        /// <param name="topology"></param>
        /// <param name="topologyRule"></param>
        /// <returns></returns>
        public IEnumTopologyErrorFeature GetErrorFeaturesForRule(ITopology topology, ITopologyRule topologyRule)
        {
            IErrorFeatureContainer errorFeatureContainer = (IErrorFeatureContainer)topology;
            IGeoDataset geoDataset = (IGeoDataset)topology;
            ISpatialReference spatialReference = geoDataset.SpatialReference;

            IEnumTopologyErrorFeature enumTopologyErrorFeature =
                errorFeatureContainer.get_ErrorFeatures(spatialReference, topologyRule,
                geoDataset.Extent, true, false);

            return enumTopologyErrorFeature;
        }

        /// <summary>
        /// 获取参与拓扑分析的图层
        /// </summary>
        /// <param name="topology">拓扑</param>
        /// <param name="layerName">图层名</param>
        /// <returns></returns>
        public ILayer GetParticipateLayer(ITopology topology, string layerName)
        {
            ILayer layer = null;
            IFeatureClassContainer featureContainer = topology.FeatureDataset as IFeatureClassContainer;
            List<ITopologyRule> rules = GetAllesriTopologyRules(topology);
            List<int> participateClassId = new List<int>();
            //获取参加拓扑分析的要素类
            List<IFeatureClass> participateClasses = new List<IFeatureClass>();
            foreach (ITopologyRule rule in rules)
            {
                if (!participateClassId.Contains(rule.OriginClassID))
                {
                    participateClassId.Add(rule.OriginClassID);
                    participateClasses.Add(featureContainer.get_ClassByID(rule.OriginClassID));
                }
                if (!participateClassId.Contains(rule.DestinationClassID))
                {
                    participateClassId.Add(rule.DestinationClassID);
                    participateClasses.Add(featureContainer.get_ClassByID(rule.DestinationClassID));
                }
            }
            IGroupLayer groupLayer = new GroupLayerClass();
            //添加拓扑图层
            ITopologyLayer topologyLayer = new TopologyLayerClass();
            topologyLayer.Topology = topology;
            groupLayer.Add(topologyLayer as ILayer);

            if (participateClasses.Count > 0)
            {
                // 按面、MultiPatch、线、点的顺序进行排序
                esriGeometryType[] sortGeometryTypes = new esriGeometryType[] {
                esriGeometryType.esriGeometryPolygon, esriGeometryType.esriGeometryMultiPatch,
                esriGeometryType.esriGeometryPolyline,esriGeometryType.esriGeometryPoint };
                List<IFeatureClass> sortedFeatureClass = new List<IFeatureClass>();
                foreach (esriGeometryType sortGeometryType in sortGeometryTypes)
                {
                    IEnumerable<IFeatureClass> sortClass = null;

                    sortClass = from cls in participateClasses
                                where cls.ShapeType == sortGeometryType
                                select cls;
                    sortedFeatureClass.AddRange(sortClass);
                }
                foreach (IFeatureClass fc in sortedFeatureClass)
                {
                    IFeatureLayer featureLayer = new FeatureLayerClass();
                    featureLayer.FeatureClass = fc;
                    groupLayer.Add(featureLayer as ILayer);
                }
                layer = groupLayer;
                layer.Name = layerName;
            }

            return layer;
        }

 

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
当使用ArcGIS创建拓扑时,可能会出现该错误提示"所选要素数据集不包含"。这是由于所选择的要素数据集中缺少必需的要素类或要素类的几何网络。要解决此问题,可以按照以下步骤进行操作: 1. 首先,确保当前的ArcGIS版本支持拓扑功能,并且安装了必要的扩展,如ArcGIS Topology扩展。 2. 检查要素数据集的内容。选择要素数据集,查看其中的要素类和几何网络是否正常存在。如果缺少某个要素类或几何网络,请添加或修复它们。 3. 如果要素类或几何网络已存在于其他要素数据集中,可以尝试使用导入功能将它们导入到当前要素数据集中。 4. 在选择创建拓扑的要素数据集时,确保选择的是正确的要素数据集。可能需要浏览其它要素数据集目录,直到找到包含所需要素类的正确数据集。 5. 进行拓扑创建之前,可以先尝试在ArcGIS Catalog中对要素数据集进行修复操作。这可以帮助解决一些潜在的问题,如损坏的拓扑规则。 6. 如果以上步骤都没有解决问题,可以尝试重新创建要素数据集,或者尝试在新的ArcGIS项目中创建拓扑。 总之,当ArcGIS提示"所选要素数据集不包含"时,我们应该确保所选的要素数据集包含正确的要素类和几何网络,并在创建拓扑之前进行必要的修复操作。如果问题仍然存在,可以尝试重新创建要素数据集或新的ArcGIS项目。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xizhjxust_GIS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值