[gdal-dev] OGR API for ESRI Shapefile writer
Even Rouault
even.rouault at mines-paris.org
Mon Jun 29 14:23:23 EDT 2009
Almost perfect, but you've made a classical error : you've just forgotten to
properly close the dataset with OGRDataSource::DestroyDataSource( poDS );
Quoting http://gdal.org/ogr/ogr_apitut.html: "Finally we need to close down
the datasource in order to ensure headers are written out in an orderly way
and all resources are recovered."
I'll mention it in the doc of OGRSFDriver::CreateDataSource() for the sake of
completeness.
Le Monday 29 June 2009 19:54:45 Chandra Shekhar Kumar, vous avez écrit :
> OGRRegisterAll();
>
> OGRSFDriver *poDriver =
> OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName("ESRI Shapefile");
>
>
>
> OGRDataSource *poDS = poDriver->CreateDataSource("point_out.shp",
> 0);
>
>
>
> OGRLayer *poLayer = poDS->CreateLayer("point_out", 0, wkbPoint, 0);
>
>
>
> OGRFieldDefn oField1("Longitude", OFTReal);
>
> OGRFieldDefn oField2("Latitude", OFTReal);
>
> OGRFieldDefn oField3("Speed", OFTReal);
>
>
>
> if(poLayer->CreateField(&oField1) != OGRERR_NONE)
>
> {
>
> std::cout << "creation of Longitude failed" << std::endl;
>
> exit(1);
>
> }
>
> if(poLayer->CreateField(&oField2) != OGRERR_NONE)
>
> {
>
> std::cout << "creation of Latitude failed" << std::endl;
>
> exit(1);
>
> }
>
> if(poLayer->CreateField(&oField3) != OGRERR_NONE)
>
> {
>
> std::cout << "creation of Speed failed" << std::endl;
>
> exit(1);
>
> }
>
>
>
> OGRFeature *poFeature = OGRFeature::CreateFeature(
> poLayer->GetLayerDefn() );
>
> poFeature->SetField("Longitude", 1.1);
>
> poFeature->SetField("Latitude", 2.2);
>
> poFeature->SetField("Speed", 3.3);
>
>
>
> if( poLayer->CreateFeature( poFeature ) != OGRERR_NONE )
>
> {
>
> std::cout << "Failed to create feature in shapefile" <<
> std::endl;
>
> exit( 1 );
>
> }
>
>
>
> OGRFeature::DestroyFeature( poFeature );
>
>
More information about the gdal-dev
mailing list