[Gdal-dev] creating features in empty OGR layers

Marco Hugentobler marco.hugentobler at autoform.ch
Wed Aug 4 10:02:48 EDT 2004


Hi list,

I'd like to program a feature in qgis where a user may create empty vector
layers (e.g. shapefiles). The idea is that later features may be added by
digitizing with the mouse. The creation of the empty layer with OGR works
fine. However, when i reopen the shapefile and try to add features, 
there is a
segfault. The strange thing is that if i add a feature to the layer after
creating it, everything works ok (even reopening the file and adding more
features). Is this a bug or do i make something wrong? As i have read in the
docs that dbfs need at least one attribut, i added a dummy attribute, but it
did not help.
Below are the most important lines of code:

//create the file
OGRSFDriverH myDriverHandle = OGRGetDriverByName( mOutputFormat );
mDataSourceHandle = OGR_Dr_CreateDataSource( myDriverHandle, 
mOutputFileName,
NULL );
OGRSpatialReferenceH mySpatialReferenceSystemHandle = NULL;
QString myWKT = NULL;
mySpatialReferenceSystemHandle = OSRNewSpatialReference( myWKT );
mLayerHandle = OGR_DS_CreateLayer( mDataSourceHandle,
          "contour",
          mySpatialReferenceSystemHandle,
          mGeometryType,
          NULL );


//add a dummy attribute
OGRFieldDefnH myFieldDefinitionHandle = OGR_Fld_Create( theName, theType );
OGR_Fld_SetWidth( myFieldDefinitionHandle, theWidthInt );
OGR_Fld_SetPrecision( myFieldDefinitionHandle, thePrecisionInt );
OGR_L_CreateField( mLayerHandle, myFieldDefinitionHandle, FALSE );
OGR_Fld_Destroy( myFieldDefinitionHandle );

//adding a point feature after this then works fine:
OGRFeatureDefnH fdef=OGR_L_GetLayerDefn(mLayerHandle);
OGRFeatureH fhand= OGR_F_Create( fdef );
OGRGeometryH geometryh=OGR_G_CreateGeometry(wkbPoint);
OGR_G_AddPoint( geometryh, thePoint->x(), thePoint->y(), 0 );
OGR_F_SetGeometryDirectly(fhand, geometryh );
OGR_L_CreateFeature( mLayerHandle, fhand );
OGR_L_SyncToDisk( mLayerHandle );
OGR_F_Destroy( fhand );

//but without adding any features, it is a problem to add features when
//reopening the files:

ogrDataSource = OGRSFDriverRegistrar::Open((const char *) uri,TRUE);
ogrLayer = ogrDataSource->GetLayer(0);
ogrLayer->ResetReading();

//and this is how a point is added (which caused the segfault if no feature
//was inserted previously)

      OGRFeatureDefn* fdef=ogrLayer->GetLayerDefn();
      OGRFeature* feature=new OGRFeature(fdef);
      QGis::WKBTYPE ftype;
      memcpy(&ftype, (f->getGeometry()+1), sizeof(int));
      switch(ftype)
      ...
      case QGis::WKBPoint:
      {
      OGRPoint* p=new OGRPoint();
      p->importFromWkb(f->getGeometry(),1+sizeof(int)+2*sizeof(double));
      feature->SetGeometry(p);
      break;
      }
      ...
      ogrLayer->CreateFeature(feature);
      ogrLayer->SyncToDisk();


unfortunately, i didn't compile OGR with full debugging support, but a
backtrace says:

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 1089679968 (LWP 5241)]
0x40a22747 in DBFGetRecordCount () from /usr/local/lib/libgdal.so.1
(gdb) backtrace
#0  0x40a22747 in DBFGetRecordCount () from /usr/local/lib/libgdal.so.1
#1  0x40a810b7 in OGRShapeLayer::CreateFeature () from 
/usr/local/lib/libgdal.so.1
#2  0x40a758d3 in OGR_L_CreateFeature () from /usr/local/lib/libgdal.so.1
#3  0x415fa875 in QgsShapeFileProvider::addFeature (this=0x0, 
f=0xbfffe440) at
qgsshapefileprovider.cpp:724

the GDAL version i am using is 1.2.0

thanks in advance,
Marco





More information about the Gdal-dev mailing list