GDAL 对shp的增删查改

OGRSFDriver* poDriver;
poDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(pszFormat);

OGRDataSource* poDS = poDriver->Open(m_sRiverShpFile.c_str(),true);

OGRLayer* poLayer = poDS->GetLayer(0);
string sttrfilter = “RVCD = ‘”+strRVCD+”‘”;
poLayer->SetAttributeFilter(sttrfilter.c_str());
OGRFeature* pFeature;
pFeature = poLayer->GetNextFeature();
while(pFeature!=NULL)
{
RVCD=pFeature->GetFieldAsString(“RVCD”);
TRVCD=pFeature->GetFieldAsString(“TRVCD”);
cout<<RVCD<<endl;
cout<<TRVCD<<endl;
cout<<endl;
pFeature = poLayer->GetNextFeature();
}
删除图源

OGRSFDriver* MyDRiver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(pszFormat);
OGRDataSource* MyDS = MyDRiver->Open(m_sRiverShpFile.c_str(),true);
OGRLayer* myLayer = MyDS->GetLayer(0);
string myfilter = “RVCD = ‘”+strRVCD+”‘”;
myLayer->SetAttributeFilter(myfilter.c_str());
myLayer->ResetReading();
OGRFeature* myFeature = myLayer->GetNextFeature();
if (myFeature!=NULL)
{
OGRErr errNo = myLayer->DeleteFeature(0);
string strSQL = “REPACK ” + string(myLayer->GetName());
MyDS->ExecuteSQL(strSQL.c_str(),NULL,””);
OGRFeature::DestroyFeature(myFeature);

}

myLayer->SetAttributeFilter(NULL); 
myLayer->ResetReading();
OGRDataSource::DestroyDataSource(MyDS);
OGRCleanupAll();

OGRSFDriver* odr = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(pszFormat);
OGRDataSource* ods = odr->Open(m_sRiverShpFile.c_str(),true);
OGRLayer* olr = ods->GetLayer(0);
//string filter = “RVCD='”+strRVCD+”‘”;
//olr->SetAttributeFilter(filter.c_str());
olr->ResetReading();
OGRFeature* ofe = new OGRFeature(olr->GetLayerDefn());
olr->CreateFeature(ofe);
if (ofe!=NULL)
{
ofe->SetField(“RVCD”,Restr.c_str());
olr->SetFeature(ofe);
}

OGRDataSource::DestroyDataSource(ods);
OGRCleanupAll();

OGRSFDriver* driver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(pszFormat);
OGRDataSource* source = driver->Open(m_sRiverShpFile.c_str(),true);
OGRLayer* layer = source->GetLayer(0);
layer->ResetReading();
OGRFeature* feature = new OGRFeature(layer->GetLayerDefn());//追加新纪录
layer->CreateFeature(feature);
feature->SetField(“RVCD”,Restr.c_str());
layer->SetFeature(feature);
OGRDataSource::DestroyDataSource(source);
OGRCleanupAll();

转载自:https://blog.csdn.net/zk23121/article/details/8979250

You may also like...