ArcEngine 拓扑检查 总结


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

欢迎浏览,拒绝转载!


拓扑基础知识

  1. 拓扑的基础知识

拓扑检查常用的方法

调用GP工具(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;
  • 建议使用范围
    有效的输入格式包括 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, IFeatureClass
    destinationClass, 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);
    // 显示每个错误要素的原始ID
    ITopologyErrorFeature topologyErrorFeature = null;
    while ((topologyErrorFeature = enumTopologyErrorFeature.Next()) != null)
    {
        Console.WriteLine("Origin feature OID: {0}", topologyErrorFeature.OriginOID);
    }
}

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

//给定拓扑和几何类型,返回指定规则的错误要素
public void DisplayErrorFeatureByGeometryType(ITopology topology, esriGeometryType
    geometryType)
{
    //获取坐标系
    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, esriTopologyRuleType
    topologyRuleType)
{
    //获取坐标系
    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帮助文档,大家实际使用中遇到的问题也可以在本帖下方留言交流,谢谢!

欢迎浏览,拒绝转载!

转载自:https://blog.csdn.net/yh0503/article/details/52432193

You may also like...