一、介绍
当需要从一个要素类中频繁的进行空间查询时,效率可能很低。如果预先为要素类建立了空间索引,将能极大的提高多次查询的效率。
本文主要介绍为要素类创建空间索引的方法。
二、示例
首先为要素类创建空间索引,然后从要素类中,获取与几何体列表中要素相交的所有的要素的OBJECTID值列表。代码如下
/// <summary>
/// 从要素类中,获取与几何体列表中要素相交的所有的要素的OBJECTID值列表
/// </summary>
/// <param name="pFeaCls">要素类</param>
/// <param name="pGeoList">几何体列表</param>
/// <returns></returns>
public List<int> GetIntersectGeoList(IFeatureClass pFeaCls, List<IGeometry> pGeoList)
{
List<int> oidList = new List<int>();
//创建空间索引
ESRI.ArcGIS.Carto.IFeatureIndex2 pFeatureIndex = new ESRI.ArcGIS.Carto.FeatureIndexClass();
pFeatureIndex.FeatureClass = pFeaCls;
pFeatureIndex.Index(null, null);
//查询
ESRI.ArcGIS.Carto.IIndexQuery2 indexQuery = pFeatureIndex as ESRI.ArcGIS.Carto.IIndexQuery2;
for (int m = 0; m < pGeoList.Count; ++m)
{
IGeometry pGeo = pGeoList[m];
object pSAIds = null;
indexQuery.IntersectedFeatures(pGeo,out pSAIds);
List<int> tempOidList = pSAIds as List<int>;
if (tempOidList.Count > 0)
{
oidList.AddRange(tempOidList);
}
}
return oidList;
}