很多人对GDAL/OGR已经很熟悉了,GDAL/OGR具有对许多栅格和矢量数据的读写功能,而我接触GDAL已经有1年的时间了,但是对于OGR中如何获得点线面要素的颜色,线宽,线型信息很是陌生。相信很多人也不是太清楚吧。下面代码可以获取字段和坐标信息:
#include "ogrsf_frmts.h" #include <iostream> #include <string> using namespace std; int main() { OGRRegisterAll(); // 对中文路径的支持 CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "NO"); OGRDataSource *poDataSource = OGRSFDriverRegistrar::Open( "G:/5.dxf",TRUE); if( poDataSource == NULL ) { cout << "文件打开失败" << endl; system("pause"); return 0; } OGRLayer *poLayer = poDataSource->GetLayer(0); OGRFeature *poFeature; while( (poFeature = poLayer->GetNextFeature()) != NULL ) { OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn(); int iField; for( iField = 0; iField < poFDefn->GetFieldCount(); iField++ ) { OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn( iField ); if( poFieldDefn->GetType() == OFTInteger ) printf( "%d,", poFeature->GetFieldAsInteger( iField ) ); else if( poFieldDefn->GetType() == OFTReal ) printf( "%.3f,", poFeature->GetFieldAsDouble(iField) ); else if( poFieldDefn->GetType() == OFTString ) printf( "%s,", poFeature->GetFieldAsString(iField) ); else printf( "%s,", poFeature->GetFieldAsString(iField) ); } printf( "\n"); OGRGeometry *poGeometry; poGeometry = poFeature->GetGeometryRef(); if( poGeometry != NULL && wkbFlatten(poGeometry->getGeometryType()) == wkbLineString ) { OGRLineString *ogrLineString = (OGRLineString *)poGeometry; // 得到线串点的数目 int nPointNum = ogrLineString->getNumPoints(); // 线要素的点坐标 for (int i = 0; i < nPointNum; i++ ) { OGRPoint poPoint; ogrLineString->getPoint(i,&poPoint); printf( "%.3f,%3.f\n", poPoint.getX(), poPoint.getY() ); } } else { printf( "no line geometry\n" ); } OGRFeature::DestroyFeature(poFeature); } OGRDataSource::DestroyDataSource( poDataSource ); system("pause"); return 1; }
运行结果:
看上面代码,我认为颜色信息应该也在OGRFeature类中,查看源码,发现OGRFeature中有:
两个有关样式的成员:
char * m_pszStyleString;
OGRStyleTable *m_poStyleTable;
六个有关样式接口函数:
virtual const char *GetStyleString();
virtual void SetStyleString( const char * );
virtual void SetStyleStringDirectly( char * );
virtual OGRStyleTable *GetStyleTable() { return m_poStyleTable; }
virtual void SetStyleTable(OGRStyleTable *poStyleTable);
virtual void SetStyleTableDirectly(OGRStyleTable *poStyleTable)
{ if ( m_poStyleTable ) delete m_poStyleTable;
m_poStyleTable = poStyleTable; }
于是,下一步,在源码中去找到OGRStyleTable的定义。
发现OGRStyleTable定义在ogrfeaturestyle.h头文件中。
在ogrfeaturestyle.h中有7个类:
OGRStyleTable :样式表类
OGRStyleMgr:样式管理器类
OGRStyleTool:样式工具类
继承于OGRStyleTool的四个类:
OGRStylePen:样式画笔类
OGRStyleBrush:样式画刷类
OGRStyleSymbol:样式符号类
OGRStyleLabel:样式标签类
下面我们获取线要素画笔颜色:
int main() { OGRRegisterAll(); // 对中文路径的支持 CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "NO"); OGRDataSource *poDataSource = OGRSFDriverRegistrar::Open( "G:/5.dxf",TRUE); if( poDataSource == NULL ) { cout << "文件打开失败" << endl; system("pause"); return 0; } OGRLayer *poLayer = poDataSource->GetLayer(0); OGRFeature *poFeature; while( (poFeature = poLayer->GetNextFeature()) != NULL ) { OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn(); int iField; for( iField = 0; iField < poFDefn->GetFieldCount(); iField++ ) { OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn( iField ); if( poFieldDefn->GetType() == OFTInteger ) printf( "%d,", poFeature->GetFieldAsInteger( iField ) ); else if( poFieldDefn->GetType() == OFTReal ) printf( "%.3f,", poFeature->GetFieldAsDouble(iField) ); else if( poFieldDefn->GetType() == OFTString ) printf( "%s,", poFeature->GetFieldAsString(iField) ); else printf( "%s,", poFeature->GetFieldAsString(iField) ); } printf( "\n"); OGRGeometry *poGeometry; poGeometry = poFeature->GetGeometryRef(); if( poGeometry != NULL && wkbFlatten(poGeometry->getGeometryType()) == wkbLineString ) { OGRLineString *ogrLineString = (OGRLineString *)poGeometry; /*****************************获取画笔颜色*******************************/ // 样式管理器 OGRStyleMgr oStyleMgr; // 初始化样式管理器 oStyleMgr.InitStyleString(poFeature->GetStyleString()); // 样式工具的个数 int styleToolCount = oStyleMgr.GetPartCount(); for(int i = 0;i < styleToolCount; i++) { OGRStyleTool *pStyleTool = oStyleMgr.GetPart(i); switch(pStyleTool->GetType()) { case OGRSTCNone: { break; } case OGRSTCPen: { OGRStylePen *pStylePen = (OGRStylePen *)pStyleTool; GBool gbool; const char *pzsClr = pStylePen->Color(gbool); int r,g,b,t;// 红、绿、蓝、通明度 pStylePen->GetRGBFromString(pzsClr,r,g,b,t); if(!gbool) // 如果颜色不为空 { printf( "线颜色:R = %d,G = %d,B = %d\n", r,g,b ); } break; } case OGRSTCBrush: { break; } case OGRSTCSymbol: { break; } case OGRSTCLabel: { break; } case OGRSTCVector: { break; } } if(pStyleTool) delete pStyleTool; } /************************************************************************/ // 得到线串点的数目 int nPointNum = ogrLineString->getNumPoints(); // 线要素的点坐标 for (int i = 0; i < nPointNum; i++ ) { OGRPoint poPoint; ogrLineString->getPoint(i,&poPoint); printf( "%.3f,%3.f\n", poPoint.getX(), poPoint.getY() ); } } else { printf( "no line geometry\n" ); } OGRFeature::DestroyFeature(poFeature); } OGRDataSource::DestroyDataSource( poDataSource ); system("pause"); return 1; }
运行结果: