[gdal-dev] copy features to empty shp file: extent problem
Emmiolla Emmiolla
emmiolla at gmail.com
Thu Jan 27 04:10:12 EST 2011
Hello, all!
I need to copy features to the empty shp file.
The code is really simple:
const char *pszDriverName = "ESRI Shapefile";
OGRSFDriver *poDriver;
OGRRegisterAll();
poDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(
pszDriverName );
if( poDriver == NULL )
{
printf( "%s driver not available.\n", pszDriverName );
exit( 1 );
}
OGRDataSource *poDS;
poDS = OGRSFDriverRegistrar::Open(
"C:\\ArcProjects\\Untamo\\Test\\test.shp", TRUE );
if( poDS == NULL )
{
printf( "Open failed.\n" );
exit( 1 );
}
OGRLayer *poLayer;
poLayer = poDS->GetLayerByName( "test" );
OGRDataSource *poDS1;
poDS1 = OGRSFDriverRegistrar::Open(
"C:\\ArcProjects\\Untamo\\Test\\arbukle0ot.shp", FALSE );
if( poDS1 == NULL )
{
printf( "Open failed.\n" );
exit( 1 );
}
OGRLayer *featuresLayer;
featuresLayer = poDS1->GetLayerByName( "arbukle0ot" );
OGRFeature *poFeature;
featuresLayer->ResetReading();
while( (poFeature = featuresLayer->GetNextFeature()) != NULL )
{
OGRFeature *newFeature;
newFeature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() );
newFeature->SetField( "ID", 1);
newFeature->SetGeometry(poFeature->GetGeometryRef());
if( poLayer->CreateFeature(newFeature ) != OGRERR_NONE )
{
printf( "Failed to create feature in shapefile.\n" );
exit( 1 );
}
OGRFeature::DestroyFeature( newFeature );
OGRFeature::DestroyFeature( poFeature );
}
OGRDataSource::DestroyDataSource( poDS );
OGRDataSource::DestroyDataSource( poDS1 );
}
arbukle0ot.shp contains features,
test is empty shp file, that is created with ArcCatalog.
Now if you run this code, features will be copied, however the extent of the
shp file will be strange, as the MinX and MinY will be 0 and 0. (however it
should not be, the extent of each feature differs from zero)
The most interesting thing, that if you will take this test.shp, draw some
features in ArcMap, check extent (it will be the right one), than delete
this features to make shp file again empty, and run the code, the extent of
the output file be the correct!
I wonder, what is a problem?
emmiolla
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/gdal-dev/attachments/20110127/2f1cd006/attachment.html
More information about the gdal-dev
mailing list