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

SetGeometryDirect

OGRErr SetGeometryDirectOGRGeometry*)        方法与 C 函数 OGR_F_SetGeometryDirectly() 相同

设置要素几何

参数:

poGeomIn – 应用于特征的新几何图形。在此处传递 NULL 值是正确的,这将导致释放当前分配的几何图形而不分配新的几何图形。

返回:  OGRERR_NONE是否成功,或者OGR_UNSUPPORTED_GEOMETRY_TYPE几何类型对于 OGRFeatureDefn 是非法的(检查尚未实现)

SetGeometry

OGRErr SetGeometryconst OGRGeometry*)        方法与 C 函数 OGR_F_SetGeometry() 相同

设置要素几何

与 SetGeometryDirect 一样

对比

SetGeometry() 接受一个指向 OGRGeometry 对象的指针,并且会复制该对象

SetGeometryDirect() 接受一个指向 OGRGeometry 对象的指针,它将直接使用传递的几何对象作为特征几何

总的来说,SetGeometryDirect() 更快,因为它避免了复制几何对象的开销。但是,使用它时需要格外小心,以避免出现内存泄漏或错误释放内存的情况。在大多数情况下,建议使用 SetGeometry() 来设置特征几何 

// 使用 SetGeometry 函数更新特征几何
OGRGeometry* geom = ...; // 创建一个 OGRGeometry 对象
OGRFeature* feature = layer->GetFeature(1); // 获取第一个特征
feature->SetGeometry(geom); // 更新特征几何
layer->SetFeature(feature); // 写入数据源
delete feature;

// 使用 SetGeometryDirect 函数更新特征几何
OGRGeometry* geom = ...; // 创建一个 OGRGeometry 对象
OGRFeature* feature = layer->GetFeature(1); // 获取第一个特征
feature->SetGeometryDirect(geom); // 更新特征几何
layer->SetFeature(feature); // 写入数据源
// 这里不需要删除 feature,因为 SetGeometryDirect 函数没有拷贝 OGRGeometry 对象

GetGeometryRef

OGRGeometry *GetGeometryRef()        与 C 函数 OGR_F_GetGeometryRef() 相同

获取指向要素几何的指针

返回:  指向内部要素几何的指针。不应修改此对象

GetGeometryRef

const OGRGeometry *GetGeometryRef() const        与 C 函数 OGR_F_GetGeometryRef() 相同

建议使用const版本的,因为不应修改此对象,第二种方法可以很好的避免问题产生

 const OGRGeometry *poGeometry = poFeature->GetGeometryRef();

StealGeometry

OGRGeometry *StealGeometry()

剥夺几何图形的所有权                在此调用之后,OGRFeature 将具有 NULL 几何图形

返回:  指向几何图形的指针

// 创建一个新的点对象
OGRPoint *poPoint = new OGRPoint();
poPoint->setX(1.0);
poPoint->setY(2.0);

// 创建一个新的特征对象
OGRFeature *poFeature = new OGRFeature(nullptr);
// 为特征对象设置一个几何对象
poFeature->SetGeometryDirectly(poPoint);

// 从特征对象中获取几何对象指针,清除特征对象上的几何对象,并返回该指针。
OGRGeometry *poGeometry = poFeature->StealGeometry();

char *pszWkt = nullptr;
poGeometry->exportToWkt(&pszWkt);
printf("WKT: %s\n", pszWkt);

StealGeometry

OGRGeometry *StealGeometry(int iField)

剥夺几何图形的所有权

参数:

iGeomField – 几何字段的索引。

返回: 指向几何图形的指针

OGRGeometry *poGeometry = poFeature->StealGeometry(1);

GetGeomFieldCount

inline int GetGeomFieldCount() const

获取此要素上的几何字段数        这将始终与 OGRFeatureDefn 的几何字段计数相同

返回:  几何字段的计数

int geomFieldCount = feature.GetGeomFieldCount();

cout << "这个要素有:" << geomFieldCount << " 几何字段" << endl;

 无参的 StealGeometry() 方法将移除所有几何字段,而带有索引参数的 StealGeometry(int iField) 方法将只移除特定索引的几何字段

GetGeomFieldDefnRef

inline OGRGeomFieldDefn *GetGeomFieldDefnRef(int iField)        inline const OGRGeomFieldDefn *GetGeomFieldDefnRef(int iField) const 

与 C 函数 OGR_F_GetGeomFieldDefnRef() 相同

获取此几何字段的定义

参数:

iGeomField – 要获取的字段,从 0 到 GetGeomFieldCount()-1。

返回: 字段定义(来自 OGRFeatureDefn)。这是一个内部引用,不应删除或修改

//获取特征中几何字段的数量
int geomFieldCount = feature->GetGeomFieldCount();

// 循环遍历每个几何字段并打印其名称
for (int i = 0; i < geomFieldCount; i++)
{
    const OGRGeomFieldDefn* geomFieldDefn = feature->GetGeomFieldDefnRef(i);
    cout << "Geometry field " << i << " name: " << geomFieldDefn->GetNameRef() << endl;
}

GetGeomFieldIndex

inline int GetGeomFieldIndex(const char *pszName) const        与 C 函数 OGR_F_GetGeomFieldIndex() 相同

获取此要素上的几何字段数

参数:

pszName – 要搜索的几何字段的名称。

返回: 几何字段索引,如果未找到匹配的几何字段,则为 -1

const char* geomFieldName = "geom";
int geomFieldIndex = feat->GetGeomFieldIndex(geomFieldName);

// 通过索引获取几何对象
OGRGeometry* geom = feat->GetGeomFieldRef(geomFieldIndex);

GetGeomFieldRef

OGRGeometry *GetGeomFieldRef(int iField)        const OGRGeometry *GetGeomFieldRef(int iField) const        与 C 函数 OGR_F_GetGeomFieldRef() 相同

获取指向要素几何的指针

参数:

iField – 要获取的几何字段。

返回:  指向内部要素几何的指针   不应修改此对象

OGRFeature* poFeature;
// ... 假设已经创建了OGRFeature对象poFeature

// 获取要素的第一个几何字段
OGRGeometry* poGeom = poFeature->GetGeomFieldRef(0);

GetGeomFieldRef

OGRGeometry *GetGeomFieldRefconst char *pszFName)        const OGRGeometry *GetGeomFieldRef(const char *pszFName) const 

获取指向要素几何的指针

参数:

pszFName – 要获取的几何字段的名称。

返回: 指向内部要素几何的指针。不应修改此对象

 SetGeomFieldDirect

OGRErr SetGeomFieldDirect(int iField, OGRGeometry*)        SetGeomFieldDirect

设置指定几何字段的要素几何

参数:

  • iField ― 要设置的几何字段。

  • poGeomIn – 应用于特征的新几何图形。在此处传递 NULL 值是正确的,这将导致释放当前分配的几何图形而不分配新的几何图形。

返回:  OGRERR_NONE如果成功,或者如果索引无效,则OGRERR_FAILURE,或者OGRERR_UNSUPPORTED_GEOMETRY_TYPE几何类型对于 OGRFeatureDefn 来说是非法的(检查尚未实现)

SetGeomField

OGRErr SetGeomField(int iField, const OGRGeometry*)        方法与 C 函数 OGR_F_SetGeomField() 相同

设置指定几何字段的要素几何

参数:

  • iField ― 要设置的几何字段。

  • poGeomIn – 应用于特征的新几何图形。在此处传递 NULL 值是正确的,这将导致释放当前分配的几何图形而不分配新的几何图形。

返回:  如果成功,则OGRERR_NONE,如果索引无效,则OGRERR_FAILURE,或者OGR_UNSUPPORTED_GEOMETRY_TYPE几何类型对于 OGRFeatureDefn 来说是非法的(检查尚未实现)

OGRPoint* point = new OGRPoint(1.0, 2.0);

// 设置 OGRFeature 对象的几何字段
OGRFeature* feature = layer->GetNextFeature();
feature->SetGeomField(0, point);

// 将 point 作为新几何字段设置到 OGRFeature 对象的索引为 0 的几何字段上
OGRPoint* newPoint = new OGRPoint(2.0, 3.0);
feature->SetGeomFieldDirect(0, newPoint);

区别

  • OGRErr SetGeomField方法是将传递的几何体副本存储到要素中,即使传递的几何体在调用方法后被修改或销毁,要素中存储的几何体也不会受到影响。因此,使用SetGeomField方法时必须确保传递的几何体是有效的并且不会在调用方法后被修改或销毁。

  • OGRErr SetGeomFieldDirect方法假定传递的几何体是有效的,并且将传递的几何体指针存储到要素中。这意味着传递的几何体指针现在变成了要素中几何体的指针,如果在方法调用后修改或销毁传递的几何体,要素中存储的几何体也会受到影响。因此,使用SetGeomFieldDirect方法时必须确保传递的几何体指针有效,并且不会在后续被修改或销毁

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

场主不吃鍋巴

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

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

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

打赏作者

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

抵扣说明:

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

余额充值