GDAL OGR C++ API 学习之路 (4)OGRFeature 篇 代码示例

GetRawFieldRef

inline OGRField *GetRawFieldRef(int i)        

获取指向给定索引的内部字段值的指针

参数:

iField -- 要获取的字段,从 0 到 GetFieldCount()-1。

返回: 指针指向内部数据结构,不应释放或修改

// 获取指定索引的原始字段引用
int fieldIndex = 0; // 要获取的字段的索引
OGRField* rawField = feature->GetRawFieldRef(fieldIndex);

// 使用原始字段引用
cout << "Field Name: " << rawField->GetNameRef() << endl;
cout << "Field Type: " << rawField->GetType() << endl;

对获取的字段进行处理 

const char* fieldName = rawField->GetNameRef();
if (strcmp(fieldName, "expected_field_name") == 0)
{
    // 属性名称匹配
}
OGRFieldType fieldType = rawField->GetType();
if (fieldType == OFTString)
{
    // 属性类型是字符串类型
}

inline const OGRField *GetRawFieldRef(int i) const    不能用于修改字段

GetFieldAsInteger

int GetFieldAsInteger(int i) const       

将字段值提取为整数

参数:

iField -- 要获取的字段,从 0 到 GetFieldCount()-1。

返回: 字段值

用于从字段中获取一个64位整数值,并以该整数的方式表示字段的内容。该函数返回的值是一个副本,表示字段的当前值,但并不影响实际的字段值。因此,您不能通过该函数修改字段本身的值

GetFieldAsInteger64

GIntBig GetFieldAsInteger64(int i) const

将字段值提取为整数 64 位

参数:

iField -- 要获取的字段,从 0 到 GetFieldCount()-1。

返回: 字段值

int fieldIndex = 0; // 要获取的字段的索引
GIntBig fieldValue = feature->GetFieldAsInteger64(fieldIndex);

// 打印字段值
cout << "Field Value: " << fieldValue << endl;

GetFieldAsDouble

double GetFieldAsDouble(int i) const

将字段值提取为双精度值

GetFieldAsString

const char *GetFieldAsString(int i) const

// 获取指定索引字段的字符串值
int fieldIndex = 0; // 要获取的字段的索引
const char* fieldValue = feature->GetFieldAsString(fieldIndex);

cout << "Field Value: " << fieldValue << endl;

GetFieldAsISO8601DateTime

const char *GetFieldAsISO8601DateTime(int i, CSLConstList papszOptions) const

参数:

  • iField -- 要获取的字段,从 0 到 GetFieldCount()-1。

  • papszOptions -- NULL 终止的字符串列表,或 NULL。当前未定义任何选项。

返回: 字段值。此字符串是内部字符串,不应修改或释放。它的寿命可能非常短暂

// 获取指定索引字段的ISO 8601日期时间值
int fieldIndex = 0; // 要获取的字段的索引
const char* options[] = {"TIME_FORMAT=YYYY/MM/DD HH:MM:SS", nullptr};

//'CSLConstList'类型的变量需要以nullptr作为列表的最后一个元素,以标识列表的结束。这样,在使用CSLConstList类型时,我们可以使用这个特殊的结束标记来确定字符串列表的边界

CSLConstList papszOptions = (CSLConstList)options;
const char* fieldValue = feature->GetFieldAsISO8601DateTime(fieldIndex, papszOptions);
  1. "TIME_FORMAT=YYYY/MM/DD HH:MM:SS":指定日期时间的格式。
  2. "TZDIR=/path/to/tz/directory":指定时区数据库的路径。
  3. "ZOFFSET=±HH:MM":指定日期时间的时区偏移量

GetFieldAsIntegerList

const int *GetFieldAsIntegerList(int i, int *pnCount) const 

将字段值作为整数列表获取(不是整数类型的数据不会获取仅适用于 OFTIntegerList 字段

参数:

  • iField -- 要获取的字段,从 0 到 GetFieldCount()-1。

  • pnCount -- 用于将列表计数(整数数)放入的整数。

返回:字段值。此列表是内部列表,不应修改或释放。它的寿命可能非常短暂。如果 *pnCount 在返回时为零,则返回的指针可能为 NULL 或非 NULL

// 获取指定索引字段的整数列表
int fieldIndex = 0; // 要获取的字段的索引
int count = 0; // 列表中整数的数量
const int* integerList = feature->GetFieldAsIntegerList(fieldIndex, &count);

// 打印整数列表
cout << "Integer List: ";
for (int i = 0; i < count; i++)
{
    cout << integerList[i] << " ";
}
cout << endl;

GetFieldAsInteger64List

const GIntBig *GetFieldAsInteger64List(int i, int *pnCount) const

将字段值作为 64 位整数的列表获取        仅适用于 OFTInteger64List 字段

GetFieldAsDoubleList

const double *GetFieldAsDoubleList(int i, int *pnCount) const

将字段值作为双精度列表获取      仅适用于 OFTInteger64List 字段

GetFieldAsStringList

char **GetFieldAsStringList(int i) const

将字段值作为字符串列表获取        仅适用于 OFTStringList 字段

GetFieldAsBinary

GByte *GetFieldAsBinary(int i, int *pnCount) const

将字段值作为二进制数据获取        仅适用于 OFTBinary 和 OFTString 字段

GetFieldAsDateTime

int GetFieldAsDateTime(int i, int *pnYear, int *pnMonth, int *pnDay,国际 *pnHour, int *pnMinute, int *pnSecond, int *pnTZFlag) const

获取字段值作为日期和时间        仅适用于 OFTDate、OFTTime 和 OFTDateTime 字段

参数:

  • iField -- 要获取的字段,从 0 到 GetFieldCount()-1。

  • pnYear -- (包括世纪)

  • pnMonth -- (1-12)

  • pnDay -- (1-31)

  • pnHour -- (0-23)

  • pn分钟 -- (0-59)

  • 第二秒 -- (0-59)

  • pnTZFlag -- (0=未知,1=本地时间,100=GMT,详见数据模型)

返回:成功时为 TRUE,失败时为 FALSE。

int GetFieldAsDateTime(int i, int *pnYear, int *pnMonth, int *pnDay,国际 *pnHour, int *pnMinute, float *pfSecond, int *pnTZFlag) const

一个接受float *pfSecond参数作为秒的浮点数,另一个接受int *pnSecond参数作为秒的整数

// 获取指定索引字段的日期和时间值
    int fieldIndex = 0; // 要获取的字段的索引
    int year, month, day, hour, minute, tzFlag;
    float second;
    int result = feature->GetFieldAsDateTime(fieldIndex, &year, &month, &day, &hour, &minute, &second, &tzFlag);

    if (result)
    {
        cout << "日期和时间解析失败." << endl;
    }
    else
    {
        cout << "Year: " << year << endl;
        cout << "Month: " << month << endl;
        cout << "Day: " << day << endl;
        cout << "Hour: " << hour << endl;
        cout << "Minute: " << minute << endl;
        cout << "Second: " << second << endl;
        cout << "Timezone Flag: " << tzFlag << endl;
    }

GetFieldAsSerializedJSon

char *GetFieldAsSerializedJSon(int i) const

将字段值作为序列化的 JSon 对象获取        仅适用于具有OFSTJSON子类型,OFTStringList,OFTIntegerList,OFTInteger64List和OFTRealList的OFTString

// 获取指定索引字段的序列化 JSON 字符串
    int fieldIndex = 0; // 要获取的字段的索引
    const char* jsonStr = feature->GetFieldAsSerializedJSon(fieldIndex);

    if (jsonStr != nullptr)
    {
        std::cout << "序列化 JSON 字符串: " << jsonStr << std::endl;
    }
    else
    {
        std::cout << "获取序列化 JSON 字符串失败." << std::endl;

    }

GetFieldAsInteger

inline int GetFieldAsInteger(const char *pszFName) const

将字段值提取为整数        OFTReal 字段将被强制转换为整数,OFTInteger64 降级为 32 位,如果超出范围,则进行钳位。其他字段类型或错误将导致返回值为零

参数:

pszFName -- 要获取的字段的名称。

返回: 字段值

// 获取指定字段的整数值
    const char* fieldName = "your_field_name"; // 要获取的字段的名称
    int fieldValue = feature->GetFieldAsInteger(fieldName);
    cout << "字段整数值: " << fieldValue << endl;

GetFieldAsInteger64

inline GIntBig GetFieldAsInteger64(const char *pszFName) const 

将字段值提取为整数 64 位

GetFieldAsDouble

inline double GetFieldAsDouble(const char *pszFName) const

将字段值提取为双精度值

GetFieldAsString

inline const char *GetFieldAsString(const char *pszFName) const

以字符串形式获取字段值

GetFieldAsISO8601DateTime

inline const char *GetFieldAsISO8601DateTime(const char *pszFName, CSLConstList papszOptions) const

获取 OFTDateTime 字段值作为 ISO8601 表示形式

下面的所有函数都和上面的获取方法一样,不一样的是这里接受个字段名称作为参数接受一个字段索引作为参数

GetFieldAsIntegerList

inline const int *GetFieldAsIntegerList(const char *pszFName, int *pnCount) const     

将字段值作为整数列表获取

GetFieldAsInteger64List

inline const GIntBig *GetFieldAsInteger64List(const char *pszFName, int *pnCount) const

将字段值作为 64 位整数的列表获取

GetFieldAsDoubleList

inline const double *GetFieldAsDoubleList(const char *pszFName, int *pnCount) const

将字段值作为双精度列表获取

GetFieldAsStringList

inline char **GetFieldAsStringList(const char *pszFName) const

将字段值作为字符串列表获取

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

场主不吃鍋巴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值