GDAL/OGR创建DXF文件中多图层的方法

来源:互联网 发布:php页面重定向 编辑:程序博客网 时间:2024/06/01 10:34

在使用GDAL/OGR创建DXF,有时会将点、线、面数据分到到各自的图层中,或者同类别的要素、对象写到DXF文件中不同的图层。如下图:

有人认为新建多个OGRLayer就可以将类别分开,这种观点是错误的,OGRLayer和DXF中的图层不是一个概念。

在写DXF文件时,不支持创建多个ORGLayer图层,一个OGRDXFWriterDS对应一个OGRDXFWriterLayer在new OGRDXFWriterLayer时,OGRDXFWriterLayer构造函数中,会创建DXF标准字段也就是OGRFieldDefn,DXF不支持额外的新建字段:标准字段新建代码(在OGRDXFWriterLayer.cpp中第58-83行):

// 图层字段,设置此字段,将要素分类到,所设置的图层中,不设置默认为0层OGRFieldDefn  oLayerField( "Layer", OFTString );    poFeatureDefn->AddFieldDefn( &oLayerField );// 好像未被使用,不管它    OGRFieldDefn  oClassField( "SubClasses", OFTString );    poFeatureDefn->AddFieldDefn( &oClassField );// 好像未被使用,也不管它    OGRFieldDefn  oExtendedField( "ExtendedEntity", OFTString );    poFeatureDefn->AddFieldDefn( &oExtendedField );// 线类型字段    OGRFieldDefn  oLinetypeField( "Linetype", OFTString );    poFeatureDefn->AddFieldDefn( &oLinetypeField );// 好像未被使用,也不管它    OGRFieldDefn  oEntityHandleField( "EntityHandle", OFTString );    poFeatureDefn->AddFieldDefn( &oEntityHandleField );//文本字段 好像未被使用,也不管它    OGRFieldDefn  oTextField( "Text", OFTString );    poFeatureDefn->AddFieldDefn( &oTextField );// 块名称 写块时设置此字段    OGRFieldDefn  oBlockField( "BlockName", OFTString );    poFeatureDefn->AddFieldDefn( &oBlockField );    // 块缩放 写块时设置此字段    OGRFieldDefn  oScaleField( "BlockScale", OFTRealList );    poFeatureDefn->AddFieldDefn( &oScaleField );    // 块角度 写块时设置此字段    OGRFieldDefn  oBlockAngleField( "BlockAngle", OFTReal );    poFeatureDefn->AddFieldDefn( &oBlockAngleField );


 

下面我们做个试验,看以下创建DXF图层的方法,代码如下:

#include "ogrsf_frmts.h"#include "stdio.h"#include <iostream>#include <string>using namespace std;int main(){const char *pszDriverName = "DXF";OGRSFDriver *poDriver;RegisterOGRDXF();poDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(pszDriverName );if( poDriver == NULL ){printf( "%s driver not available.\n", pszDriverName );exit( 1 );}OGRDataSource *poDS;// 模板文件名称,文件在GDAL中data下,拷贝到与cpp同目录下// 也可以设置GDAL_DATA路径// 按下设置,也可以设置一个正确的相对路径和绝对路径char **papszOptions = (char **)CPLCalloc(sizeof(char *),3);papszOptions[0] = "HEADER=header.dxf";papszOptions[1] = "TRAILER=trailer.dxf";poDS = poDriver->CreateDataSource( "out.dxf", papszOptions );CPLFree(papszOptions);if( poDS == NULL ){printf( "Creation of output file failed.\n" );exit( 1 );}OGRLayer *poLayer;poLayer = poDS->CreateLayer( "hatch_out", NULL, wkbUnknown, NULL );if( poLayer == NULL ){printf( "Layer creation failed.\n" );exit( 1 );}OGRFeature *poFeature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() );// 生成一个正方形,放置在"正方形"图层OGRLinearRing oSquare;oSquare.addPoint(0.0,0.0);oSquare.addPoint(1.0,0.0);oSquare.addPoint(1.0,1.0);oSquare.addPoint(0.0,1.0);oSquare.closeRings();poFeature->SetField( "Layer", "正方形" );poFeature->SetGeometry( &oSquare );if( poLayer->CreateFeature( poFeature ) != OGRERR_NONE ){printf( "Failed to create feature in dxffile.\n" );exit( 1 );}// 生成一个三角形,放置在"三角形"图层poFeature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() );OGRLinearRing oTriangle;oTriangle.addPoint(2.0,2.0);oTriangle.addPoint(3.0,2.0);oTriangle.addPoint(3.0,3.0);oTriangle.closeRings();poFeature->SetField( "Layer", "三角形" );poFeature->SetGeometry( &oTriangle );if( poLayer->CreateFeature( poFeature ) != OGRERR_NONE ){printf( "Failed to create feature in dxffile.\n" );exit( 1 );}// 生成一个菱形,放置在"三角形"图层poFeature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() );OGRLinearRing oRhombus;oRhombus.addPoint(4.0,3.0);oRhombus.addPoint(5.0,4.0);oRhombus.addPoint(4.0,5.0);oRhombus.addPoint(3.0,4.0);oRhombus.closeRings();poFeature->SetField( "Layer", "三角形" );poFeature->SetGeometry( &oRhombus );if( poLayer->CreateFeature( poFeature ) != OGRERR_NONE ){printf( "Failed to create feature in dxffile.\n" );exit( 1 );}// 生成一个多段线,不设置图层,将默认层poFeature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() );OGRLinearRing oLinearRing;oLinearRing.addPoint(5.0,5.0);oLinearRing.addPoint(6.0,4.0);oLinearRing.addPoint(7.0,5.0);oLinearRing.addPoint(8.0,4.0);poFeature->SetGeometry( &oLinearRing );if( poLayer->CreateFeature( poFeature ) != OGRERR_NONE ){printf( "Failed to create feature in dxffile.\n" );exit( 1 );}OGRFeature::DestroyFeature( poFeature );OGRDataSource::DestroyDataSource( poDS );}