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

16 篇文章 6 订阅

有人认为新建多个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同目录下
	char **papszOptions = (char **)CPLCalloc(sizeof(char *),3);
	papszOptions[0] = "HEADER=header.dxf";
	papszOptions[1] = "TRAILER=trailer.dxf";
	poDS = poDriver->CreateDataSource( "out.dxf", papszOptions );//若生成的out.dxf文件正在被CAD打开着,则再次创建时会出错,创建不成功
	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() );

	// 生成一个正方形,放置在"Square"图层
	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", "Square" );
	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", "Triangle" );
	poFeature->SetGeometry( &oTriangle );
	if( poLayer->CreateFeature( poFeature ) != OGRERR_NONE )
	{
		printf( "Failed to create feature in dxffile.\n" );
		exit( 1 );
	}

	// 生成一个菱形,放置在"Triangle"图层
	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", "Triangle" );
	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 );
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值