1 空间查询
1.1 ArcEngine的三种游标
更为详细的分析参见之前的文章:
1.2 基于属性的查询
1.2.1 查询方式
字符型查询使用LIKE进行模糊查询
数值型采用 > >= < <= + - * /
多条件查询AND OR IN
Name = ‘China’ AND City = '北京'
cityName IN('BeiJing', 'ShangHai','NanJing')
不支持OrderBy,要排序得先使用ITableSort
1.2.2 ITableSort
属性及方法描述:
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 有五种属性,单独或组合使用来对现有数据表或数据子集进行排序。
- Table
- Table + Cursor
- Table + QueryFilter
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接口
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 要素选择集
2 空间几何图形的集合运算
常见空间几何运算:
- 点与多边形
- 多边形对点的包含关系
- 线与多边形
- 线上坐标与多边形坐标的关系,判断线是否落入多边形内部分析
- 多边形与多边形
- 两个或多个多边形叠加形成新的多边形
对于矢量图层,集合关系有相交(Interset)和叠置求和(Union),叠置求和是将两个图层的共同区域内的要素和属性组合到第三图层