[Gdal-dev] Error while writing to .shp file.

Vidhiyadharan Nadarajah vdharan2001 at gmail.com
Fri Jul 7 09:02:04 EDT 2006


Hi,

I want to add a feature to an existing polygon layer. Iam getting errors as
follows.

ERROR 3: Error in fseek() or fwrite() writing object to .shp file.
ERROR 4: Failure writing .shp header.

Please find below the code Iam using and indicate me what is wrong.

*********** start********

#include "stdafx.h"
#include "ogrsf_frmts.h"


int main()

{

	OGRRegisterAll();
    OGRDataSource       *poDS;
    poDS =
GRSFDriverRegistrar::Open( "C:\\Projects\\Test\\GDALTest\\shp\\india_ds.shp"
, FALSE );
    if( poDS == NULL )
    {
        printf( "Open failed.\n%s" );
        exit( 1 );
    }

    OGRLayer  *poLayer;
    poLayer = poDS->GetLayerByName( "india_ds" );

    poLayer->ResetReading();
	if (NULL == poLayer)
	 {
		 printf("Layer not found\n");
		 OGRDataSource::DestroyDataSource(poDS);
		 return -1;
	}


    OGRFeature *poFeature = new OGRFeature( poLayer->GetLayerDefn() );

	// set the field
    poFeature->SetField( "State", "123" );

	OGRPoint *poPoint = new OGRPoint();

	OGRLinearRing *poLinearRing = new OGRLinearRing() ;
	poLinearRing->setNumPoints(4);

    poPoint->setX( 13 );
    poPoint->setY( 64 );
	poLinearRing->setPoint(0,poPoint);

    poPoint->setX( 13 );
    poPoint->setY( 66 );
	poLinearRing->setPoint(1,poPoint);

    poPoint->setX( 15 );
    poPoint->setY( 66 );
	poLinearRing->setPoint(2,poPoint);

    poPoint->setX( 15 );
    poPoint->setY( 64 );
	poLinearRing->setPoint(3,poPoint);

	poLinearRing->closeRings();


	OGRPolygon *poOGRPolygon = new OGRPolygon();
	poOGRPolygon->addRingDirectly(poLinearRing);


    if(poFeature->SetGeometryDirectly( poOGRPolygon ) != OGRERR_NONE )
	{
       printf( "Failed to Set Geometry.\n" );
       exit( 1 );

	}

    if( poLayer->CreateFeature( poFeature ) != OGRERR_NONE )
    {
       printf( "Failed to create feature in shapefile.\n" );
       exit( 1 );
    }

    delete poFeature;

    OGRDataSource::DestroyDataSource( poDS );

	return 0;
}

*********** END********

Best Regards,
N.Vidhiyadharan

--
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.394 / Virus Database: 268.9.10/385 - Release Date: 11/07/2006




More information about the Gdal-dev mailing list