基于C#的ArcEngine二次开发42:空间分析接口及分析(ITopologicalOperator / IRelationalOperator / IProximityOperator)(二)

简介: 基于C#的ArcEngine二次开发42:空间分析接口及分析(ITopologicalOperator / IRelationalOperator / IProximityOperator)

3 空间拓扑运算

之前的文章:基于C#的ArcEngine二次开发22:要素拓扑检查

3.1 概述

拓扑关系是指描述地理对象的空间位置关系

要点说明:

  • 拓扑要求在同一要素集下的要素类之间的关系的集合,故要参与拓扑运算的所有要素类,必须在同一要素集内【即必须具有相同的空间参考】
  • 每个要素集只能参与一个拓扑,但是一个拓扑可以定义多个规则
  • Coverage和GeoDatabase可以建立拓扑,但是shape格式数据无法建立拓扑

3.2 在GeoDatabase中进行拓扑检查

打开FeatureDataset,将其强转为ITopologyContainer,根据拓扑的名称打开ITopology

// For example, featureDatasetName = "Landbase."
// topologyName = "Landbase_topology".
public ITopology OpenTopologyFromFeatureWorkspace(IFeatureWorkspace featureWorkspace,
    String featureDatasetName, String topologyName)
{
    // Open the feature dataset and cast it to ITopologyContainer.
    IFeatureDataset featureDataset = featureWorkspace.OpenFeatureDataset
        (featureDatasetName);
    ITopologyContainer topologyContainer = (ITopologyContainer)featureDataset;
    // Open the topology and return it.
    ITopology topology = topologyContainer.get_TopologyByName(topologyName);
    return topology;
}

ITopology.State用来检查在一个拓扑中存在的错误,他将返回一个esriTopologyState.枚举变量,如果拓扑被验证有效且找到错误,将返回esriTSAnalyzedWithErrors


可以使用ITopologyRuleContainer访问拓扑规则,由Topology类实现。ITopologyRuleContainer有几种方法可用于基于要素类的ID或要素类的特定子类型查看拓扑的规则。下面为查询一个Topology中包含的所有拓扑规则的示例:

public void DisplayTypesForEachRule(ITopology topology)
{
    // Cast the topology to ITopologyRuleContainer and get the rule enumerator.
    ITopologyRuleContainer topologyRuleContainer = (ITopologyRuleContainer)topology;
    IEnumRule enumRule = topologyRuleContainer.Rules;
    // Iterate through each rule.
    enumRule.Reset();
    IRule rule = null;
    while ((rule = enumRule.Next()) != null)
    {
        // Cast to ITopology and display the rule type.
        ITopologyRule topologyRule = (ITopologyRule)rule;
        Console.WriteLine("Rule type: {0}", topologyRule.TopologyRuleType);
    }
}

可以将Topology转换为IErrorFeatureContainer接口,该接口具有可用于返回与拓扑关联的错误功能的属性。给定拓扑错误功能,可以通过ITopologyErrorFeature  接口检查以下属性:

违反规则

错误中涉及的要素类

几何误差

错误中涉及的功能的功能ID

如果错误已被标记为异常

IErrorFeatureContainer上的每个属性都需要一个空间参考参数。对于大多数用户,这将是拓扑的空间参考。

以下代码示例显示了获取拓扑空间参考的最快方法:

public ISpatialReference GetSpatialReferenceFromTopology(ITopology topology)
{
    IGeoDataset geoDataset = (IGeoDataset)topology;
    return geoDataset.SpatialReference;
}

IErrorFeatureContainer接口上有四个属性。所述  IErrorFeatureContainer.ErrorFeatures属性返回错误的枚举设有属于随附的程度内的指定拓扑规则。以下代码示例显示如何获取有关拓扑范围的错误功能:

// Given the topology and specific topology rule, return the error features for that rule.
public void DisplayErrorFeaturesForRule(ITopology topology, ITopologyRule
    topologyRule)
{
    // Cast to required interfaces and get the spatial reference.
    IErrorFeatureContainer errorFeatureContainer = (IErrorFeatureContainer)topology;
    IGeoDataset geoDataset = (IGeoDataset)topology;
    ISpatialReference spatialReference = geoDataset.SpatialReference;
    // Get an enumerator for the error features.
    IEnumTopologyErrorFeature enumTopologyErrorFeature =
        errorFeatureContainer.get_ErrorFeatures(spatialReference, topologyRule,
        geoDataset.Extent, true, false);
    // Display the origin IDs for each of the error features.
    ITopologyErrorFeature topologyErrorFeature = null;
    while ((topologyErrorFeature = enumTopologyErrorFeature.Next()) != null)
    {
        Console.WriteLine("Origin feature OID: {0}", topologyErrorFeature.OriginOID);
    }
}

该  IErrorFeatureContainer.ErrorFeaturesByGeometryType属性返回一个特定几何类型的所有错误的功能。请参见以下代码示例,该示例将具有指定几何类型的每个错误特征的ID属性写入控制台:

public void DisplayErrorFeatureByGeometryType(ITopology topology, esriGeometryType
    geometryType)
{
    // Cast to required interfaces and get the spatial reference.
    IErrorFeatureContainer errorFeatureContainer = (IErrorFeatureContainer)topology;
    IGeoDataset geoDataset = (IGeoDataset)topology;
    ISpatialReference spatialReference = geoDataset.SpatialReference;
    // Get all errors that have the specified geometry type.
    IEnumTopologyErrorFeature enumTopologyErrorFeature =
        errorFeatureContainer.get_ErrorFeaturesByGeometryType(spatialReference,
        geometryType, false);
    // Display each error feature (if any exist) and display their properties.
    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);
    }
}

与IErrorFeatureContainer.ErrorFeatures属性类似,IErrorFeatureContainer.ErrorFeaturesByGeometryType属性的最终参数是布尔值,它指示是否应返回错误或异常。请参阅以下内容:

如果设置为false(如前面的代码示例中所示),则仅返回错误功能。

如果设置为true,则仅返回异常。

IErrorFeatureContainer.ErrorFeaturesByRuleType属性返回错误的枚举特征属于在所提供的范围指定的拓扑规则类型。请注意,这与IErrorFeatureContainer.ErrorFeatures属性有何不同,因为不需要引用ITopologyRule,而是需要引用esriTopologyRuleType枚举的值。这也与其他属性的不同之处在于,未指定任何特定规则。因此,如果多个规则具有相同的规则类型,则将返回每个规则的错误特征。请参见以下代码示例:

public void DisplayErrorFeatureByRuleType(ITopology topology, esriTopologyRuleType
    topologyRuleType)
{
    // Cast to required interfaces and get the spatial reference.
    IErrorFeatureContainer errorFeatureContainer = (IErrorFeatureContainer)topology;
    IGeoDataset geoDataset = (IGeoDataset)topology;
    ISpatialReference spatialReference = geoDataset.SpatialReference;
    // Return all errors for the supplied rule in the given extent, then retrieve the first one.
    IEnumTopologyErrorFeature enumTopologyErrorFeature =
        errorFeatureContainer.get_ErrorFeaturesByRuleType(spatialReference,
        topologyRuleType, geoDataset.Extent, true, false);
    // Get the first error feature (if any exist) and display its properties.
    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);
    }
}

3.3 ITopologicalOperator

在10.1中该接口已经更新到ITopologicalOperator5

image.png

image.png

基本代码:

//为几何图形建立缓冲区
IGeometry pGeo = pFeature.ShapeCopy;
ITopologicalOperator pTopo = pGeo as ITopologicalOperator;
pTopo.Buffer(0.5);
//判断其他图形是否在该缓冲区内
IRelationalOperator pRelate = pOtherFeature.ShapeCopy as IRelationalOperator ;
pRelate.Contains(pRelate);
相关文章
|
2月前
|
达摩院 Linux API
阿里达摩院MindOpt求解器V1.1新增C#接口
阿里达摩院MindOpt求解器发布最新版本V1.1,增加了C#相关API和文档。优化求解器产品是求解优化问题的专业计算软件,可广泛各个行业。阿里达摩院从2019年投入自研MindOpt优化求解器,截止目前经历27个版本的迭代,取得了多项国内和国际第一的成绩。就在上个月,2023年12月,在工信部产业发展促进中心等单位主办的首届能源电子产业创新大赛上,MindOpt获得电力用国产求解器第一名。本文将为C#开发者讲述如何下载安装MindOpt和C#案例源代码。
158 3
阿里达摩院MindOpt求解器V1.1新增C#接口
|
2月前
|
IDE C# 开发工具
C#系列之接口介绍
C#系列之接口介绍
|
2月前
|
编译器 C# 开发者
C# 11.0中的新特性:覆盖默认接口方法
C# 11.0进一步增强了接口的灵活性,引入了覆盖默认接口方法的能力。这一新特性允许类在实现接口时,不仅可以提供接口中未实现的方法的具体实现,还可以覆盖接口中定义的默认方法实现。本文将详细介绍C# 11.0中接口默认方法覆盖的工作原理、使用场景及其对现有代码的影响,帮助开发者更好地理解和应用这一新功能。
|
2月前
|
安全 C# 开发者
C#中的默认接口方法:接口演化的新篇章
【1月更文挑战第11天】本文探讨了C# 8.0中引入的默认接口方法,这一特性允许在接口中定义具有默认实现的方法。文章介绍了默认接口方法的语法、使用场景,以及它们如何影响接口的设计和实现,同时讨论了默认接口方法带来的好处和潜在的陷阱。
|
7天前
|
编译器 API C#
技术心得记录:深入分析C#键盘勾子(Hook)拦截器,屏蔽键盘活动的详解
技术心得记录:深入分析C#键盘勾子(Hook)拦截器,屏蔽键盘活动的详解
|
1月前
|
C# Windows
C# 串口关闭时主界面卡死原因分析
串口程序关闭导致界面卡死的原因是主线程与辅助线程间的死锁。问题出在`SerialPort.Close()`方法与`DataReceived`事件处理程序。`DataReceived`事件在`lock (stream)`块中执行,而`Close()`方法会关闭`SerialStream`并锁定自身。当辅助线程处理数据并尝试更新UI时,UI线程因调用`Close()`被阻塞,造成死锁。解决办法是让`DataReceived`事件处理程序使用`this.BeginInvoke()`异步更新界面,避免等待UI线程,从而防止死锁。
|
2月前
|
安全 算法 测试技术
C#编程实战:项目案例分析
【4月更文挑战第20天】本文以电子商务系统为例,探讨C#在实际项目中的应用。通过面向对象编程实现组件抽象和封装,确保代码的可维护性和可扩展性;利用安全性特性保护用户数据;借助数据库操作处理商品信息;通过逻辑控制和算法处理订单;调试工具加速问题解决,展现C#的优势:面向对象、数据库交互、数据安全和开发效率。C#在实际编程中展现出广泛前景。
|
2月前
|
前端开发 API C#
C# 接口
C# 接口
25 1
|
2月前
|
C# 开发者 索引
C# 11.0中的所需成员:强化接口与抽象类的约束
【1月更文挑战第24天】C# 11.0引入了所需成员(Required members)的概念,这一新特性允许在接口和抽象类中定义必须被实现的成员,包括方法、属性、索引器和事件。通过所需成员,C# 强化了对接口实现和抽象类继承的约束,提高了代码的一致性和可维护性。本文将详细探讨C# 11.0中所需成员的工作原理、使用场景及其对现有编程模式的影响。
|
2月前
|
C# 开发者 索引
C# 11.0中的静态抽象成员:接口中的新变革
【1月更文挑战第25天】C# 11.0引入了接口中的静态抽象成员,这一新特性为接口设计带来了更大的灵活性。静态抽象成员允许在接口中定义静态方法和属性,并要求实现类提供具体的实现。本文将详细探讨C# 11.0中静态抽象成员的工作原理、优势及其对现有编程模式的影响,旨在帮助读者更好地理解和应用这一新特性。