Wechat: yu389741| Email: gisdqy@163.com

Shop:https://www.giserdqy.com/shop

GDAL/OGR 地理要素样式管理器(一)


 很多人对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;
}

运行结果:



转载自:https://blog.csdn.net/langtaosha_a/article/details/17539771