Opened 11 years ago

Closed 11 years ago

#5134 closed defect (invalid)

can't read csv file by using OGRFeature *OGRLayer::GetFeature( long nFID );

Reported by: cpponly2008 Owned by: warmerdam
Priority: normal Milestone:
Component: default Version: unspecified
Severity: normal Keywords:
Cc:

Description

Environment:GDAL 1.9.0/Windows XP/VS2008

If I use OGRFeature *OGRLayer::GetNextFeature( long nFID ) to read csv file, it will return a OGRFeature pointer, but it will get wrong FID by calling pointer->GetFID(), because FID starts with 1,not 0.

If I use OGRFeature *OGRLayer::GetFeature( long nFID ) to read .csv file, it will return a NULL OGRFeature pointer.I debug the gdal source code ,find there is a sentence "if( poFeature->GetFID() == nFID )" in GetFeature. But It works well when I read .shp file.

void testReadCSV()
{
	OGRRegisterAll();
	CPLSetConfigOption("GDAL_FILENAME_IS_UTF8","NO");

	OGRDataSource *poDS = OGRSFDriverRegistrar::Open( "E:\\test.csv", FALSE );

	if( poDS == NULL )
	{
		printf( "Open failed.\n" );
		exit( 1 );
	}
	printf( "OK.\n" );
	long iLayerCount = poDS->GetLayerCount();
	for (long i=0;i <iLayerCount; ++i)
	{
		OGRLayer  *poLayer = poDS->GetLayer(i);

		OGRFeature *poFeature = NULL;

		poLayer->ResetReading();
		long lFeatureCount = poLayer->GetFeatureCount();
		while( (poFeature = poLayer->GetNextFeature()) != NULL )
		{
                        //lFID starts with 1,not 0
			long lFID = poFeature->GetFID();

			OGRFeature::DestroyFeature( poFeature );
		}
		for (long j=0; j<lFeatureCount; ++j)
		{
                        //poFeature will be NULL
			poFeature = poLayer->GetFeature(j);

			OGRFeature::DestroyFeature( poFeature );
		}
	}

	OGRDataSource::DestroyDataSource( poDS );
}

Change History (1)

comment:1 by Even Rouault, 11 years ago

Resolution: invalid
Status: newclosed

The FID in CSV layers starts at 1, so GetFeature(0) will return a NULL pointer. There's no bug there. The convention in the shapefile driver is just different.

Note: See TracTickets for help on using tickets.