ArcEngine查询统计IQueryFilter、ISpatialFilter、IQueryDef 、WHEREclauses

用ArcEngine的接口做过查询统计的同学应该知道,由于ArcEngine并没有完整支持SQL,所以做简单的查询还好,稍微复杂一点就开始捉襟见肘了。 
现在总结一下几种查询的方法: 
1.IQueryFilter、ISpatialFilter接口。 
这是最常见的,把参数设置进去,然后调用IFeatureClass或者ITable的Search方法就行。可以转为IQueryFilterDefinition接口设置Order By和Group By语句。 
2.IQueryDef接口。 
个人觉得这个接口最方便,可以设置多表查询。虽然有些帮助文档说不能使用Order By和Group By语句,但是其实是可以的,比如可以这样设置: 
queryDef.Tables = “tableA Group by Field01”; 
queryDef.SubFields = “Field01”; 
或者 
queryDef.Tables = “tableA”; 
queryDef.SubFields = “Field01, COUNT(Field01)”; 
queryDef.WhereClause = “Field01 is not null GROUP BY Field01 HAVING COUNT(Field01) > 1”; 
但是这个接口只可以用在GeoDatabase,不能用于ShapeFile和Coverage数据。 
3.使用IDataStatistics接口,然后调用Statistics方法返回IStatisticsResults对象,可以获取六种统计结果。 
4.另外还要加一种方法,就是IWorkspace.ExecuteSQL,可以直接输入SQL语句,但是这个方法没有返回值,不能获取结果,所以只有在做一些系统维护功能,需要直接操纵SDE表的时候才用到。 
暂时只有这些,有遗漏的话以后再补充。

Geodatabase应用程序编程接口(API)提供了许多不同的方式来查询表和要素类。本主题介绍如何使用query filters, spatial filters, and QueryDefs对数据进行查询。 


1.IQueryFilter interface

// Create the query filter.  
IQueryFilter queryFilter = new QueryFilterClass();  

// Select the fields to be returned—the name and address of the businesses.  
queryFilter.SubFields = "NAME, ADDRESS";  

// Set the filter to return only restaurants.  
queryFilter.WhereClause = "TYPE = 'Restaurant'";  

// Use the PostfixClause to alphabetically order the set by name.  
IQueryFilterDefinition queryFilterDef = (IQueryFilterDefinition)queryFilter;  
queryFilterDef.PostfixClause = "ORDER BY NAME";  

// Output the returned names and addresses.  
int nameIndex = table.FindField("NAME");  
int addressIndex = table.FindField("ADDRESS");  
using (ComReleaser comReleaser = new ComReleaser())  
{  
  ICursor cursor = table.Search(queryFilter, true);  
  comReleaser.ManageLifetime(cursor);  
  IRow row = null;  
  while ((row = cursor.NextRow()) != null)  
  {  
    String name = Convert.ToString(row.get_Value(nameIndex));  
    String address = Convert.ToString(row.get_Value(addressIndex));  
    Console.WriteLine("{0} - {1}", name, address);  
  }  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2.ISpatialFilter interface

// Create the envelope and define its position.  
IEnvelope envelope = new EnvelopeClass();  
envelope.PutCoords(-84.4078, 33.7787, -84.3856, 33.7997);  

// Create the spatial filter and set its spatial constraints.  
ISpatialFilter spatialFilter = new SpatialFilterClass  
{  
  Geometry = envelope,  
  GeometryField = featureClass.ShapeFieldName,  
  SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects  
};  

// Set the attribute constraints and subfields.  
// You want to exclude ramps, highways and interstates.  
spatialFilter.WhereClause = "NAME <> 'Ramp' AND PRE_TYPE NOT IN ('Hwy', 'I')";  
spatialFilter.SubFields = "NAME, TYPE";  

// Execute the query, displaying the roads returned.  
int nameIndex = featureClass.FindField("NAME");  
int typeIndex = featureClass.FindField("TYPE");  
using (ComReleaser comReleaser = new ComReleaser())  
{  
  IFeatureCursor featureCursor = featureClass.Search(spatialFilter, true);  
  comReleaser.ManageLifetime(featureCursor);  
  IFeature feature = null;  
  while ((feature = featureCursor.NextFeature()) != null)  
  {  
    String roadName = Convert.ToString(feature.get_Value(nameIndex));  
    String roadType = Convert.ToString(feature.get_Value(typeIndex));  
    Console.WriteLine("Name: {0}, Type: {1}", roadName, roadType);  
  }  
}  
//if a geometry bag is being used as the filter's query geometry, create a spatial index for the geometry bag before being assigned //to the geometry property. This can drastically increase the query's efficiency. The following code example shows how to do this:  

// Cast the bag to the ISpatialIndex interface.  
ISpatialIndex spatialIndex = (ISpatialIndex)geometryBag;  
spatialIndex.AllowIndexing = true;  
spatialIndex.Invalidate(); 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
3.IQueryDef interface

// Create the QueryDef.  
IQueryDef2 queryDef2 = (IQueryDef2)featureWorkspace.CreateQueryDef();  

// Specify the table and fields to query.  
queryDef2.Tables = "Cities";  
queryDef2.SubFields = "Name, Pop1996";  
queryDef2.PostfixClause = "ORDER BY Pop1996 DESC";  


// Execute the query.  
using (ComReleaser comReleaser = new ComReleaser())  
{  
  ICursor cursor = queryDef2.Evaluate2(true);  
  comReleaser.ManageLifetime(cursor);  
  int nameIndex = cursor.FindField("Name");  
  int pop1996Index = cursor.FindField("Pop1996");  
  IRow row = null;  
  while ((row = cursor.NextRow()) != null)  
  {  
    String cityName = Convert.ToString(row.get_Value(nameIndex));  
    int population = Convert.ToInt32(row.get_Value(pop1996Index));  
    Console.WriteLine("{0}: {1}", cityName, population);  
  }  
}  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
通过创建游标QueryDef 可用于生成一个虚拟表或要素类。

// Create a reference to a TableQueryName object.  
IQueryName2 queryName2 = new TableQueryNameClass();  
queryName2.PrimaryKey = String.Empty;  

// Specify the query definition.  
queryName2.QueryDef = queryDef;  

// Get a name object for the workspace.  
IDataset dataset = (IDataset)workspace;  
IWorkspaceName workspaceName = (IWorkspaceName)dataset.FullName;  

// Cast the TableQueryName object to the IDatasetName interface and open it.  
IDatasetName datasetName = (IDatasetName)queryName2;  
datasetName.WorkspaceName = workspaceName;  
datasetName.Name = tableName;  
IName name = (IName)datasetName;  

// Open the name object and get a reference to a table object.  
ITable table = (ITable)name.Open();  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
4.WHERE clauses 

--------------------- 
作者:sRhee 
来源:CSDN 
原文:https://blog.csdn.net/srhee/article/details/79036578 
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自blog.csdn.net/myfatenight/article/details/86467088