最近使用开发的过程中出现了一个小问题,顺便记录一下原因和方法--图层设置
在使用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 shapefile.\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 shapefile.\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 shapefile.\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 shapefile.\n" ); exit( 1 ); } OGRFeature::DestroyFeature( poFeature ); OGRDataSource::DestroyDataSource( poDS ); }
文章结束给大家分享下程序员的一些笑话语录:
古鸽是一种搜索隐禽,在中国快绝迹了…初步的研究表明,古鸽的离去,很可能导致另一种长着熊爪,酷似古鸽,却又习性不同的猛禽类——犤毒鸟
---------------------------------
原创文章 By
图层和设置
---------------------------------