FileGDB API for linux 学习之七,数据的查询

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;
}

运行结果如下:

image

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值