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

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

1 空间查询

1.1 ArcEngine的三种游标

image.png

更为详细的分析参见之前的文章:

1.2 基于属性的查询

1.2.1 查询方式


字符型查询使用LIKE进行模糊查询

数值型采用 > >= < <= + - * /

多条件查询AND OR IN

Name = ‘China’ AND City = '北京'

cityName IN('BeiJing', 'ShangHai','NanJing')

不支持OrderBy,要排序得先使用ITableSort


1.2.2 ITableSort

属性及方法描述:

image.png

image.png

ITableSort接口用于准备排序操作、执行排序和检索结果。


必须设置Fields属性和Table或SelectionSet属性,但其余属性是可选的,可用于进一步细化排序。

Fields属性是要排序的字段的逗号分隔列表。

Table属性指定要对其执行排序的表、对象类或要素类。selection set属性可以代替Table属性,因为设置它会自动将Table属性设置为选择集的基表。

调用sort方法时,首先对第一个字段进行排序,然后对第二个字段进行排序,依此类推。

并非所有字段都可以包含在排序中。例如,默认情况下,无法对shape 字段或Raster字段进行排序。

IFeatureClass featureClass = featureWorkspace.OpenFeatureClass("Counties");
ITable table = (ITable)featureClass;
// Create the TableSort object.
ITableSort tableSort = new TableSortClass();
tableSort.Table = table;
// If the table is the result of a join, remember to fully qualify field names.
tableSort.Fields = "State_Name, Name"; // "Name" is the field for the county name

The ITableSort 有五种属性,单独或组合使用来对现有数据表或数据子集进行排序。

IQueryFilter queryFilter = new QueryFilterClass();
queryFilter.WhereClause = "POP > 10000";
tableSort.QueryFilter = queryFilter;

SelectionSet

SelectionSet + QueryFilter

排序参数:


Ascending—Set on a specific field by supplying the field's name. When the Boolean value is set to true for a field, the values in that field are returned in ascending order (for example, 1–9 then A–Z).【按照字段名称排序,true表示递增排序】

CaseSensitive—Can only be used on string fields. This property is set to false by default. When set to true, case sensitivity is used as part of the sort algorithm if supported by the workspace.【仅适用于字符串字段,默认为fasle; 当设置为true时,如果工作空间支持的话,大小写敏感将被应用到排序算法中】

SortCharacters—Can only be used on string fields. This property indicates how many string characters are used to determine the sort order. The default for this property is null, which indicates that all available characters should be used.【仅适用于字符串字段,此属性指示用于确定排序顺序的字符串个数。此属性的默认值为空,表示应使用所有可用字符。】

// Set ascending property with field name and Boolean value.
tableSort.set_Ascending("State_Name", false);
tableSort.set_Ascending("Name", true);
tableSort.set_CaseSensitive("State_Name", true);
tableSort.set_CaseSensitive("Name", true);

执行排序:

1. 支持使用ITrackCancel接口取消排序
2. 常用形式:tableSort.Sort(null);


排序之后,可以使用Rows属性或IDs属性访问已排序的数据。Rows属性返回一个游标,该游标可用于遍历返回的每一行。下面的代码示例显示了通过Rows属性检索结果的过程:

ICursor cursor = tableSort.Rows;
// Get field indexes for efficient reuse.
int stateNameIndex = cursor.Fields.FindField("State_Name");
int countyNameIndex = cursor.Fields.FindField("Name");
int popIndex = cursor.Fields.FindField("POP");
// Walk through results and print information about each row.
IRow row = null;
while ((row = cursor.NextRow()) != null)
{
    Console.WriteLine("{0}, {1}, {2}", row.get_Value(stateNameIndex), row.get_Value
        (countyNameIndex), row.get_Value(popIndex));
}

IDs属性返回包含objectid的枚举器。单个ID值可以由它们在具有ID by index属性的枚举器中的索引返回。IDs上的索引是基于零的索引,这意味着第一个对象的索引为0。下面的代码示例显示了作为ID枚举器检索结果以及从索引检索特定结果的过程:

// Get an enumerator of ObjectIDs for the sorted rows.
IEnumIDs enumIDs = tableSort.IDs;
// Get field indexes for efficient reuse.
int stateNameIndex = table.FindField("State_Name");
int countyNameIndex = table.FindField("Name");
int popIndex = table.FindField("POP");
int id =  - 1;
IRow row = null;
while ((id = enumIDs.Next()) !=  - 1)
// -1 is returned after the last valid ID is reached.
{
    row = table.GetRow(id);
    Console.WriteLine("{0} , {1} , {2}", row.get_Value(stateNameIndex),
        row.get_Value(countyNameIndex), row.get_Value(popIndex));
}
// or,
// Get the third feature (for example, what is the third largest county?).
id = tableSort.get_IDByIndex(2); // Zero based index.
row = table.GetRow(id);
Console.WriteLine("{0}, {1}, {2}", row.get_Value(stateNameIndex), row.get_Value
    (countyNameIndex), row.get_Value(popIndex));

1.2.3 IQueryFilter接口


image.png

IQueryFilter pQueryFilter = new QueryFilterClass();
pQueryFilter.WhereClause = "name = 'beijing'";
ILayer pLayer = this.axMapControl1.get_Layer(0);
IFeatureClass pFeatureClass = (pLayer as IFeatureLayer).FeatureClass as IFeatureClass;
ITableSort pTableSort = new TableSortClass();
pTableSort.Table = pFeatureClass as ITable;
pTableSORT.Fields = "name, address";
pTableSort.QueryFilter = pQueryFilter ;
pTableSort.set_Assending("name", false);
pTableSort.set_CaseSensitive("address", true);
pTableSort.Sort(null);
ICursor pSortedCursor = pTableSort.Rows;
IRow pRow = pSortedCursor.NextRow();
while(pRow != null)
{
   // pRow.get_Value(filedIndex);
}

1.2.4 ISpatialFilter接口


参见博文:基于C#的ArcEngine二次开发38: 几何关系描述接口- ISpatialFilter 最全解析


定义了如下空间关系:相交(Inserect)、相接(Touch)、叠加(Overlap)、穿越(Crosses)、内部(Within)、包含(Contains)


1.2.5 要素选择集


image.png

image.png

2 空间几何图形的集合运算

常见空间几何运算:

  • 点与多边形
  • 多边形对点的包含关系
  • 线与多边形
  • 线上坐标与多边形坐标的关系,判断线是否落入多边形内部分析
  • 多边形与多边形
  • 两个或多个多边形叠加形成新的多边形

对于矢量图层,集合关系有相交(Interset)和叠置求和(Union),叠置求和是将两个图层的共同区域内的要素和属性组合到第三图层




相关文章
|
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中静态抽象成员的工作原理、优势及其对现有编程模式的影响,旨在帮助读者更好地理解和应用这一新特性。