//OGR 1.11版本之后,一些geometry fields可以和a feature关联在一起
//这只限于几种文件格式,例如PostGIS
//要创建这样的数据源datasources,首先创建geometry fields,
//同时控件参考系统spatial reference system objects可以和每一个图形字段关联起来
OGRGeomFieldDefn oPointField( "PointField", wkbPoint );OGRSpatialReference *poSRS = new OGRSpatialReference();
poSRS->importFromEPSG( 4326 );
oPointField.SetSpatialRef( poSRS );
poSRS->Release();
if( poLayer->CreateGeomField( &oPointField ) != OGRERR_NONE )
{
printf( "Creating field PointField failed.\n" );
exit( 1 );
}
OGRGeomFieldDefn oFieldPoint2( "PointField2", wkbPoint );
poSRS = new OGRSpatialReference();
poSRS->importFromEPSG( 32631 );
oPointField2.SetSpatialRef( poSRS );
poSRS->Release();
if( poLayer->CreateGeomField( &oPointField2 ) != OGRERR_NONE )
{
printf( "Creating field PointField2 failed.\n" );
exit( 1 );
}
//要将a feature 写进磁盘,首先创建本地OGRFeature,用layer关联的OGRFeatureDefn构造OGRFeature,
//为其设置属性,添加geometries,再写入layer
OGRFeature *poFeature;
OGRGeometry *poGeometry;
char* pszWKT;
poFeature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() );
pszWKT = ( char* ) "POINT(2 49)";
OGRGeometryFactory::createFromWkt( &pszWKT, NULL, &poGeometry );
poFeature->SetGeomFieldDirectly( "PointField", poGeometry );
pszWKT = (char*) "POINT(500000 4500000)";
OGRGeometryFactory::createFromWkt( &pszWKT, NULL, &poGeometry );
poFeature->SetGeomFieldDirectly( "PointField2", poGeometry );
if( poLayer->CreateFeature(poFeature) != OGRERR_NONE )
{
printf( "Failed to create feature.\n" );
exit( 1 );
}
OGRFeature::DestroyFeature( poFeature );