FileGDB API Table类的Search函数支持简单的SELECT查询语句,其包含两种定义方式,分别是:
第一种定义方式为: long FileGDBAPI::Table::Search ( const std::wstring & subfields, const std::wstring & whereClause, Envelope envelope, bool recycling, EnumRows & rows ) Performs a spatial query (envelope intersects) on the table. Parameters: [in] subfields (Optional) The fields that should be fetched by the query's returned rows. Must include a comma delimited list of fields or a "*". Passing in blank will return a -2147220985 (INVALID_SQL) error. [in] whereClause (Optional) Attribute constraints to apply to the query. [in] envelope The spatial extent of the query. [in] recycling Indicates whether row memory should be recycled. [out] rows The results of the query. Returns: A long integer indicating whether the method finished successfully.
这个函数可以通过范围和属性两种方法进行过滤。
第二种定义方式:
long FileGDBAPI::Table::Search ( const std::wstring & subfields, const std::wstring & whereClause, bool recycling, EnumRows & rows ) Performs an attribute query on the table. Parameters: [in] subfields (Optional) The fields that should be fetched by the query's returned rows. Must include a comma delimited list of fields or a "*". A blank will return a -2147220985 (INVALID_SQL) error. [in] whereClause (Optional) Attribute constraints to apply to the query. [in] recycling Indicates whether row memory should be recycled. [out] rows The results of the query. Returns: A long integer indicating whether the method finished successfully.
这种方法不支持范围查询,只支持where字句查询。
需要注意的是:
如果过滤的时候不用where字句进行过滤,那么whereclause参数需要设置成L””,而不是L” “. 否则构建出来的SQL语句是错误的。
Query例子提供了这两个函数的使用,大家可以参考一下这个例子。
Query例子程序使用的数据为一个点层数据,下面的例子是我自己写的,来访问面和线数据。由于FileGDB提供的函数提供给我们的是各种geometry对象的二进制buffer,并没有提供具体的对象,因此我们需要自己写程序将这些二进制buffer解析成点串坐标。
下面是显示多边形和线点串坐标的一段程序:
#include <stdio.h> #include <tchar.h> #include <string> #include <iostream> #include <vector> #include <map> #include <atlbase.h> #define EXT_FILEGDB_API _declspec(dllimport) #pragma warning (disable : 4251) #include "Geodatabase.h" #include "GeodatabaseManagement.h" #include "Table.h" #include "Row.h" #include "Util.h" #include "commonlib.h" using namespace std; using namespace FileGDBAPI; Geodatabase geodatabase; int main() { // Create a new geodatabase in the current directory. long hr; // Re-open the geodatabase. if ((hr = OpenGeodatabase(L"../data/TestFileGDB.gdb", geodatabase)) != S_OK) { cout << "An error occurred while opening the geodatabase." << endl; cout << "Error code: " << hr << endl; return -1; } cout << "The geodatabase has been opened." << endl; Table table; if((hr = geodatabase.OpenTable(L"//Drainage//Catchment",table)) != S_OK) { cout << "An error occurred while open the table." << endl; cout << "Error code: " << hr <<endl; return -1; } EnumRows QueryRows; hr = table.Search(L"Shape",L"",true,QueryRows); //if ((hr = table.Search(L"Shape, NAME, Pop1996", L"TERM = 'City'", true, QueryRows)) != S_OK) if(S_OK != hr) { cout << "An error occurred while Searching the table." << endl; cout << "Error code: " << hr <<endl; return -1; } Row QueryRow; ShapeBuffer geometry; int i = 0; while(QueryRows.Next(QueryRow) == S_OK) { QueryRow.GetGeometry(geometry); GeometryObj* pGeometryObj = getgeometry(geometry.shapeBuffer); if(pGeometryObj) { for (i = 0; i < pGeometryObj->nVertixCount; i++) { cout << "{x= " << pGeometryObj->pfXY[i].x <<"y= "<<pGeometryObj->pfXY[i].y << " }"<< endl; }
releasegeometry(pGeometryObj); } } QueryRows.Close(); if((hr = geodatabase.CloseTable(table)) != S_OK) { cout << "An error occurred while closing the table." << endl; cout << "Error code: " << hr << endl; return -1; } if ((hr = CloseGeodatabase(geodatabase)) != S_OK) { cout << "An error occurred while closing the geodatabase." << endl; cout << "Error code: " << hr << endl; return -1; } return 0; }
运行结果如下: