本文主要介绍空间分析的相关接口,主要包含空间查询接口、空间几何运算接口、空间拓扑运算、空间关系运算和网络分析相关接口
目录
1 空间查询
1.1 ArcEngine的三种游标
1.2 基于属性的查询
1.2.1 查询方式
1.2.2 ITableSort
1.2.3 IQueryFilter接口
1.2.4 ISpatialFilter接口
1.2.5 要素选择集
2 空间几何图形的集合运算
3 空间拓扑运算
3.1 概述
3.2 在GeoDatabase中进行拓扑检查
3.3 ITopologicalOperator
4 空间关系运算
4.1 IRelationalOperator
4.1.1 基本接口
4.1.2 Clementini 空间关系
4.2 IProximityOperator
游标 | 描述 |
Search | 需要过滤器对象IQueryFilter,过滤出要查询的要素 |
Update | 需要过滤器对象IQueryFilter,过滤出要查询的要素 可用来更新和删除一条记录,比Store和Delete方法快 |
Insert | 插入游标,比Store和CreateRow快 |
更为详细的分析参见之前的文章:
属性及方法描述:
属性/方法 | 描述 | 备注 |
Ascending | Field sort order. | 递增排序 |
CaseSensitive | Character fields case sensitive. Default: False. | 是否大小写敏感 |
Compare | Compare call back interface. Specify Null (default) for normal behavior. | 比较回调接口 |
Cursor | The cursor of the data to sort on. Ensure that sorting fields are available. Cancels SelectionSet. | 游标(禁选择集) |
Fields | Comma list of field names to sort on. | |
IDByIndex | A id by its index value. | 基于ID索引排序 |
IDs | List of sorted IDs. | 基于ID列表排序 |
QueryFilter | The query filter on table or selection set. | 对表或选择集查询过滤 |
Rows | Cursor of sorted rows. | 行排序游标 |
SelectionSet | The selection set as a source of the data to sort on. Cancels Cursor. | 选择集(禁游标) |
Sort | Sort rows. | 行排序 |
SortCharacters | Number of characters to sort on, for string fields. A null (default) sorts on the whole string. | 字母序 |
Table | The table as a source of the data to sort on. | 数据源表排序 |
ITableSort接口用于准备排序操作、执行排序和检索结果。
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;
排序参数:
// 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);
执行排序:
支持使用ITrackCancel接口取消排序
常用形式: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));
属性/方法 | 描述 | 解释 |
AddField | Appends a single field name to the list of sub-fields. | 向输出字段集中增加一个字段 |
OutputSpatialReference | The spatial reference in which to output geometry for a given field. | 获取给定字段输出几何图形的空间参考 |
SubFields | The comma delimited list of field names for the filter. | 获取或设置输出要素的字段名称清单 |
WhereClause | The where clause for the filter. | 获取或设置查询条件 |
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);
}
参见博文:基于C#的ArcEngine二次开发38: 几何关系描述接口- ISpatialFilter 最全解析
定义了如下空间关系:相交(Inserect)、相接(Touch)、叠加(Overlap)、穿越(Crosses)、内部(Within)、包含(Contains)
属性/方法 | 描述 | 备注 |
Add | Adds an object id to the selection set. | 向要素选择集中增加对象 |
AddList | Adds a list of object id's to the selection set. | 向要素选择集中增加对象列表 |
Combine | Combines this selection set with another selection set using the specified set operation. | 使用指定的集合操作将一个选择集与另一个选择集合并;注意两个选择集必须来自同一目标表或要素类 |
Count | The number of oids in the selection set. | 选择集中的ID个数 |
FullName | The full name of the selection set. | 选择集的全名 |
IDs | Enumerates the object ids in the selection set. | 选择集的对象ID枚举 |
MakePermanent | Makes the SelectionSet permanent. By default SelectionSets are deleted when released. | 永久选择集,默认释放时被删除 |
Refresh | Refreshes the state of a snapshot selection. | 刷新选择集快照 |
RemoveList | Removes a list of object id's from the selection set. | 从选择集中移除对象id列表 |
Search | Returns a cursor that can be used to retrieve the objects specified by a query over this selection set. | 在选择集中进行再选择 |
Select | Returns a new selection That contains the object ids selected by a query over this selection set. | 在当前选择集中构建新的选择集对象 |
Target | The Table or FeatureClass over which the selection set is defined. | 定义选择集的表或要素类 |
常见空间几何运算:
对于矢量图层,集合关系有相交(Interset)和叠置求和(Union),叠置求和是将两个图层的共同区域内的要素和属性组合到第三图层
之前的文章:基于C#的ArcEngine二次开发22:要素拓扑检查
拓扑关系是指描述地理对象的空间位置关系
要点说明:
打开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);
}
}
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.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);
}
}
在10.1中该接口已经更新到ITopologicalOperator5
属性/方法 | 描述 | |
Boundary | The boundary of this geometry. A polygon's boundary is a polyline. A polyline's boundary is a multipoint. A point or multipoint's boundary is an empty point or multipoint.【几何图形的边界】 Polygon --> Boundary; Polyline --> Point; Point无边界 |
|
Buffer | Constructs a polygon that is the locus of points at a distance less than or equal to a specified distance from this geometry.【为高级几何对象产生一个缓冲区;所谓高级几何对象】 | |
Clip | Constructs the intersection of this geometry and the specified envelope. | |
ClipDense | Constructs the intersection of this geometry and the specified envelope; densifies lines in output contributed by the clipping envelope. | |
ConstructUnion | Defines this geometry to be the union of the inputs. More efficient for unioning multiple geometries than calling Union repeatedly. | |
ConvexHull | Constructs the convex hull of this geometry. | |
Cut | Splits this geometry into a part left of the cutting polyline, and a part right of it. | |
Difference | Constructs the geometry containing points from this geometry but not the other geometry. | |
Intersect | Constructs the geometry that is the set-theoretic intersection of the input geometries. Use different resultDimension values to generate results of different dimensions. | |
IsKnownSimple | Indicates whether this geometry is known (or assumed) to be topologically correct. | |
IsSimple | Indicates whether this geometry is known (or assumed) to be topologically correct, after explicitly determining this if the geometry is not already known (or assumed) to be simple. | |
QueryClipped | Redefines clippedGeometry to be the intersection of this geometry and the clipping envelope. | |
QueryClippedDense | Redefines clippedGeometry to be the intersection of this geometry and the clipping envelope; densifies lines in the output contributed by the clipping envelope. | |
Simplify | Makes this geometry topologically correct. | |
SymmetricDifference | Constructs the geometry that contains points from either but not both input geometries. | |
Union | Constructs the geometry that is the set-theoretic union of the input geometries. |
基本代码:
//为几何图形建立缓冲区
IGeometry pGeo = pFeature.ShapeCopy;
ITopologicalOperator pTopo = pGeo as ITopologicalOperator;
pTopo.Buffer(0.5);
//判断其他图形是否在该缓冲区内
IRelationalOperator pRelate = pOtherFeature.ShapeCopy as IRelationalOperator ;
pRelate.Contains(pRelate);
关系运算符比较两个几何,然后返回一个布尔值,指示是否存在所需的关系。一些关系要求输入几何形状具有相同的尺寸,而其他关系则具有更灵活的尺寸约束。大多数预定义的关系运算符是互斥的Clementini运算符。
方法 | 描述 | 备注 | |
Contains | Indicates if this geometry contains the other geometry. | 该图形包含其他图形 | |
Crosses | Indicates if the two geometries intersect in a geometry of lesser dimension. | 两个图形在更低的维度相交 | |
Disjoint | Indicates if the two geometries share no points in common. Negate this result to compute the Intersect relation. | 两个几何图形是否不相交 | |
Equals | Indicates if the two geometries are of the same type and define the same set of points in the plane. | 两个几何图形是否相等 | |
Overlaps | Indicates if the intersection of the two geometries has the same dimension as one of the input geometries. | 两个几何图形是否有重叠 | |
Relation | Indicates if the defined relationship exists. | 用于监测是否存在定义的Relationship | |
Touches | Indicates if the boundaries of the geometries intersect. | 两个几何图形是否相接 | |
Within | Indicates if this geometry is contained (is within) another geometry. | 该几何图形是否包含与其他图形,也即是否被其他图形包含 |
Clementini 位于以及 Clementini 包含分别指的是“位于”和“包含”空间关系,以下文献对此类空间关系进行了定义:Clementini, Eliseo, Paolino Di Felice, and Peter van Oosterom, "A Small Set of Formal Topological Relationships Suitable for End-User Interaction," Proceedings of the Third International Symposium on Advances in Spatial Databases, 277–295, June 23–25, 1993.
更为详尽的内容参考链接:
三种 "包含" 空间关系之比较
边界定义:对于线来说,边界是两个端点;而对于面来说,边界即边。
三种 "位于" 空间关系之比较
方法 | 描述 | 备注 | |
QueryNearestPoint | Copies into 'nearest' a point on this geometry nearest to the input point. | 查询获取几何对象距离给定点最近的点的引用 | |
ReturnDistance | Returns the minimum distance between two geometries. | 返回两个几何图形之间的最小距离 | |
ReturnNearestPoint | Creates and returns a point on this geometry nearest to the input point. | 返回几何对象上给定输入点的最近距离的点 |
可与ReturnDistance一起使用的几何组合为:
更多精彩,还请关注以下公众号,目前已更名为【运筹优化与地理信息】