根据GPS数据统计里程数(VC++)

代码片段个人收藏
/************************************************************************/
/* 根据返回的GPS获取里程数                                              */
/************************************************************************/
const double EARTH_RADIUS = 6378.137;
const double PI = 3.1415926;


double rad(double d)
{
 return d * PI / 180.0;
}
//计算两个坐标之间的距离-纬度1,经度1,纬度2,经度2
double GetDistance(double StartLong,double StartLat,double EndLong,double EndLat)//(double lat1, double lng1, double lat2, double lng2)
{
 double radLat1 = rad(EndLat);
 double radLat2 = rad(StartLat);
 double a = radLat1 - radLat2;
 double b = rad(EndLong) - rad(StartLong);
 double s = 2 * asin(sqrt(pow(sin(a/2),2) + cos(radLat1)*cos(radLat2)*pow(sin(b/2),2)));
 s = s * EARTH_RADIUS;
 s= (s*10000)/10000;
 return s;
}
//输入设备号 起始时间 结束时间 间距
char* selectGPSInfo(char* str_id,char* str_start,char*str_end,char*str_jump)
{
	typedef struct s_NV_GPS_FILEHEAD
	{
		char szDeviceID[10]; // 设备ID.
		char szCompanyName[40]; // 公司名称.
		char szVehicleNo[20]; // 车牌号.
		char szDriverName[40]; // 司机姓名.
		char szLineNo[20]; // 线路号.
		char ucFlag; //0:老结构体 1:新结构体
		char Reserved[381]; // 保留字节.
	}s_NV_GPS_FILEHEAD;
	
	
	//GSensor参数
	typedef struct s_NV_GPS_GSensor
	{
		bool bValid; //信息有效性
		float fAccelate_X; //x方向加速度(单位:g)
		float fAccelate_Y; //y方向加速度(单位:g)
		float fAccelate_Z; //z方向加速度(单位:g)
	}s_NV_GPS_GSensor;
	//GyroSensor
	typedef struct s_NV_GPS_GyroSensor
	{
		bool bValid; //信息有效性
		float fCorner; //转角加速度(单位:g)
	}s_NV_GPS_GyroSensor;
	//GPS状态
	typedef struct s_NV_GPS_Status
	{
		bool bValid; //信息有效性
		bool bKeyValid; //车钥匙有效性
		bool bBrakeValid; //刹车信号有效性
		bool bLeftValid; //左转有效性
		bool bRightValid; //右转有效性
		bool bGPSModuleValid; //GPS模块有效性
	}s_NV_GPS_Status;
	//GPS基础信息
	typedef struct s_NV_GPS_BaseInfo
	{
		bool bValid; //信息有效性
		double dbLongitude; //经度(>0:东经,<0:西经)
		double dbLatitude; //纬度(>0:南纬,<0:北纬)
		float fDirect; //方向(0~360°)
		float fSpeed; //速度(千米/小时,负数无效)
	}s_NV_GPS_BaseInfo;
	//GPS扩展信息
	typedef struct s_NV_GPS_ExternInfo
	{
		bool bValid; //信息有效性
		short sHigh; //海拔高度
		char cSatelliteNumber; //卫星数量
		float fTemperature; //温度(摄氏度,>=-10000无效)
	}s_NV_GPS_ExternInfo;
	
	typedef struct s_NV_GPS_All
	{
		s_NV_GPS_BaseInfo stBaseInfo; //基础信息
		s_NV_GPS_Status stStatus; //状态数据
		s_NV_GPS_GSensor stGSensor; //G-Sensor数据
		s_NV_GPS_GyroSensor stGyroSensor; //Gyro-Sensor数据
		s_NV_GPS_ExternInfo stExternInfo; //扩展信息
		char szTime[24]; //时间(如:2010-10-10 10:10:10)
	}s_NV_GPS_All;
	
	s_NV_GPS_FILEHEAD first;
	s_NV_GPS_All second;
	
	CString The_id = str_id;
	CString The_start = str_start;
	CString The_end = str_end;
	CString The_jump = str_jump;
	double jing,wei;     //保存上次的经纬度,以删除重合的点
	int the_last = 0;         //判断第一次的经纬度
	
	
	string the_to_all;
	CString now_time;
	/*number是为了判断传过来的间隔时间的问题*/
	int number = 1;


	
	char *to_all = (char *)malloc(524287*sizeof(char)*3);
	memset(to_all,0,sizeof(to_all));
	/*首先判断起始时间和结束时间,如果起始时间大于结束时间,返回4*/
	if(The_start.Compare(The_end) >= 0)
	{
	//	to_all = "4";
		sprintf(to_all, "%d,", 4);
		return to_all;
	}
	int offset = 0;
	char *p_The_jump = (LPSTR)(LPCTSTR)The_jump;
	/*将间隔时间转化为整形int*/
	int The_jump_time = _ttoi(The_jump);
	
	
	
	/*起始的年月日 -9去掉00:00:00*/
	CString The_start_time ;
	The_start_time = The_start.Left(The_start.GetLength() - 9);
	
	
	
	/*结束的年月日 -9去掉00:00:00*/
	CString The_end_time ;
	The_end_time = The_end.Left(The_end.GetLength() - 9);
	
	
	
	
	//	CString to_search("e://GPSTrack//2013-05-14//72958//2013-05-14.gpx");
	CString to_search("e://GPSTrack//");
	
	
	CString first_day = The_start_time;
	
	int day_num = 1;
	int test2 = 0;
		/*首先判断起始时间和终止时间是否为同一天,如果不是的话就读取多个文件*/
		
		while(first_day.Compare(The_end_time) != 0)
		{
			//WriteLog(File,  Line,"首先判断起始时间和终止时间是否为同一天,如果不是的话就读取多个文件");
			CString to_search("e://GPSTrack//");
			to_search += first_day;
			to_search += "//";
			to_search += The_id;
			to_search += "//";
			to_search += first_day;
			to_search += ".gpx";
			char *p_to_search = (LPSTR)(LPCTSTR)to_search;
			
			
			//WriteLog(File,  Line,(LPSTR)(LPCTSTR)to_search);
			
			FILE*  file1;	
			/*检测传过来的车牌和文件中读取的车牌是否一致*/
			CString check_id;
			
			file1 = fopen(p_to_search,"rb+");	
			if(file1 == NULL)
			{
				
				/*时间加 1 再比较*/
				//CString first_day = "2010-09-30 10:22:01";
				int nYear1, nMonth1, nDate1, nHour1 =0, nMin1=0, nSec1=0;
				sscanf(first_day,"%d-%d-%d",&nYear1,&nMonth1,&nDate1,&nHour1,&nMin1,&nSec1);
				CTime t2(nYear1, nMonth1, nDate1, nHour1, nMin1, nSec1);				
				CTimeSpan ts2(1,0,0,0);//一天
				t2+= ts2;//t1 = 2007-9-1
				first_day = t2.Format("%Y-%m-%d");				
				//WriteLog(File,  Line,"..时间加 1 再比较");				
				continue;
			}
			
			fread(&first,sizeof(first),1,file1);
			check_id = first.szDeviceID;
			
			if(check_id.Compare(The_id))
			{
				
				/*如果读到的车牌号和传过来的参数不一致,返回1*/
				//to_all = "1";
				sprintf(to_all, "%d,", 1);
				return to_all;				
			}	
			
			if(day_num == 1)
			{
				
				
				/*读一回,只有起始时间的限制而没有结束时间*/
				//WriteLog(File,  Line,"读一回,只有起始时间的限制而没有结束时间");
				double jing,wei;
				while(1)
				{
					
					int ret = fread(&second,sizeof(second),1,file1);
					if(ret == 0)
					{
						
						break;
					}
					now_time = second.szTime;
					
					
					if(the_last == 0)
					{
						if(The_start.CompareNoCase(now_time) <= 0)
						{
							
							offset += sprintf(to_all + offset, "%lf,", second.stBaseInfo.dbLongitude);
							offset += sprintf(to_all + offset, "%lf,", second.stBaseInfo.dbLatitude);
							offset += sprintf(to_all + offset, "%f,", second.stBaseInfo.fDirect);
							offset += sprintf(to_all + offset, "%f,", second.stBaseInfo.fSpeed);
							offset += sprintf(to_all + offset, "%d,", second.stExternInfo.sHigh);
							offset += sprintf(to_all + offset, "%d,", second.stExternInfo.cSatelliteNumber);
							offset += sprintf(to_all + offset, "%s;", second.szTime);
							if(offset > 524287*3 -100)
							{
								return to_all;
							}
							jing = second.stBaseInfo.dbLongitude;
							wei = second.stBaseInfo.dbLatitude;
							the_last++;
							
						}
						else
						{
							continue;
						}
					}
					
					if(The_start.CompareNoCase(now_time) <= 0 && second.stExternInfo.cSatelliteNumber > 3 && (jing != second.stBaseInfo.dbLongitude) && (wei != second.stBaseInfo.dbLatitude))
					{
						
						if(number == The_jump_time)
						{
							
							offset += sprintf(to_all + offset, "%lf,", second.stBaseInfo.dbLongitude);
							offset += sprintf(to_all + offset, "%lf,", second.stBaseInfo.dbLatitude);
							offset += sprintf(to_all + offset, "%f,", second.stBaseInfo.fDirect);
							offset += sprintf(to_all + offset, "%f,", second.stBaseInfo.fSpeed);
							offset += sprintf(to_all + offset, "%d,", second.stExternInfo.sHigh);
							offset += sprintf(to_all + offset, "%d,", second.stExternInfo.cSatelliteNumber);
							offset += sprintf(to_all + offset, "%s;", second.szTime);
							if(offset > 524287*3 -100)
							{
								return to_all;
							}
							number = 0;
							
						}				
						number++;
						test2++;
						
					}
					jing = second.stBaseInfo.dbLongitude;
					wei = second.stBaseInfo.dbLatitude;
					
					fflush(NULL);
				}
				
			}
			else  
			{
				//WriteLog(File,  Line,"中间天数");
				while(1)
				{
					int ret = fread(&second,sizeof(second),1,file1);
					if(ret == 0)
					{			
						break;
					}
					now_time = second.szTime;
					
					
					if(second.stExternInfo.cSatelliteNumber > 3 && (jing != second.stBaseInfo.dbLongitude) && (wei != second.stBaseInfo.dbLatitude))
					{
						if(number == The_jump_time)
						{
							offset += sprintf(to_all + offset, "%lf,", second.stBaseInfo.dbLongitude);
							offset += sprintf(to_all + offset, "%lf,", second.stBaseInfo.dbLatitude);
							offset += sprintf(to_all + offset, "%f,", second.stBaseInfo.fDirect);
							offset += sprintf(to_all + offset, "%f,", second.stBaseInfo.fSpeed);
							offset += sprintf(to_all + offset, "%d,", second.stExternInfo.sHigh);
							offset += sprintf(to_all + offset, "%d,", second.stExternInfo.cSatelliteNumber);
							offset += sprintf(to_all + offset, "%s;", second.szTime);
							if(offset > 524287*3 -100)
							{
								return to_all;
							}
							number = 0;
						}
						number++;
						test2++;
						
					}
					
					
					jing = second.stBaseInfo.dbLongitude;
					wei = second.stBaseInfo.dbLatitude;
					fflush(NULL);
				}			
			}
			
			/*时间加 1 再比较*/
			int nYear1, nMonth1, nDate1, nHour1 = 0, nMin1 = 0, nSec1 = 0;
			sscanf(first_day, "%d-%d-%d", &nYear1, &nMonth1, &nDate1, &nHour1, &nMin1, &nSec1);
			CTime t2(nYear1, nMonth1, nDate1, nHour1, nMin1, nSec1);				
			CTimeSpan ts2(1,0,0,0);//一天
			t2+= ts2;//t1 = 2007-9-1
			first_day = t2.Format("%Y-%m-%d");	
			
			day_num++;
			fclose(file1);
	}
	//WriteLog(File,  Line,"首先判断是第一次读还是已经读了几天的,如果是后者,则此次只有结束时间的限制");	
	/*首先判断是第一次读还是已经读了几天的,如果是后者,则此次只有结束时间的限制*/
	if(day_num == 1)               //则是前者
	{
		
		/*直接复制下面的代码*/
		//WriteLog(File,  Line,"只有一天");
		CString to_search("e://GPSTrack//");
		to_search += first_day;
		to_search += "//";
		to_search += The_id;
		to_search += "//";
		to_search += first_day;
		to_search += ".gpx";
		char *p_to_search = (LPSTR)(LPCTSTR)to_search;
		
		
		FILE*  file1;	
		/*检测传过来的车牌和文件中读取的车牌是否一致*/
		CString check_id;
		
		file1 = fopen(p_to_search,"rb+");	
		if(file1 == NULL)
		{
			/*文件存在但打不开,返回3*/
		//	to_all  = "3";	
			sprintf(to_all, "%d,", 3);
			return to_all;
		}
		fread(&first,sizeof(first),1,file1);
		check_id = first.szDeviceID;
		
		if(check_id.Compare(The_id))
		{
			/*如果读到的车牌号和传过来的参数不一致,返回1*/
			to_all = "1";
			return to_all;
		}
		
		while(1)
		{
			int ret = fread(&second,sizeof(second),1,file1);
			if(ret == 0)
			{
				fclose(file1);
				break;
			}
			now_time = second.szTime;
			if(The_end.CompareNoCase(now_time) < 0)
			{
				break;
			}
			
			if(the_last == 0)
			{
	
						if(The_start.CompareNoCase(now_time) <= 0&&(second.stBaseInfo.dbLongitude)>0 &&(second.stBaseInfo.dbLatitude)>0&&second.stExternInfo.cSatelliteNumber > 3)
						{
							
							offset += sprintf(to_all + offset, "%lf,", second.stBaseInfo.dbLongitude);
							offset += sprintf(to_all + offset, "%lf,", second.stBaseInfo.dbLatitude);
							offset += sprintf(to_all + offset, "%f,", second.stBaseInfo.fDirect);
							offset += sprintf(to_all + offset, "%f,", second.stBaseInfo.fSpeed);
							offset += sprintf(to_all + offset, "%d,", second.stExternInfo.sHigh);
							offset += sprintf(to_all + offset, "%d,", second.stExternInfo.cSatelliteNumber);
							offset += sprintf(to_all + offset, "%s;", second.szTime);
							if(offset > 524287*3 -100)
							{
								return to_all;
							}
							jing = second.stBaseInfo.dbLongitude;
							wei = second.stBaseInfo.dbLatitude;
							the_last++;
							
						}
						else
						{
							continue;
						}
			}
			
			if(The_start.CompareNoCase(now_time) <= 0 && The_end.CompareNoCase(now_time) >= 0 && second.stExternInfo.cSatelliteNumber > 3 && (jing != second.stBaseInfo.dbLongitude) && (wei != second.stBaseInfo.dbLatitude))
			{
				if(number == The_jump_time)
				{
					
					offset += sprintf(to_all + offset, "%lf,", second.stBaseInfo.dbLongitude);
					offset += sprintf(to_all + offset, "%lf,", second.stBaseInfo.dbLatitude);
					offset += sprintf(to_all + offset, "%f,", second.stBaseInfo.fDirect);
					offset += sprintf(to_all + offset, "%f,", second.stBaseInfo.fSpeed);
					offset += sprintf(to_all + offset, "%d,", second.stExternInfo.sHigh);
					offset += sprintf(to_all + offset, "%d,", second.stExternInfo.cSatelliteNumber);
					offset += sprintf(to_all + offset, "%s;", second.szTime);	
					printf("5#########%s#######\n",first.szDeviceID);


					if(offset > 524287*3 -100)
					{
						return to_all;
					}
					number = 0;
				}
				
				number++;
				
			}
			jing = second.stBaseInfo.dbLongitude;
			wei = second.stBaseInfo.dbLatitude;
			fflush(NULL);
		}			
		
		}
		else
		{
			//WriteLog(File,  Line,"最后一天");
			CString to_search("e://GPSTrack//");
			to_search += first_day;
			to_search += "//";
			to_search += The_id;
			to_search += "//";
			to_search += first_day;
			to_search += ".gpx";
			
			char *p_to_search = (LPSTR)(LPCTSTR)to_search;
			
			FILE*  file1;	
			/*检测传过来的车牌和文件中读取的车牌是否一致*/
			CString check_id;
			
			/*如果前面几天的数据不存在,最后一天再打不开的话就返回错误码  11*/
			file1 = fopen(p_to_search,"rb+");	
			if(file1 == NULL && test2 == 0)
			{
				
				/*文件存在但打不开,返回3*/
				to_all  = "11";	
				return to_all;
			}
			if(file1 == NULL)
			{			
				return to_all;
			}
			
			fread(&first,sizeof(first),1,file1);
			check_id = first.szDeviceID;
			
			if(check_id.Compare(The_id))
			{
				/*如果读到的车牌号和传过来的参数不一致,返回1*/
				to_all = "1";
				return to_all;
			}
			number = 0;
			
			while(1)
			{
				
				int ret = fread(&second,sizeof(second),1,file1);
				if(ret == 0)
				{
					fclose(file1);
					break;
				}
				now_time = second.szTime;
				if(The_end.CompareNoCase(now_time) < 0)
				{
					break;
				}
				if(The_end.CompareNoCase(now_time) >= 0 && second.stExternInfo.cSatelliteNumber > 3 && (jing != second.stBaseInfo.dbLongitude) && (wei != second.stBaseInfo.dbLatitude))
				{
					
					if(number == The_jump_time)
					{
						offset += sprintf(to_all + offset, "%lf,", second.stBaseInfo.dbLongitude);
						offset += sprintf(to_all + offset, "%lf,", second.stBaseInfo.dbLatitude);
						offset += sprintf(to_all + offset, "%f,", second.stBaseInfo.fDirect);
						offset += sprintf(to_all + offset, "%f,", second.stBaseInfo.fSpeed);
						offset += sprintf(to_all + offset, "%d,", second.stExternInfo.sHigh);
						offset += sprintf(to_all + offset, "%d,", second.stExternInfo.cSatelliteNumber);
						offset += sprintf(to_all + offset, "%s;", second.szTime);
						printf("6#########%s#######\n",first.szDeviceID);
						if(offset > 524287*3 -100)
						{
							return to_all;
						}
						number = 0;
						
					}
					
					number++;
					
				}
				jing = second.stBaseInfo.dbLongitude;
				wei = second.stBaseInfo.dbLatitude;
				fflush(NULL);
			}			
			
			
		}
		
		return to_all;
}


char*GPSinfo2 = selectGPSInfo("80002", "2014-05-23 00:00:00", "2014-05-23 05:00:00", "100");
/************************************************************************/
		/* 插入晚上的数据                                                       */
		/************************************************************************/	
			CString cstr1=GPSinfo_Night.c_str();
			CStringArray GPSALL_N;
			SplitString(cstr1,";",GPSALL_N,TRUE);
			if (GPSALL_N.GetSize()==1)
			{
				continue;
			}


			CStringArray GPSALL_D;
			CString cstr=GPSinfo_Day.c_str();			
			SplitString(cstr,";",GPSALL_D,TRUE);
			printf("数组的长度%d\n",GPSALL_D.GetSize());
			if (GPSALL_D.GetSize()==1)
			{
				printf("错错错");
				continue;
			}
			for(int i=0;i<GPSALL_N.GetSize()-1;++i){
				CStringArray GPSInfo_N;
				SplitString(GPSALL_N[i],",",GPSInfo_N,TRUE);
				gps.setLongitude(GPSInfo_N[0].GetBuffer(0));
				gps.setLatitude(GPSInfo_N[1].GetBuffer(0));
				gps.setDirect(GPSInfo_N[2].GetBuffer(0));
				gps.setSpeed(GPSInfo_N[3].GetBuffer(0));
				gps.setHigh(GPSInfo_N[4].GetBuffer(0));
				gps.setSatelliteNum(GPSInfo_N[5].GetBuffer(0));
				gps.setTime(GPSInfo_N[6].GetBuffer(0));
				
				gpslist_Night.push_back(gps);
				
				
			}
			list<GPS>::iterator it=gpslist_Night.begin();
			
			printf("数组的长度%d\n",GPSALL_N.GetSize());
			printf("链表的长度%d\n",gpslist_Night.size());
			
			double StartPosLa1 =atof((gpslist_Night.front().getLatitude().c_str()));
			double StartPosLo1 =atof((gpslist_Night.front().getLongitude().c_str()));


			printf("设备号是%s \n",s.GetBuffer(0));
			printf("经纬度起始:%lf %lf \n",StartPosLo1,StartPosLa1);
			for(it;it!=gpslist_Night.end();++it){
				
				double EndPosLa1=atof((*it).getLatitude().c_str());
				double EndPosLo1=atof((*it).getLongitude().c_str());
				double speed1=atof((*it).getSpeed().c_str());
				
				//计算距离 
				CarDistance_N=GetDistance(StartPosLa1, StartPosLo1, EndPosLa1, EndPosLo1);
				sum_N += CarDistance_N; 


				
				if(speed1>(double)MINSPEED && speed1<(double)MAXSPEED)
				{
					ValidDistance_N = GetDistance(StartPosLa1, StartPosLo1, EndPosLa1, EndPosLo1);
				}else{
					ValidDistance_N = 0;
				}
				
				ValidSum_N += ValidDistance_N;
				
				StartPosLa1 = EndPosLa1;
				StartPosLo1 = EndPosLo1;
				printf("晚上每一段的距离%f,总距离%f\n",CarDistance_N,sum_N);
			}

=================================================================================================================

程序猿和程序媛必备的咖啡-OneDay咖啡生活-https://shop110384469.taobao.com/



  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值