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

📄 ellipsedlg.cpp

📁 测绘行业:四种不同参数的椭球面上的几种坐标系的变换
💻 CPP
字号:
// EllipseDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Ellipse.h"
#include "EllipseDlg.h"
#include "DefEllipse.h"

#include<math.h>
#include<stdio.h>
#include<fstream.h>

#define C 100
#define R 206264.806
#define PI  3.14159265358979
double  dfm_hd(double a);

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
	double eb;
	double ea;
class CAboutDlg : public CDialog
{
public:
	CAboutDlg();
	
	// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA
	
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL
	
	// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEllipseDlg dialog

CEllipseDlg::CEllipseDlg(CWnd* pParent /*=NULL*/)
: CDialog(CEllipseDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CEllipseDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	eflag=1;
	Ywj="*.txt";
	Mbwj="*.txt";
}

void CEllipseDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEllipseDlg)
	DDX_Control(pDX, IDC_CBELLIPSE, m_ctlEllipse);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CEllipseDlg, CDialog)
//{{AFX_MSG_MAP(CEllipseDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BLHLxy, OnBLHLxy)
ON_BN_CLICKED(IDC_BLHXYZ, OnBLHXYZ)
ON_BN_CLICKED(IDC_BROWS, OnBrows)
ON_BN_CLICKED(IDC_LxyXYZ, OnLxyXYZ)
ON_BN_CLICKED(IDC_SAVE, OnSave)
ON_BN_CLICKED(IDC_XYZBLH, OnXYZBLH)
ON_BN_CLICKED(IDEXIT, OnExit)
ON_BN_CLICKED(IDC_BUTSTART, OnButstart)
	ON_CBN_SELCHANGE(IDC_CBELLIPSE, OnSelchangeCBEllipse)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEllipseDlg message handlers

BOOL CEllipseDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	
	// Add "About..." menu item to system menu.
	
	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);
	
	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}
	
	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
    strcpy(ellipse1,"1975年国际IUGG椭球");
    strcpy(ellipse2,"克拉索夫斯基椭球");
    strcpy(ellipse3,"WGS-84椭球");
    strcpy(ellipse4,"自定义椭球");
	this->m_ctlEllipse.AddString(ellipse1);
	this->m_ctlEllipse.AddString(ellipse2);
    this->m_ctlEllipse.AddString(ellipse3); 
	this->m_ctlEllipse.AddString(ellipse4);
	this->m_ctlEllipse.SetCurSel(0);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CEllipseDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CEllipseDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting
		
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
		
		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;
		
		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CEllipseDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}




void CEllipseDlg::OnBrows() 
{
	// TODO: Add your control notification handler code here
	CString	m_strFilePathName;
	CFileDialog dlg(TRUE,_T("txt"),_T("*.txt"),OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,	
		_T("文本文件(*.txt)|*.txt|数据文件(*.dat)|*.dat|所有文件(*.*)|*.*|"));
	//构造打开文件对话框
	if(dlg.DoModal( ))		//显示打开文件对话框
	{
		
		m_strFilePathName=dlg.GetPathName( );	//获取文件路径名称
		if(m_strFilePathName!="*.txt")	
			//判断是否在打开文件对话框中选择了取消
			Ywj=m_strFilePathName;
	}	
}
void CEllipseDlg::OnSave() 
{
	// TODO: Add your control notification handler code here
	CString	m_strFilePathName;
	CFileDialog dlg(FALSE,_T("txt"),_T("*.txt"),OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,	
		_T("文本文件(*.txt)|*.txt|"));
	//构造保存文件对话框
	if(dlg.DoModal( ))		//显示打开文件对话框
	{
		
		m_strFilePathName=dlg.GetPathName( );	//获取文件路径名称
		if(m_strFilePathName!="*.txt")	
			//判断是否在打开文件对话框中选择了取消
			Mbwj=m_strFilePathName;
	}		
}

void CEllipseDlg::OnExit() 
{
	// TODO: Add your control notification handler code here
	CDialog::OnCancel();	
}
void CEllipseDlg::OnBLHLxy() 
{
	// TODO: Add your control notification handler code here
	cflag=1;	
}

void CEllipseDlg::OnLxyXYZ() 
{
	// TODO: Add your control notification handler code here
	cflag=2;
}
void CEllipseDlg::OnBLHXYZ() 
{
	// TODO: Add your control notification handler code here
	cflag=3;
}

void CEllipseDlg::OnXYZBLH() 
{
	// TODO: Add your control notification handler code here
	cflag=4;
}


struct ellipse  //椭球参数选择
{
	double a;
	double b;
	double c;
	double e12;
	double e22;
	double V;
	double W;
	double M;
	double N;	 
}; 
void SETellipse(int eflag,double B,ellipse &E)
{
	switch(eflag)
	{
	case 1: E.a=6378140.000;//1975年国际IUGG椭球
		E.b=6356755.2881575287;
		E.c=6399596.6519880105;
		E.e12=0.006694384999588;
		E.e22=0.006739501819473;
		break;	
	case 2: 
		E.a=6378245.000;//克拉索夫斯基椭球
		E.b=6356863.0187730473;
		E.c=6399698.901782711;
		E.e12=0.006693421622966;
		E.e22=0.006738525414683;
		break;

	case 3: E.a=6378137.000;//WGS-84 椭球
		E.b=6356752.3142;
		E.c=6399593.6258;
		E.e12=0.0066943799013;
		E.e22=0.00673949674227;
		break;
	case 4://自定义椭球
		   E.a=ea;
		   E.b=eb;
		   E.c=E.a*E.a/E.b;
           E.e12=1-(E.b/E.a)*(E.b/E.a);
	       E.e22=(E.a/E.b)*(E.a/E.b)-1;
		   break;
	}
	E.V=sqrt(1+E.e22*cos(B)*cos(B));
	E.W=sqrt(1-E.e12*sin(B)*sin(B));
	E.N=E.a/E.W;
	E.M=E.b/E.V;
}

struct dBLH
{
	double  B,L,H;
} BLH[C];
struct dXYZ
{
	double X;
	double Y;
	double Z;
} XYZ[C];
struct dLxy
{
	double  L,x,y;
} Lxy[C];

double  dfm_hd(double a)   /*度分秒(dd.ffmmm)表示的角度化弧度函数*/
{
	int t;double x,y;
	t= int(a);
	x=int((a-t)*100);y=(a-t-x/100)*10000;
	a=(t*3600+x*60+y)/R;
	return a;
}

double  hd_dfm(double a)   /*弧度表示的角度化度分秒(dd.ffmmm)函数*/
{
	int du,fen;double x,miao;
	x=a*180/PI;
	du= int(x);
	fen=int((x-du)*60);
	miao=((x-du)*3600)-fen*60;
	a=du+fen/100+miao/10000;
	return a;
}
//*********************************************
//椭球面坐标间的坐标换算
void BLHLxy(int eflag,dBLH &t,dLxy &a)  //大地坐标到子午面直角坐标cflag=1
{ 
    ellipse q;
    SETellipse(eflag,t.B,q);
	a.L=t.L;
	a.x=q.N*cos(t.B);
	a.y=q.M*sin(t.B);
}

void LxyXYZ(int eflag,dLxy &t,dXYZ &a)//子午面直角坐标到空间直角坐标cflag=2
{
	a.X=t.x*cos(t.L);
	a.Y=t.x*sin(t.L);
	a.Z=t.y;
}

void BLHXYZ(int eflag,dBLH &t,dXYZ  &a)//大地坐标到空间直角坐标cflag=3
{	
	ellipse q;
    SETellipse(eflag,t.B,q);
	a.X=(q.N+(t.H))*cos(t.B)*cos(t.L);
	a.Y=(q.N+(t.H))*cos(t.B)*sin(t.L);
	a.Z=(q.M+(t.H))*sin(t.B);
}

void  XYZBLH(int eflag,dXYZ &t,dBLH &a)//空间直角坐标到大地坐标cflag=4
{
	ellipse q;
    SETellipse(eflag,20,q);//因B未知,先假定一值,设为20弧度 
	double r,p,k,t0,t1,t2,T;
	r=sqrt(t.X*t.X+t.Y*t.Y);
	a.L=acos(t.Y/r);//L为弧度
	t0=t.Z/r;t1=t0;
	p=q.c*q.e12/r;
	k=1+q.e22;
	do
	{
		t2=t0+p*t1/sqrt(k+t1*t1);
		T=t1; t1=t2;
	}
	while(fabs(t2-T)<1e-8);//纬度计算精确到arctan(1e-8)/2弧度
	a.B=atan((t1+t2)/2);//B为弧度
    SETellipse(eflag,a.B,q);//重新计算椭球参数N
	a.H=r/cos(a.B)-q.N;
    hd_dfm(a.B);//弧度表示的B化为度分秒表示
    hd_dfm(a.L);//弧度表示的L化为度分秒表示
}

int Caculate(int eflag,int cflag,CString Ywj,CString Mbwj)
{
	int i=0;int n;
    FILE *fp1;
	fp1=fopen(Ywj,"r");
	if(fp1==NULL)
	{
		printf("无法打开文件");return 0;
	} 
	fscanf(fp1,"%d\n",&n);//点的个数 
    FILE *fp2;
	fp2=fopen(Mbwj,"w");
	switch(cflag)
	{
	case 1:  
        fprintf(fp2,"(B,L,H)到(L,x,y)\n");
		for(i=0;i<n;i++)
		{ 
			fscanf(fp1,"%lf,%lf,%lf\n",&(BLH[i].B),&(BLH[i].L),&(BLH[i].H));	
			BLH[i].B=dfm_hd(BLH[i].B); dLxy a;
			BLHLxy(eflag,BLH[i],a);	
		    fprintf(fp2,"%f,%f,%f\n",a.L,a.x,a.y);//输出为L为度分秒格式
		} 
		break;
	case 2: for(i=0;i<n;i++)
			{ 
                fprintf(fp2,"(L,x,y)到(X,Y,Z)\n");
				fscanf(fp1,"%lf,%lf,%lf\n",&(Lxy[i].L),&(Lxy[i].x),&(Lxy[i].y));	
				Lxy[i].L=dfm_hd(Lxy[i].L); 
				dXYZ a;
				LxyXYZ(eflag,Lxy[i],a);	 
                fprintf(fp2,"%lf,%lf,%lf\n",a.X,a.Y,a.Z);
			} 
		break;
	case 3: for(i=0;i<n;i++)
			{ 
                fprintf(fp2,"(B,L,H)到(X,Y,Z)\n");
				fscanf(fp1,"%lf,%lf,%lf\n",&(BLH[i].B),&(BLH[i].L),&(BLH[i].H));	
				BLH[i].B=dfm_hd(BLH[i].B);BLH[i].L=dfm_hd(BLH[i].L);
				dXYZ a;
				BLHXYZ(eflag,BLH[i],a);	 
				fprintf(fp2,"%lf,%lf,%lf\n",a.X,a.Y,a.Z);
			} 
		break;
	case 4: for(i=0;i<n;i++)
			{ 
                fprintf(fp2,"(X,Y,Z)到(B,L,H)\n");
				fscanf(fp1,"%lf,%lf,%lf\n",&(XYZ[i].X),&(XYZ[i].Y),&(XYZ[i].Z));	
				dBLH a;
				XYZBLH(eflag,XYZ[i],a);
				fprintf(fp2,"%lf,%lf,%lf\n",a.B,a.L,a.H);//输出为B,L为度分秒格式
			} 
		break;
	}
	fclose(fp1);
	return 1;
}

void CEllipseDlg::OnButstart() 
{
	// TODO: Add your control notification handler code here
	if(eflag!=1&&eflag!=2&&eflag!=3&&eflag!=4)
	{	MessageBox("请选择椭球基准!","警告");return;}
	if(cflag!=1&&cflag!=2&&cflag!=3&&cflag!=4)	
	{	MessageBox("请选择坐标变换类型!","警告");return;}
	if(Ywj=="*.txt"||Mbwj=="*.txt")
	{	MessageBox("请选择原坐标文件和目标文件存放位置!","警告");return;}
	if(!Caculate(eflag,cflag,Ywj,Mbwj))
	{	MessageBox("不能打开文件!","警告");return;}
	else
		MessageBox(Mbwj+"文件已保存","保存文件");
}

void CEllipseDlg::OnSelchangeCBEllipse() 
{
	// TODO: Add your control notification handler code here
	CString m_Getstring;
	this->m_ctlEllipse.GetLBText(m_ctlEllipse.GetCurSel(),m_Getstring);
	if(m_Getstring==ellipse1)
		eflag=1;
	else if(m_Getstring==ellipse2)
		eflag=2;
	else if(m_Getstring==ellipse3)
		eflag=3;
	else if(m_Getstring==ellipse4)
	{
		eflag=4;
        DefEllipse  dlg;
		dlg.DoModal();
		ea=dlg.m_ea;
    	eb=dlg.m_eb;
	}	
	UpdateData(false);	
}

⌨️ 快捷键说明

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