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

OGRFeature Class        “ogrsf_frmts.hpp/ogrsf_frmts.h”

OGRFeature是OGR库中用于描述矢量数据要素的类。要素是GIS数据的基本单元,它由一组属性值和空间几何体组成。OGRFeature类可以描述各种类型的要素,例如点、线、多边形、面等

OGRFeature对象包含了一条要素(feature)的所有数据,包括几何形状和属性值等

OGRFeature 和 OGRFeatureDefn 之间有很多相似的方法,这是因为 OGRFeature 类是基于 OGRFeatureDefn 类的模板而构建的。OGRFeatureDefn 描述了一个要素的结构,而 OGRFeature 则包含了实际的数据。因此,OGRFeature 拥有许多 OGRFeatureDefn 的方法,用于获取或设置 OGRFeature 的属性信息。但是,OGRFeature 还包含了一些额外的方法,如 GetGeometryRef() 和 SetGeometry(),用于获取和设置要素的几何信息

OGRFeature

explicit OGRFeature(OGRFeatureDefn*)        此方法与 C 函数 OGR_F_Create() 相同

构造函数

参数: poDefnIn – 要素将遵循的要素类(图层)定义

OGRFeatureDefn featureDefn("TestFeature");
featureDefn.AddFieldDefn("Name", OFTString);
featureDefn.AddFieldDefn("Age", OFTInteger);

// 创建OGRFeature对象,并设置两个字段的值
OGRFeature feature(&featureDefn);

begin

ConstFieldIterator begin() const

返回字段值迭代器的开始

end

ConstFieldIterator end() const

返回字段值迭代器的结束

OGRFeature *poFeature = poLayer->GetNextFeature();
if (poFeature != NULL) {
    for (OGRFeature::ConstFieldIterator i = poFeature->begin(); i != poFeature->end(); ++i) {
        const char* pszFieldName = i->GetNameRef();
        OGRFieldType fieldType = i->GetFieldType();
        OGRField* field = poFeature->GetFieldRef(pszFieldName);
        // 对Field进行某些处理
    }
}

operator[]

const FieldValue operator[ ](int iField) const                FieldValue operator[ ] (int iField)       

当我们只需要获取OGRFeature对象中的值时,可以使用const;如果需要修改或者获取OGRFeature对象中的值,则使用后者

返回字段值

参数:

iField – 要获取的字段,从 0 到 GetFieldCount()-1。这不是通过方法检查的!

返回:  字段值

    OGRFeatureDefn featureDefn("TestFeature");
    featureDefn.AddFieldDefn("Name", OFTString);
    featureDefn.AddFieldDefn("Age", OFTInteger);

    // 创建OGRFeature对象,并设置两个字段的值
    OGRFeature feature(&featureDefn);
    feature.SetField("Name", "Tom");
    feature.SetField("Age", 20);

    // 获取第一个字段的值
    const FieldValue name = feature[0];  // 使用整数类型的字段索引
 // FieldValue name = feature[0];

    if (name.getType() == OFTString) {
        cout << "Name: " << name.getString() << endl;
    }

    // 获取第二个字段的值
    const FieldValue age = feature[1];    // 使用整数类型的字段索引
 // FieldValue age = feature[1];
    if (age.getType() == OFTInteger) {
        cout << "Age: " << age.getInteger() << endl;
    }

operator[]

const FieldValue operator[](const char *pszFieldName) const        FieldValue operator[]const char *pszFieldName )

当我们只需要获取OGRFeature对象中的值时,可以使用const;如果需要修改或者获取OGRFeature对象中的值,则使用后者

参数:

psz字段名称 – 字段名称

返回: 字段值,如果未找到,则抛出 FieldNotFoundException

const char *pszFieldName = "Name";
    try {
        const FieldValue &fieldValue = oFeature[pszFieldName];
        cout << "Found field value: " << fieldValue.ToString() << endl;
    } catch (const FieldNotFoundException &e) {
        cerr << e.what() << std::endl;
    }

    // 找一个并不存在的字段名
    pszFieldName = "NonExistingField";
    try {
        const FieldValue &fieldValue = oFeature[pszFieldName];
        cout << "Found field value: " << fieldValue.ToString() << endl;
    } catch (const FieldNotFoundException &e) {
        cerr << e.what() << endl;
    }

GetDefnRef

inline OGRFeatureDefn *GetDefnRef()       inline const OGRFeatureDefn *GetDefnRef() const        此方法与 C 函数 OGR_F_GetDefnRef() 相同

前者适用于对对象的只读访问,后者适用于对对象进行修改的情况

获取功能定义

返回:  对功能定义对象的引用

OGRFeatureDefn* poDefn = poFeature->GetDefnRef();

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

场主不吃鍋巴

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

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

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

打赏作者

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

抵扣说明:

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

余额充值