我正在使用GDAL版本2.0.0。如何使用GDAL C++从shapefile中删除特征?
我有一个C++程序,我想从shapefile中删除一个特征。返回错误代码表示成功删除。但是,该功能保留在shapefile中。
是否有一些后续功能,我必须调用,以便删除将持续?例如,当您对现有功能的字段进行更改时,必须再次调用OGRLayer::SetFeature()
,否则该更改不会持续。
以下是我正在做的一个片段。 shapefile是通过对栅格文件进行多边形化从头开始创建的。然后我在关闭文件之前尝试从该shape文件中删除一个特征。
#include <gdal_alg.h> // GDALPolygonize()
#include <gdal_priv.h>
//
// srcPath is the path of the soruce raster geospatial file. e.g., a GeoTIFF file.
// tgtPath is the path of the target vector geospatial file. i.e., a shapefile.
//
void test(char const * srcPath, char const * tgtPath)
{
GDALAllRegister();
// Open source file.
GDALDataset * pSrcDataset = static_cast<GDALDataset *>(GDALOpen(srcPath, GA_ReadOnly));
GDALRasterBand * pSrcBand = pSrcDataset->GetRasterBand(1);
// Create and open target file.
GDALDriver * pDriver = GetGDALDriverManager()->GetDriverByName("ESRI Shapefile");
GDALDataset * pTgtDataset = pDriver->Create(tgtPath, 0, 0, 0, GDT_Unknown, NULL);
// Create a layer.
OGRLayer * pTgtLayer = pTgtDataset->CreateLayer("layer", NULL, wkbPolygon, NULL);
// Create a field to contain value associated with each polygon feature.
OGRFieldDefn field("value", OFTInteger);
field.SetWidth(6);
pTgtLayer->CreateField(&field);
// Call GDALPolygonize to convert source raster to polygon features.
GDALRasterBand * pMaskBand = NULL;
int valueFieldIdx = 0;
char ** options = NULL;
GDALPolygonize(
pSrcBand,
pMaskBand,
pTgtLayer,
valueFieldIdx,
options,
GDALTermProgress, NULL);
// Demonstrate that the target layer has the capability to delete a feature.
if (!pTgtLayer->TestCapability(OLCDeleteFeature))
{
throw "Layer does not support delete feature capability";
}
// Delete a feature that I know is there.
// The feature has a particular integer ID.
OGRErr err = pTgtLayer->DeleteFeature(12);
// Demonstrate that there is a zero error code, indicating successful deletion.
if (err != OGRERR_NONE)
{
throw "Failed to remove feature";
}
// Close source and target files.
GDALClose(pSrcDataset);
GDALClose(pTgtDataset);
}
什么是pLayer?这是一个错字吗? –
这确实是一个错字。我修好了它。 –