[Gdal-dev] OGR:GetNextFeature() throws exception in WINXP
Mateusz Łoskot
mateusz at loskot.net
Tue Mar 21 10:57:10 EST 2006
Vidhiyadharan Nadarajah wrote:
> You can down load the shape files from the following link
> http://vdharan.741.com/india_ds.dbf
> http://vdharan.741.com/india_ds.shp
> http://vdharan.741.com/india_ds.shx
>
Hi,
I think I've found the problem.
Your code also gave me exceptions and your program crashed.
I tested with GDAL 1.3.0, compiled using VC++ 2005.
Simply, you've used OGR API incorrectly.
So, here is working version with my fixes, please read big comments
marked with my nickname "mloskot".
//////////////////////////////////////////////////////////////////////////////
#include <ogrsf_frmts.h>
int main()
{
OGRRegisterAll();
OGRDataSource* poDS = NULL;
poDS =
OGRSFDriverRegistrar::Open("D:\\download\\india\\india_ds.shp", FALSE);
if( poDS == NULL )
{
printf( "Open failed.\n%s" );
exit( 1 );
}
OGRLayer* poLayer = NULL;
poLayer = poDS->GetLayerByName( "india_ds" );
OGRFeature *poFeature;
poLayer->ResetReading();
if (NULL == poLayer)
{
printf("Layer not found\n");
OGRDataSource::DestroyDataSource(poDS);
return -1;
}
while( (poFeature = poLayer->GetNextFeature()) != NULL )
{
long fid = poFeature->GetFID();
printf("==================================\n");
printf("FID: %d\n", fid);
OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
if (NULL == poFDefn)
{
// mloskot: HANDLE THIS ERROR AS YOU WISH
printf("\nEEERRROOORRR!\n");
return 0;
}
for(int iField = 0; iField < poFDefn->GetFieldCount(); iField++)
{
OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn( iField );
if( poFieldDefn->GetType() == OFTInteger )
printf( "%d,", poFeature->GetFieldAsInteger( iField ) );
else if( poFieldDefn->GetType() == OFTReal )
printf( "%.3f,", poFeature->GetFieldAsDouble(iField) );
else if( poFieldDefn->GetType() == OFTString )
printf( "%s,", poFeature->GetFieldAsString(iField) );
else
printf( "%s,", poFeature->GetFieldAsString(iField) );
}
OGRGeometry *poGeometry = NULL;
poGeometry = poFeature->GetGeometryRef();
if( poGeometry == NULL)
{
printf("NULL GEOMETRY!!!\n");
}
else
{
if (wkbFlatten(poGeometry->getGeometryType()) == wkbPolygon)
{
// mloskot: I'D CHECK THOSE POINTERS AGAINST NULL
OGRPolygon *poPolygon = (OGRPolygon *)poGeometry ;
OGRLinearRing *poLinearRing = poPolygon->getExteriorRing();
// mloskot:
// WHAT IS THIS CAST FOR???
// This line causes your problems
//OGRPoint *poPoint = (OGRPoint *) poGeometry;
for(int pi = 0; pi < poLinearRing->getNumPoints(); pi++)
{
// mloskot:
OGRPoint point; // CAN NOT BE NULL
// getPoint expects allocated OGRPoint objec to will with coordinates
poLinearRing->getPoint(pi, &point);
printf( "%.3f,%.3f\n", point.getX(), point.getY() );
}
}
else
{
printf( "no polygon geometry\n" );
}
}
}
printf("\n\n");
OGRFeature::DestroyFeature( poFeature );
OGRDataSource::DestroyDataSource( poDS );
return 0;
}
//////////////////////////////////////////////////////////////////////////////
Good Luck!
--
Mateusz Łoskot
http://mateusz.loskot.net
More information about the Gdal-dev
mailing list