⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shptovml.cpp

📁 用于地理信息系统专业或空间信息与数字技术专业
💻 CPP
字号:
#include"shptovml.h"
ofstream out;
ifstream in;
int shpToVML::ChangeByteOrder(int num)//交换字节顺序
{
	int a1,a2,a3,a4;
	a1=num<<24;
	a2=(num<<8)&0x00ff0000;
	a3=(num>>8)&0x0000ff00;
	a4=(num>>24)&0x000000ff;
	num=a1+a2+a3+a4;
	return num;
}

void shpToVML::ReadPointShp(){

		double xPoint;
		double yPoint;
		int RecordNumber;
		int ContentLength;
		int shapeType;
		while(!in.eof()){
		   in.read((char *)&RecordNumber,sizeof(int));
		   RecordNumber = ChangeByteOrder (RecordNumber);

		   in.read((char *)&ContentLength,sizeof(int));
		   ContentLength  = ChangeByteOrder (ContentLength);

		   in.read((char*)&shapeType,sizeof(int));
			
		   in.read((char*)&xPoint,sizeof(double));
		   in.read((char*)&yPoint,sizeof(double));
		   
		   out<<"<v:polyline style=\"Z-INDEX:1;LEFT:1;POSITION:absolute;TOP:1\" points=\"";
	
		   out<<xPoint<<","<<yPoint + 50<<","<<xPoint + 1<<","<<yPoint + 51;//调整范围
		   out<<"\" filled=\"t\" fillcolor=\"#eeeeee\"/>";  
		}
		in.close();
		out.close();
}
void shpToVML::ReadLineShp()
{
	int RecordNumber;
	int ContentLength;
	while(!in.eof())
	{
		in.read((char *)&RecordNumber,sizeof(int));
		RecordNumber = ChangeByteOrder (RecordNumber);

		in.read((char *)&ContentLength,sizeof(int));
		ContentLength  = ChangeByteOrder (ContentLength);

		int shapeType;
		double Box[4];
		int NumParts;
		int NumPoints;
		int *Parts;

		in.read((char *)&shapeType,    sizeof(int));

		//读Box
		for(int i=0;i<4;i++)
			in.read((char *)(Box+i), sizeof(double));

		//读NumParts和NumPoints

		in.read((char *)&NumParts, sizeof(int));//子线段个数
		in.read((char *)&NumPoints, sizeof(int));//顶点个数

		//读Parts和Points
		Parts =new int[NumParts];
		for(int i=0;i<NumParts;i++)
			in.read((char*)(Parts+i),sizeof(int));//每个子线段第一个坐标点起始位置
		int pointNum;
		for(int i=0;i<NumParts;i++)
		{
			if(i!=NumParts-1)
				pointNum =Parts[i+1]-Parts[i];

			else
				pointNum =NumPoints-Parts[i];

		  double *PointsX;
		  double *PointsY;

			PointsX =new double[NumPoints];
			PointsY =new double[NumPoints];

			out<<"<v:polyline style=\"Z-INDEX:1;LEFT:1;POSITION:absolute;TOP:1\" points=\"";
			for(int j=0;j<NumPoints;j++)
			{

				in.read((char*)(PointsX+j), sizeof(double));
				in.read((char*)(PointsY+j), sizeof(double));
				out<<PointsX[j]*1000-100500<<","<<-PointsY[j]*1000+50100<<",";//调整范围
			}
			out<<"\" filled=\"t\" fillcolor=\"#eeeeee\"/>";
			delete[] PointsX;
			delete[] PointsY;
		}
		delete[] Parts;
	}
	in.close();
	out.close();
}
void shpToVML::ReadPolygonShp()
{
	int RecordNumber;
	int ContentLength;
	while(!in.eof())
	{
		in.read((char *)&RecordNumber,sizeof(int));
		RecordNumber = ChangeByteOrder (RecordNumber);

		in.read((char *)&ContentLength,sizeof(int));
		ContentLength  = ChangeByteOrder (ContentLength);

		int shapeType;
		double Box[4];
		int NumParts;
		int NumPoints;
		int *Parts;

		in.read((char *)&shapeType,    sizeof(int));

		//读Box
		for(int i=0;i<4;i++)
			in.read((char *)(Box+i), sizeof(double));

		//读NumParts和NumPoints

		in.read((char *)&NumParts, sizeof(int));
		in.read((char *)&NumPoints, sizeof(int));

		//读Parts和Points
		Parts =new int[NumParts];
		for(int i=0;i<NumParts;i++)
			in.read((char*)(Parts+i),sizeof(int));
		//int pointNum;
		for(int i=0;i<NumParts;i++)
		{
			double *PointsX;
			double *PointsY;

			PointsX =new double[NumPoints];
			PointsY =new double[NumPoints];

			out<<"<v:polyline style=\"Z-INDEX:1;LEFT:1;POSITION:absolute;TOP:1\" points=\"";
			for(int j=0;j<NumPoints;j++)
			{
				in.read((char*)(PointsX+j), sizeof(double));
				in.read((char*)(PointsY+j), sizeof(double));
				out<<PointsX[j]*1000-100500<<","<<-PointsY[j]*1000+50100<<",";
			}
			out<<"\" filled=\"t\" fillcolor=\"white\"/>";
			delete[] PointsX;
			delete[] PointsY;
		}
		delete[] Parts;
	}
	in.close();
	out.close();
}

void shpToVML::ReadShp()
{
	//html文件内容
	out.open("soudpt.htm",ios::out | ios::binary | ios::trunc);//如果文件存在则会被删除
	out<<"<HTML xmlns:v>";
	out<<"<STYLE>v\\:*{behavior:url(#default#VML)\;}</STYLE>";
	out<<"<BODY bgcolor=\"#eeeeee\">";
	//html文件内容

	in.open("soudpt.shp",ios::binary);
	if(!in)
	{
		cout<<"read error"<<endl;
		exit(1);
	}

	//读取坐标文件头的内容 开始
	in.read((char *)&FileCode, sizeof(int));
	FileCode  = ChangeByteOrder (FileCode);

	for(int i=0;i<5;i++)
		in.read((char *)&Unused,sizeof(int));

	in.read((char *)&FileLength, sizeof(int));
	FileLength = ChangeByteOrder (FileLength);

	in.read((char *)&Version, sizeof(int));
	in.read((char *)&ShapeType,sizeof(int));
	in.read((char *)&Xmin, sizeof(double));
	in.read((char *)&Ymin, sizeof(double));
	in.read((char *)&Xmax,  sizeof(double));
	in.read((char *)&Ymax, sizeof(double));
	in.read((char *)&Zmin, sizeof(double));
	in.read((char *)&Zmax, sizeof(double));
	in.read((char *)&Mmin, sizeof(double));
	in.read((char *)&Mmax, sizeof(double));
	//读取坐标文件头的内容 结束


	switch(ShapeType)
	{
	case 1:
		shpToVML::ReadPointShp();//读取点状目标的实体信息
		break;
	case 3:
		shpToVML::ReadLineShp();//读取线状目标的实体信息
		break;
	case 5:
		shpToVML::ReadPolygonShp();//读取面状目标的实体信息
		break;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -