ArcEngine 拓扑检查 总结

2024-08-28 13:18
文章标签 总结 检查 拓扑 arcengine

本文主要是介绍ArcEngine 拓扑检查 总结,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

ArcEngine 拓扑检查 总结

本文大部分内容参考AO帮助文档和ArcMap帮助文档,大家实际使用中遇到的问题也可以在本帖下方留言交流,谢谢!

欢迎浏览,拒绝转载!


拓扑基础知识

  1. 拓扑的基础知识

拓扑检查常用的方法

调用GP工具(CheckGeometry)检查数据的几何

  • CheckGeometry的相关说明
    CheckGeometry工具官方说明链接地址
  • CheckGeometry质检项
    这里写图片描述
  • CheckGeometry调用示例代码
ESRI.ArcGIS.Geoprocessor.Geoprocessor geoProcessor = new ESRI.ArcGIS.Geoprocessor.Geoprocessor();
ESRI.ArcGIS.DataManagementTools.CheckGeometry checkGeometryTool = new ESRI.ArcGIS.DataManagementTools.CheckGeometry();
checkGeometryTool.in_features = otherPara[0];
checkGeometryTool.out_table = otherPara[1];
IGeoProcessorResult gpResult = geoProcessor.Execute(checkGeometryTool, null) as IGeoProcessorResult;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 建议使用范围
    有效的输入格式包括 shapefile 以及存储在个人地理数据库或文件地理数据库中的要素类。而 SDE 地理数据库则会在上传几何时自动检查每个几何的有效性;因此,检查几何和修复几何工具无需用于 SDE
    缺点:使用该GP工具会生成检查结果图层,检查的几何错误种类相对固定,对于复杂的拓扑错误不支持。
    优点:代码相对简单,适合对拓扑检查规则不是很复杂的入库级别的检查。

调用ITopologicalOperator5接口进行拓扑检查

  • ITopologicalOperator5接口相关说明
    Provides access to members for constructing new geometries based upon topological relationships between existing geometries.
    这里写图片描述
  • ITopologicalOperator5常用方法
    这里写图片描述
  • ITopologicalOperator5示例代码
IPointCollection polygonVertices = new PolygonClass();
IPointCollection lineVertices = pGeometry as IPointCollection;
polygonVertices.AddPointCollection(lineVertices);
ITopologicalOperator3 pTopology = polygonVertices as ITopologicalOperator3;
esriNonSimpleReasonEnum reason = esriNonSimpleReasonEnum.esriNonSimpleSelfIntersections;    //自相交
pTopology.IsKnownSimple_2 = false;
if (!pTopology.get_IsSimpleEx(out reason))
{   if (reason == esriNonSimpleReasonEnum.esriNonSimpleSelfIntersections){//记录拓扑错误}
}
  • 参考资料
    ArcEngine 空间分析相关代码
  • 建议使用范围
    这个接口类似ArcMap中编辑工具条中的验证功能,只能针对要素进行逐个检查,个人觉得如果图层中要素不多的话 可以使用此接口进行拓扑检查,如果图层中要素比较多,数据量比较大,建议使用GP工具或者创建拓扑进行检查,个人建议仅供参考,如果异议请留言!

通过接口创建拓扑进行拓扑检查

  • 拓扑基础知识
    一个拓扑可以添加多个拓扑图层,一个要素数据集可以拥有多个拓扑,但是一个要素类(图层)只能在一个拓扑中并且只有简单的要素类才能添加到拓扑中进行拓扑检查。
    参考链接:ArcObjects Topology官方教材
  • 常用接口
    ITopologyContainer2接口:
    这里写图片描述

  • 创建拓扑
    用到的方法:
    1.ITopologyContainer.CreateTopology
    2.ITopologyContainer2.CreateTopologyEx
    这两个方法的返回类型都是ITopology 对象
    示例代码1:

// featureDataset is an IFeatureDataset where the topology will be located.
// specifyZClusterTolerance is a System.Boolean whether a ZClusterTolerance has been specified.
// topologyName is a string with the topology's name.// Cast the feature dataset to the ITopologyContainer2 interface to create a topology.
//下面代码大概意思是如果需要z容差用CreateTopologyEx方法,不需要z容差用CreateTopology方法
ITopologyContainer2 topologyContainer = (ITopologyContainer2)featureDataset;
ITopology topology = null;
if (specifyZClusterTolerance)
{topology = topologyContainer.CreateTopologyEx(topologyName,topologyContainer.DefaultClusterTolerance,topologyContainer.DefaultZClusterTolerance,  - 1, "");
}else
{topology = topologyContainer.CreateTopology(topologyName,topologyContainer.DefaultClusterTolerance,  - 1, "");
}
  • 示例代码2:

    IFeatureDataset featureDataset = featureClass.FeatureDataset;
    ITopologyContainer2 topologyContainer = (ITopologyContainer2)featureDataset;
    if (topologyContainer == null)
    {return true;
    }
    //判断当前命名的拓扑是否存在,如果存在,删除
    bool bTopExists = (featureDataset.Workspace as IWorkspace2).get_NameExists(esriDatasetType.esriDTTopology, sTopologyName);
    if (bTopExists)
    {ITopology topologyTemp = topologyContainer.get_TopologyByName(sTopologyName);//删除拓扑IDataset pDatasetTemp = (IDataset)topologyTemp;pDatasetTemp.Delete();Marshal.ReleaseComObject(pDatasetTemp);
    }//创建一个新的拓扑
    if (dTolerance == -1)
    {dTolerance = topologyContainer.DefaultClusterTolerance;
    }
    //ITopology topology = topologyContainer.CreateTopology(sTopologyName, topologyContainer.DefaultClusterTolerance, -1, "");
    ITopology topology = topologyContainer.CreateTopology(TopologyName, dTolerance, -1, "");
      • 添加图层和拓扑规则
        用到的方法:
        1.ITopology.AddClass
        2.ITopologyRuleContainer.AddRule

        示例代码1:

      //添加拓扑图层
      topology.AddClass(featureClass, weight, xyRank, zRank, false);
      //添加拓扑规则
      public void AddRuleToTopology(ITopology topology, esriTopologyRuleType ruleType,String ruleName, IFeatureClass featureClass)
      {// 创建拓扑规则ITopologyRule topologyRule = new TopologyRuleClass();topologyRule.TopologyRuleType = ruleType;topologyRule.Name = ruleName;topologyRule.OriginClassID = featureClass.FeatureClassID;topologyRule.AllOriginSubtypes = true;//把topology对象强制转换到ITopologyRuleContainer对象,然后添加拓扑规则ITopologyRuleContainer topologyRuleContainer = (ITopologyRuleContainer)topology;if (topologyRuleContainer.get_CanAddRule(topologyRule)){topologyRuleContainer.AddRule(topologyRule);}else{throw new ArgumentException("Could not add specified rule to the topology.");}
      }

      示例代码2:The following code example demonstrates how to create a topology rule between two feature classes, specify it at the subtype level for the destination, and add it to the topology:

      public void AddRuleToTopology(ITopology topology, esriTopologyRuleType ruleType,String ruleName, IFeatureClass originClass, int originSubtype, IFeatureClassdestinationClass, int destinationSubtype)
      {// Create a topology rule.ITopologyRule topologyRule = new TopologyRuleClass();topologyRule.TopologyRuleType = ruleType;topologyRule.Name = ruleName;topologyRule.OriginClassID = originClass.FeatureClassID;topologyRule.AllOriginSubtypes = false;topologyRule.OriginSubtype = originSubtype;topologyRule.DestinationClassID = destinationClass.FeatureClassID;topologyRule.AllDestinationSubtypes = false;topologyRule.DestinationSubtype = destinationSubtype;// Cast the topology to the ITopologyRuleContainer interface and add the rule.ITopologyRuleContainer topologyRuleContainer = (ITopologyRuleContainer)topology;if (topologyRuleContainer.get_CanAddRule(topologyRule)){topologyRuleContainer.AddRule(topologyRule);}else{throw new ArgumentException("Could not add specified rule to the topology.");}
      }
      • 验证拓扑
        用到的方法:
        1.ITopology.ValidateTopology

        示例代码1:

      public void ValidateTopology(ITopology topology, IEnvelope envelope)
      {// Get the dirty area within the provided envelope.IPolygon locationPolygon = new PolygonClass();ISegmentCollection segmentCollection = (ISegmentCollection)locationPolygon;segmentCollection.SetRectangle(envelope);IPolygon polygon = topology.get_DirtyArea(locationPolygon);// If a dirty area exists, validate the topology.if (!polygon.IsEmpty){// Define the area to validate and validate the topology.IEnvelope areaToValidate = polygon.Envelope;IEnvelope areaValidated = topology.ValidateTopology(areaToValidate);}
      }

      示例代码2:完整的创建拓扑函数

      public void CreateTopology()
      {// Open the workspace and the required datasets.Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory");IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);IWorkspace workspace = workspaceFactory.OpenFromFile(@"C:\arcgis\ArcTutor\BuildingaGeodatabase\Montgomery.gdb", 0);IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace;IFeatureDataset featureDataset = featureWorkspace.OpenFeatureDataset("Landbase");IFeatureClass blocksFC = featureWorkspace.OpenFeatureClass("Blocks");IFeatureClass parcelsFC = featureWorkspace.OpenFeatureClass("Parcels");// Attempt to acquire an exclusive schema lock on the feature dataset.ISchemaLock schemaLock = (ISchemaLock)featureDataset;try{schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock);// 创建拓扑ITopologyContainer2 topologyContainer = (ITopologyContainer2)featureDataset;ITopology topology = topologyContainer.CreateTopology("Landbase_Topology",topologyContainer.DefaultClusterTolerance,  - 1, "");//添加要素类到拓扑中topology.AddClass(blocksFC, 5, 1, 1, false);topology.AddClass(parcelsFC, 5, 1, 1, false);AddRuleToTopology(topology, esriTopologyRuleType.esriTRTAreaNoOverlap, "No Block Overlap", blocksFC);AddRuleToTopology(topology,esriTopologyRuleType.esriTRTAreaCoveredByAreaClass, "ResParcels Covered by ResBlocks", parcelsFC, 1, blocksFC, 1);// 获取验证拓扑的范围并且验证拓扑IGeoDataset geoDataset = (IGeoDataset)topology;IEnvelope envelope = geoDataset.Extent;ValidateTopology(topology, envelope);}catch (COMException comExc){throw new Exception(String.Format("Error creating topology: {0} Message: {1}", comExc.ErrorCode,comExc.Message), comExc);}finally{schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock);}
      }
      • 读取拓扑结果
        通过 ITopology.State属性可以用来检查“拓扑中的错误要素是否存在”,他返回一个esriTopologyState枚举对象,如果拓扑已经被验证过并且发现了拓扑错误,那么返回 esriTSAnalyzedWithErrors 。
        这里写图片描述
        用到的接口:
        1.ITopology
        2.ITopologyRuleContainer
        3.IErrorFeatureContainer
        4.ITopologyErrorFeature
        示例代码1:
      //打开拓扑
      public ITopology OpenTopologyFromFeatureWorkspace(IFeatureWorkspace featureWorkspace,String featureDatasetName, String topologyName)
      {//打开数据集IFeatureDataset featureDataset = featureWorkspace.OpenFeatureDataset(featureDatasetName);//获取拓扑容器ITopologyContainer topologyContainer = (ITopologyContainer)featureDataset;//打开拓扑ITopology topology = topologyContainer.get_TopologyByName(topologyName);return topology;
      }//显示拓扑规则
      public void DisplayTypesForEachRule(ITopology topology)
      {ITopologyRuleContainer topologyRuleContainer = (ITopologyRuleContainer)topology;IEnumRule enumRule = topologyRuleContainer.Rules;// 遍历拓扑规则.    enumRule.Reset();    IRule rule = null;while ((rule = enumRule.Next()) != null)    {ITopologyRule topologyRule = (ITopologyRule)rule;Console.WriteLine("Rule type: {0}", topologyRule.TopologyRuleType);}
      }
      

      1)根据拓扑错误读取错误要素
      主要利用属性:IErrorFeatureContainer.ErrorFeatures

      //给定拓扑和拓扑规则,返回指定规则的错误要素
      public void DisplayErrorFeaturesForRule(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);// 显示每个错误要素的原始IDITopologyErrorFeature topologyErrorFeature = null;while ((topologyErrorFeature = enumTopologyErrorFeature.Next()) != null){Console.WriteLine("Origin feature OID: {0}", topologyErrorFeature.OriginOID);}
      }

      2)根据几何读取错误要素
      主要利用属性:IErrorFeatureContainer.ErrorFeaturesByGeometryType

      //给定拓扑和几何类型,返回指定规则的错误要素
      public void DisplayErrorFeatureByGeometryType(ITopology topology, esriGeometryTypegeometryType)
      {//获取坐标系IErrorFeatureContainer errorFeatureContainer = (IErrorFeatureContainer)topology;IGeoDataset geoDataset = (IGeoDataset)topology;ISpatialReference spatialReference = geoDataset.SpatialReference;//遍历拓扑错误IEnumTopologyErrorFeature enumTopologyErrorFeature =errorFeatureContainer.get_ErrorFeaturesByGeometryType(spatialReference,geometryType, false);//显示错误要素的信息ITopologyErrorFeature topologyErrorFeature = null;while ((topologyErrorFeature = enumTopologyErrorFeature.Next()) != null){Console.WriteLine("Error Feature Origin Class ID: {0}",topologyErrorFeature.OriginClassID);Console.WriteLine("Error Feature Origin Feature ID: {0}",topologyErrorFeature.OriginOID);Console.WriteLine("Error Feature Dest. Class ID: {0}",topologyErrorFeature.DestinationClassID);Console.WriteLine("Error Feature Dest. Feature ID: {0}",topologyErrorFeature.DestinationOID);}
      }

      3)根据拓扑规则类型读取错误要素
      主要利用属性: IErrorFeatureContainer.ErrorFeaturesByRuleType

      //给定拓扑和拓扑规则类型,返回指定规则的错误要素
      public void DisplayErrorFeatureByRuleType(ITopology topology, esriTopologyRuleTypetopologyRuleType)
      {//获取坐标系IErrorFeatureContainer errorFeatureContainer = (IErrorFeatureContainer)topology;IGeoDataset geoDataset = (IGeoDataset)topology;ISpatialReference spatialReference = geoDataset.SpatialReference;//返回指定范围内所有的错误要素,然后检索在第一个错误要素上IEnumTopologyErrorFeature enumTopologyErrorFeature =errorFeatureContainer.get_ErrorFeaturesByRuleType(spatialReference,topologyRuleType, geoDataset.Extent, true, false);// 如果存在则获取第一个错误要素并且显示它的属性ITopologyErrorFeature topologyErrorFeature = enumTopologyErrorFeature.Next();if (topologyErrorFeature != null){Console.WriteLine("Error Feature Origin Class ID: {0}",topologyErrorFeature.OriginClassID);Console.WriteLine("Error Feature Origin Feature ID: {0}",topologyErrorFeature.OriginOID);Console.WriteLine("Error Feature Dest. Class ID: {0}",topologyErrorFeature.DestinationClassID);Console.WriteLine("Error Feature Dest. Feature ID: {0}",topologyErrorFeature.DestinationOID);}
      }

      4)单独访问错误要素
      主要利用属性:IErrorFeatureContainer.ErrorFeature
      相关参数说明:
      GeometryType—Geometry type of the error feature requested
      OriginClassID—Class ID of the feature class to which the rule is applied
      OriginOID—ObjectID of the origin feature causing the error
      DestinationClassID—Class ID of the feature class in which the rule interacts
      DestinationOID—ObjectID of the destination feature causing the error

      //返回面互相压盖拓扑错误的要素
      public ITopologyErrorFeature GetErrorFeatureForNoOverlapRule(ITopology topology, IFeatureClass featureClass, int originFeatureOID, int destFeatureOID)
      {// 获取坐标系IErrorFeatureContainer errorFeatureContainer = (IErrorFeatureContainer)topology;IGeoDataset geoDataset = (IGeoDataset)topology;ISpatialReference spatialReference = geoDataset.SpatialReference;// 找到拓扑错误要素并返回ITopologyErrorFeature topologyErrorFeature = errorFeatureContainer.get_ErrorFeature(spatialReference, esriTopologyRuleType.esriTRTAreaNoOverlap,esriGeometryType.esriGeometryPolygon, featureClass.FeatureClassID, originFeatureOID, featureClass.FeatureClassID, destFeatureOID);return topologyErrorFeature;
      }

      5)根据范围读取拓扑错误要素

      //给定拓扑和查找的范围,返回所有的拓扑错误要素
      public void FindAllErrorFeatures(ITopology topology, IEnvelope searchExtent)
      {//获取坐标系IErrorFeatureContainer errorFeatureContainer = (IErrorFeatureContainer)topology;IGeoDataset geoDataset = (IGeoDataset)topology;ISpatialReference spatialReference = geoDataset.SpatialReference;ITopologyRuleContainer topologyRuleContainer = (ITopologyRuleContainer)topology;//遍历拓扑规则IEnumRule enumRule = topologyRuleContainer.Rules;enumRule.Reset();IRule rule = null;while ((rule = enumRule.Next()) != null){//获取当前拓扑规则的拓扑错误并遍历ITopologyRule topologyRule = (ITopologyRule)rule;IEnumTopologyErrorFeature enumTopologyErrorFeature = errorFeatureContainer.get_ErrorFeatures(spatialReference, topologyRule, searchExtent, true, true);ITopologyErrorFeature topologyErrorFeature = null;while ((topologyErrorFeature = enumTopologyErrorFeature.Next()) != null){//显示错误要素的信息Console.WriteLine("Class ID: {0} Object ID: {0} IsException {0}", topologyErrorFeature.OriginClassID, topologyErrorFeature.OriginOID,topologyErrorFeature.IsException);}}
      }
      • 拓扑验证监听
        这块就不赘述了,请参考AO本地文档!
        这里写图片描述

      • 建议使用范围
        对拓扑规则要求比较多的、比较细的、图层数据量较大的优先考虑创建拓扑,个人建议仅供参考,如果异议请留言!

      • 注意事项
        创建拓扑时需要考虑COM对象的释放,如果释放不干净创建的拓扑可能会删除不掉。如果觉得麻烦还可以拷贝数据再创建拓扑获取拓扑错误,个人建议仅供参考,如果异议请留言!

      通过拓扑工具箱GP调用进行拓扑检查

      • 拓扑工具箱
        拓扑常用的GP工具位置:Data Management Tools ->Topology
        这里写图片描述
        具体每个工具是干什么的,不懂的看看翻译、看看帮助,这块我就不赘述了。

      本文大部分内容参考AO帮助文档和ArcMap帮助文档,大家实际使用中遇到的问题也可以在本帖下方留言交流,谢谢!

      欢迎浏览,拒绝转载!

这篇关于ArcEngine 拓扑检查 总结的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1114872

相关文章

java常见报错及解决方案总结

《java常见报错及解决方案总结》:本文主要介绍Java编程中常见错误类型及示例,包括语法错误、空指针异常、数组下标越界、类型转换异常、文件未找到异常、除以零异常、非法线程操作异常、方法未定义异常... 目录1. 语法错误 (Syntax Errors)示例 1:解决方案:2. 空指针异常 (NullPoi

Java反转字符串的五种方法总结

《Java反转字符串的五种方法总结》:本文主要介绍五种在Java中反转字符串的方法,包括使用StringBuilder的reverse()方法、字符数组、自定义StringBuilder方法、直接... 目录前言方法一:使用StringBuilder的reverse()方法方法二:使用字符数组方法三:使用自

Python依赖库的几种离线安装方法总结

《Python依赖库的几种离线安装方法总结》:本文主要介绍如何在Python中使用pip工具进行依赖库的安装和管理,包括如何导出和导入依赖包列表、如何下载和安装单个或多个库包及其依赖,以及如何指定... 目录前言一、如何copy一个python环境二、如何下载一个包及其依赖并安装三、如何导出requirem

Rust格式化输出方式总结

《Rust格式化输出方式总结》Rust提供了强大的格式化输出功能,通过std::fmt模块和相关的宏来实现,主要的输出宏包括println!和format!,它们支持多种格式化占位符,如{}、{:?}... 目录Rust格式化输出方式基本的格式化输出格式化占位符Format 特性总结Rust格式化输出方式

Java实现检查多个时间段是否有重合

《Java实现检查多个时间段是否有重合》这篇文章主要为大家详细介绍了如何使用Java实现检查多个时间段是否有重合,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录流程概述步骤详解China编程步骤1:定义时间段类步骤2:添加时间段步骤3:检查时间段是否有重合步骤4:输出结果示例代码结语作

Python中连接不同数据库的方法总结

《Python中连接不同数据库的方法总结》在数据驱动的现代应用开发中,Python凭借其丰富的库和强大的生态系统,成为连接各种数据库的理想编程语言,下面我们就来看看如何使用Python实现连接常用的几... 目录一、连接mysql数据库二、连接PostgreSQL数据库三、连接SQLite数据库四、连接Mo

Git提交代码详细流程及问题总结

《Git提交代码详细流程及问题总结》:本文主要介绍Git的三大分区,分别是工作区、暂存区和版本库,并详细描述了提交、推送、拉取代码和合并分支的流程,文中通过代码介绍的非常详解,需要的朋友可以参考下... 目录1.git 三大分区2.Git提交、推送、拉取代码、合并分支详细流程3.问题总结4.git push

Kubernetes常用命令大全近期总结

《Kubernetes常用命令大全近期总结》Kubernetes是用于大规模部署和管理这些容器的开源软件-在希腊语中,这个词还有“舵手”或“飞行员”的意思,使用Kubernetes(有时被称为“... 目录前言Kubernetes 的工作原理为什么要使用 Kubernetes?Kubernetes常用命令总

Python中实现进度条的多种方法总结

《Python中实现进度条的多种方法总结》在Python编程中,进度条是一个非常有用的功能,它能让用户直观地了解任务的进度,提升用户体验,本文将介绍几种在Python中实现进度条的常用方法,并通过代码... 目录一、简单的打印方式二、使用tqdm库三、使用alive-progress库四、使用progres

shell脚本快速检查192.168.1网段ip是否在用的方法

《shell脚本快速检查192.168.1网段ip是否在用的方法》该Shell脚本通过并发ping命令检查192.168.1网段中哪些IP地址正在使用,脚本定义了网络段、超时时间和并行扫描数量,并使用... 目录脚本:检查 192.168.1 网段 IP 是否在用脚本说明使用方法示例输出优化建议总结检查 1