首页 > 其他 > 详细

基于GDAL的线数据保存

时间:2015-02-03 21:23:24      阅读:335      评论:0      收藏:0      [点我收藏+]

保存数据的第一步是要把数据解析出来,然后根据GDAL的规则进行数据point类型的shapefile数据生成。大概步骤为:

一、定义保存点要素数据的类

这里定义了两个基类:

//基类,保存要素类型,点、线、面
class Element
{
private:	
	char Type;
	int Code;
public:
	Element(void);
	~Element(void);
	void setType(char Type);
	char getType();	
	void setCode(int Code);
	int getCode();
};

//基类,保存要素中点的XY坐标
class Geometry
{
private:
	double X;
	double Y;
public:
	Geometry(void);
	~Geometry(void);
	void setX(double X);
	double getX();
	void setY(double Y);
	double getY();
};

单个线要素数据的类定义为:

#include "Element.h"
#include "Geometry.h"
#include<vector>
using namespace std;

//单条线
//Code为线的编码
//pointSet为线中点集
class SPolyline :
	public Element,public Geometry
{
private:
	//int Code;	
	vector<Geometry> pointSet;

public:
	SPolyline(void);
	~SPolyline(void);	
	void clearPointSet();
	//void setCode(int Code);
	//int getCode();
	void setPoints(vector<Geometry> pointSet);
	vector<Geometry> getPoints();
	Geometry getPointFromIndex(int index);
	void destroy();
};

在保存线数据时,定义一个list集合

//保存读取的线数据
list<SPolyline> mPolyline;

二、数据解析

三、数据保存

//保存线数据
void savePolyline(char *Filepath,list<SPolyline>& savedData)
{	
	//判断文件是否存在,不存在就进行下面的保存操作,存在就先删除再保存
	fstream f;
	f.open(Filepath,ios::in);

	//文件不存
	if(!f)
	{
		f.close();
		//remove(FilePath);
	}
	//文件存在,删除文件
	else
	{
		f.close();
		remove(Filepath);
	}

	//要保存的shapefile文件名,只保存文件名,不保存路径和.shp
	char FileName[20];  
	char *p=strrchr(Filepath,'\\')+1;
	strcpy(FileName,p);
	int i=0,j=0;
	while(i< 20 &&FileName[i]!='\0' &&FileName[i]!='.')
		i++;
	if(i !=20) FileName[i]='\0';

	//注册OGR
	OGRRegisterAll();
	//定义驱动
	const char *pszDriverName = "ESRI Shapefile";
	OGRSFDriver *poDriver;
	poDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(pszDriverName );
	if( poDriver == NULL )
	{
		printf( "%s 驱动不可用.\n", pszDriverName );
		return;
	}

	OGRDataSource *poDS;
	poDS = poDriver->CreateDataSource( Filepath, NULL );
	if( poDS == NULL )
	{
		printf( "保存文件s%失败.\n",Filepath );
		return;
	}

	//定义图层
	OGRLayer *poLayer;
	poLayer = poDS->CreateLayer(FileName, NULL, wkbLineString, NULL );
	if( poLayer == NULL )
	{
		printf( "创建图层失败.\n" );
		return;
	}

	//创建属性字段
	//定义属性字段code和type
	OGRFieldDefn codeField("code", OFTInteger );
	OGRFieldDefn typeField("type", OFTString );
	//设置code和type字段的宽度
	codeField.SetWidth(32);
	typeField.SetWidth(32);
	//创建字段
	if( poLayer->CreateField( &codeField ) != OGRERR_NONE )
	{
		printf( "创建字段codeField失败.\n" );
		return;
	}
	if( poLayer->CreateField( &typeField ) != OGRERR_NONE )
	{
		printf( "创建字段codeField失败.\n" );
		return;
	}
	
	
	list<SPolyline>::iterator itor;
	for(itor=savedData.begin();itor!=savedData.end();itor++)
	{  
		list<SPolyline>::iterator it = itor;
		//创建第i条线
		OGRFeature *poFeature=OGRFeature::CreateFeature( poLayer->GetLayerDefn() );

		//设置第i条线的属性
		poFeature->SetField("code",itor->getCode());
		//cout<<mPolyline.getLineFromIndex(i).getCode()<<endl;
		poFeature->SetField("type", itor->getType());
		//cout<<mPolyline.getLineFromIndex(i).getType()<<endl;

		OGRLineString *poPolyline = new OGRLineString();
		//第i条线中包含的点数
		int num=static_cast<int>(itor->getPoints().size());
		poPolyline->setNumPoints(num);
		for(j=0;j<num;j++)
		{
			poPolyline->setPoint(j,itor->getPointFromIndex(j).getX(),itor->getPointFromIndex(j).getY());
		}
		poFeature->SetGeometry(poPolyline);
		if( poLayer->CreateFeature( poFeature ) != OGRERR_NONE )
		{
			printf( "创建polyline失败.\n" );
			return;
		}
		savedData.erase(it);
		OGRFeature::DestroyFeature( poFeature );
	}
	OGRDataSource::DestroyDataSource( poDS );
}


基于GDAL的线数据保存

原文:http://blog.csdn.net/longshengguoji/article/details/43457149

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!