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

GetArrowStream

virtual bool GetArrowStream(struct ArrowArrayStream *out_stream, CSLConstList papszOptions = nullptr)       

获取箭头 C 流

参数:

  • out_stream – 输出流。不得为空。结构的内容不需要初始化。

  • papszOptions – 键=值选项的空终止列表。

返回: 在成功的情况下为真

Arrow流是Apache Arrow数据格式的一种序列化表示形式。它由多个数据块组成,每个数据块都是一个封装为字节数组的Arrow表。每个表由三个部分组成:

  1. Schema: 描述表的结构和元数据,例如字段名称、数据类型、nullability等。
  2. Record Batch: 表示表的一个子集,由一系列行组成,其中每行包含表中一组字段的值。
  3. Dictionary Batch (可选): 如果表包含字典编码的列,则这个部分会包含这些列的字典值,以便在序列化和反序列化期间进行转换。

它用于将OGRLayer中的要素数据导出为Apache Arrow格式的数据流。由于Arrow格式可以在不同的数据处理系统之间高效地传输和共享数据,因此GetArrowStream可以方便用户在GDAL/OGR和其他数据处理系统之间进行数据交互和集成。

然而,由于Apache Arrow在很长一段时间内只是一个新兴的数据格式,因此在实际应用中,很多老的数据处理系统可能并不支持Arrow格式,因此GetArrowStream在实际应用中的使用还比较有限。不过随着Arrow格式的逐渐普及和成熟,GetArrowStream在未来的数据处理应用中可能会得到更广泛的应用

SetFeature

 OGRErr SetFeatureOGRFeature *poFeature)        此方法与 C 函数 OGR_L_SetFeature() 相同

重写/替换现有特征

参数:

poFeature – 要编写的功能。

返回:OGRERR_NONE操作是否有效,否则提供适当的错误代码(例如,如果该功能不存在,则OGRERR_NON_EXISTING_FEATURE)

GDALAllRegister();
OGRRegisterAll();

GDALDataset *poDS = (GDALDataset*)GDALOpenEx("CHN_adm0.shp", GDAL_OF_VECTOR, NULL, NULL, NULL);

// 获取第一个图层
OGRLayer *poLayer = poDS->GetLayer(0);

// 获取第一个要素
OGRFeature *poFeature = poLayer->GetFeature(1);

// 将新的点坐标设置给要素
OGRPoint pt(1.0, 2.0);
poFeature->SetGeometry(&pt);

// 将更新的要素写回图层
OGRErr err =poLayer->SetFeature(poFeature);
if (err == OGRERR_NONE)
{
    printf("成功添加了一个要素到图层中!\n");
}
else
{
    printf("添加要素失败!\n");
}

// 释放资源
delete poFeature;
GDALClose(poDS);

SetGeometry

用于设置要素的几何体

这在ORGFeature会讲解:

查看是否支持随机写入

GDALAllRegister();

    // 打开Shapefile数据源
    GDALDataset *poDS = (GDALDataset*)GDALOpenEx("test.shp", GDAL_OF_VECTOR, NULL, NULL, NULL);
    if (poDS == NULL) {
        cout << "打开数据失败" << endl;
        return 1;
    }

    // 获取第一个图层
    OGRLayer *poLayer = poDS->GetLayer(0);

    // 测试图层是否支持随机访问写入
    if (poLayer->TestCapability(OLCRandomWrite)) {
        cout << "图层支持随机写入." << endl;
    } else {
        cout << "图层不支持随机写入." << endl;
    }

    GDALClose(poDS);

CreateFeature

OGRErr CreateFeatureOGRFeature *poFeature)        此方法与 C 函数 OGR_L_CreateFeature() 相同

在图层中创建并写入新要素

参数:

poFeature – 写入磁盘的功能。

返回:  OGRERR_NONE成功

OGRLayer *poLayer = poDS->GetLayer(0);
OGRFeature* poFeature = new OGRFeature(poLayer->GetLayerDefn());

// 设置要素几何
OGRPoint pt(1.0, 2.0);
poFeature->SetGeometry(&pt);

// 添加要素到图层
if (poLayer->CreateFeature(poFeature) == OGRERR_NONE) {
    cout << "Feature added successfully." << endl;
} else {
    cout << "Failed to add feature." << endl;
}

UpsertFeature

OGRErr UpsertFeatureOGRFeature *poFeature)        此方法与 C 函数 OGR_L_UpsertFeature() 相同

重写/替换现有要素或在图层内创建新要素

参数:

poFeature – 写入磁盘的功能。

返回: OGRERR_NONE成功

展示了Creat与UPpsert的区别

OGRLayer *poLayer = poDS->GetLayer(0);
OGRFeature* poFeature = new OGRFeature(poLayer->GetLayerDefn());
// 设置要素属性
poFeature->SetField("ID", 1);
OGRPoint pt(1.0, 2.0);
poFeature->SetGeometry(&pt);

// 判断是否存在指定的要素
OGRFeature* existingFeature = poLayer->GetFeature(1);
if (existingFeature != nullptr) {
    // 如果已存在,则更新属性
    poLayer->SetFeature(poFeature);
} else {
    // 如果不存在,则添加新要素
    poLayer->CreateFeature(poFeature);
}

// 释放要素内存
OGRFeature::DestroyFeature(poFeature);

UpdateFeature

OGRErr UpdateFeature(OGRFeature *poFeature,   int nUpdateFieldsCount,   const int *panUpdateFieldsIdx

 int nUpdateGeomFieldsCount, const int *panUpdateGeomFieldsIdx, bool bUpdateStyleString)            此方法与 C 函数 OGR_L_UpdateFeature() 相同

此方法与 C 函数 OGR_L_UpdateFeature() 相同

更新(部分)现有功能

参数:

  • poFeature – 要更新的功能。

  • nUpdateFieldsCount – 要更新的属性字段数。可能为 0

  • panUpdateFieldsIdx – nUpdateFieldsCount 值数组,每个值介于 0 和 GetLayerDefn()->GetFieldCount() - 1 之间,指示必须在图层中更新 poFeature 的哪些字段。

  • nUpdateGeomFieldsCount – 要更新的几何字段数。可能为 0

  • panUpdateGeomFieldsIdx – nUpdateGeomFieldsCount 值的数组,每个值介于 0 和 GetLayerDefn()->GetGeomFieldCount() - 1 之间,指示必须在图层中更新 poFeature 的哪些几何字段。

  • bUpdateStyleString – 是否应使用 poFeature 之一更新图层中的要素样式字符串。

返回:  OGRERR_NONE操作是否有效,否则提供适当的错误代码(例如,如果该功能不存在,则OGRERR_NON_EXISTING_FEATURE)。

OGRErr err = poLayer->UpdateFeature(poFeature, 0, NULL, 0, NULL, false) 表示更新所有字段

OGRLayer *poLayer = poDS->GetLayer(0);
// 获取要更新的要素
OGRFeature* poFeature = poLayer->GetFeature(1);

// 更新要素的属性
poFeature->SetField("Name", "NewName");
poFeature->SetField("Age", 25);

// 更新要素的几何信息
OGRGeometry* poGeometry = poFeature->GetGeometryRef();
OGRPoint pt(2.0, 2.0);
poGeometry->addPoint(&pt);

// 更新要素
int anUpdateFieldsIdx[] = { 1, 2 }; // 要更新的字段索引
int anUpdateGeomFieldsIdx[] = { 0 }; // 要更新的几何字段索引
OGRErr eErr = poLayer->UpdateFeature(poFeature, 2, anUpdateFieldsIdx, 1, anUpdateGeomFieldsIdx, false);

if (eErr == OGRERR_NONE)
{
    printf("要素更新成功\n");
}
else
{
    printf("要素更新失败:%s\n", CPLGetLastErrorMsg());
}

// 释放要素内存
OGRFeature::DestroyFeature(poFeature);

DeleteFeature

virtual OGRErr DeleteFeatureGIntBig nFID)        此方法与 C 函数 OGR_L_DeleteFeature() 相同

从图层中删除要素

参数:

nFID – 要从图层中删除的要素 ID

返回:  OGRERR_NONE操作是否有效,否则提供适当的错误代码(例如,如果该功能不存在,则OGRERR_NON_EXISTING_FEATURE)

GDALDataset* poDS = (GDALDataset*) GDALOpenEx("path/to/your/dataset", GDAL_OF_VECTOR, NULL, NULL, NULL);
OGRLayer* poLayer = poDS->GetLayer(0);

// 获得要素并通过FID删除
OGRFeature* poFeature = poLayer->GetFeature(5);
OGRErr err = poLayer->DeleteFeature(poFeature->GetFID());

// 检查
if (err != OGRERR_NONE) {
    printf("删除要素错误\n");
} else {
    printf("删除要素成功\n");
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

场主不吃鍋巴

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

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

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

打赏作者

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

抵扣说明:

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

余额充值