经纬高转XYZ

1.入行时候所写,留作纪念

#if 1
#include <stdio.h>
#include <math.h>
#define MAXLEN 150
#define MAXSOLLEN 8191
#define PI          3.1415926535897932  /* pi */
#define D2R         (PI/180.0)          /* deg to rad */
#define RE_WGS84    6378137.0            //长半轴
#define FE_WGS84    (1.0/298.257223563) /* 地球扁率 (WGS84) */
 
extern double str2num(const char *s, int i, int n)
{
	double value;
	char str[256], *p = str;

	if (i<0 || (int)strlen(s)<i || (int)sizeof(str)-1<n) return 0.0;
	for (s += i; *s&&--n >= 0; s++) *p++ = *s == 'd' || *s == 'D' ? 'E' : *s;
	*p = '\0';
	return sscanf(str, "%lf", &value) == 1 ? value : 0.0;
}
int blh2xyz(double *XYZ, double *BLH)
{
	double t = RE_WGS84*(1.0 - FE_WGS84);
	double e = sqrt((RE_WGS84*RE_WGS84 - t*t) / (RE_WGS84*RE_WGS84));
	double Br = BLH[0] * D2R;
	double Lr = BLH[1] * D2R;
	double N = RE_WGS84 / sqrt(1.0 - e*e*sin(Br)*sin(Br));

	XYZ[0] = (N + BLH[2]) * cos(Br)*cos(Lr);
	XYZ[1] = (N + BLH[2]) * cos(Br)*sin(Lr);
	XYZ[2] = (N*(1.0 - e*e) + BLH[2]) * sin(Br);

	return 1;
}
int outXYZ(double GPST,double *BLH, FILE *fp)
{
	double XYZ[3];
	int i,j;
	if (blh2xyz(&XYZ, BLH)>0)
	{
		fprintf(fp, "%.3lf  ", GPST);
		for (i = 0; i < 3; i++)
		{
			fprintf(fp, "%.5lf  ", XYZ[i]);
		}
		fprintf(fp, "\n");
		return 1;
	}
	return 0;
}
int exsess(char *infile, char *outfile)
{
	int i,flag=0;
	char buff[MAXLEN];
	double GPST;
	double BLH[3];
	FILE *in, *out;
	if (!(in=fopen(infile, "rb")))
	{
		printf("打开输入文件错误 ");
	}
	if (!(out = fopen(outfile, "w")))
	{
		printf("打开输出文件错误 ");
	}
	while (fgets(buff, MAXLEN, in))
	{
		if (flag == 1){
			GPST = str2num(buff, 0, 10);
			for (i = 0; i < 3; i++)
			{
				BLH[i] = str2num(buff, 61 + 15 * i, 15);
			}
			outXYZ(GPST,BLH, out);
		}
		if (strstr(buff, "(sec)")){ flag = 1; }
	}
	fclose(in); fclose(out);
	return 1;
}
int main(int argc,char **argv)
{
	char *tfile;
	char *p,name[20];
	char *outfile = {0};
	char *infile = argv[1];
	if (*argv[1])
	{
		tfile = name;
		strcpy(tfile, argv[1]);
		outfile = tfile;
		if ((p = strrchr(outfile, '\0'))) strcpy(p, ".txt");
	}
	if (exsess(infile, outfile)){ printf("转换成功!!"); return 1; }
	else { printf("转换失败!!"); return 0; }
	free(tfile);
	system("pause");
}
#endif


  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值