Location: PHPKode > scripts > PHP GPS Locator > php-gps-locator/cls.nmeaParser.php
<?

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////// CLASS.NMEAPARSER - CULTURAL LUGGAGE ////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

/*		
	NmeaParser Cultural Luggage Class
	Version: 1.0 
	Author: David Boardman
	URL: http://www.netzfunk.org/?usr=d
	Licenced under Creative Commons Attribution-NonCommercial-ShareAlike 2.5

	If redistributed in any form, please include credits and a link to http://www.netzfunk.org/?usr=d
*/

class nmeaParser{

	var $nmea;  // main
	var $ua;	// user agent
	var $gpslog; // gps data log
	var $spec;  // array collettore
	var $collect; // flag stato collettore
	var $config; // variabili di configurazione db
	var $lid;	// connessione db
	var $cnt=0;	// contatore report gps data
	var $loop=60; // loop salvataggio dati (60 = circa 15 secondi --> 4 salvataggi x minuto)
	var $utc_local=-4; // ora locale rispetto a GMT
	
	function getUserAgent(){  //sniffer sistema operativo in uso
		return $this->ua=getenv("HTTP_USER_AGENT");
	}
	
	
	function initWinSerialPort(){ //inizializza porta seriale windows
	//AGGIUNGI FUNZIONI INIT
		return;
	}
	
	
	function initBSDSerialPort(){ //inizializza porta serial Mac + freeBSD
	//AGGIUNGI FUNZIONI INIT
		return;
	}
	
	
	function openWinSerialPort(){  //apri porta seriale windows
		$output=array();
		$port_name="?";
		$baud=19200;
		$parity="";
		$data="";
		$stop="";
		exec("MODE $port_name BAUD=$baud PARITY=$parity DATA=$data STOP=$stop TO=ON OCTS=ON ODSR=OFF IDSR=OFF RTS=HS DTR=ON", $this->gpslog, $res); 
		if($res==0) return $this->gpslog;
		else die("Porta seriale Windows non aperta o già in uso.");
	}
	
	
	function openBSDSerialPort(){  // apri porta seriale Mac + freeBSD
		return $this->gpslog=@fopen("/dev/tty.BTGPS","r") or die("Porta seriale BSD non aperta o già in uso.");
	}


	function closeSerialPort(){  //chiudi porta seriale (generico)
		//return $this->gpslog=@fclose("/dev/tty.BTGPS") or die("Porta seriale BSD non è stata chiusa.");
		return exec("close serial port '/dev/tty.BTGPS'");
	}
	
	function setDataDB(){ //imposta dati connessione database
		$this->config['DB_server']="localhost";  // host del database
		$this->config['DB_name']="cultural_luggage"; // nome del database
		$this->config['DB_user']="root"; // username
		$this->config['DB_pwd']="";  // password
	}


	function connectDB(){  //connessione al db cultural_luggage
		$this->lid = mysql_connect($this->config["DB_server"],$this->config["DB_user"],$this->config["DB_pwd"]);
		mysql_select_db($this->config["DB_name"]);
	}
	
	
	function closeDB(){  //chiudi connessione db
		mysql_close($this->lid);
	}
	
	
	function unsetGpsLog(){  //svuota log gps
		unset($this->gpslog);
	}
	
	
	function unsetNmeaParsed(){ //svuota array log gps parsed
		unset($this->spec);
	}
	
	
	function unsetCollectionData(){  //svuota controllo collezione dati
		unset($this->collect);
	}
	
	
	function nmeaType($input){  // determina tipologia dati gps
		if(eregi("GPGGA",$input)) $type="GPGGA";
		//if(eregi("GPGLL",$input)) $type="GPGLL";
		if(eregi("GPGSA",$input)) $type="GPGSA";
		//if(eregi("GPGSV",$input)) $type="GPGSV";
		if(eregi("GPRMC",$input)) $type="GPRMC";
		if(eregi("GPVTG",$input)) $type="GPVTG";
		return $type;
	}


	function nmeaSwitcher($input){ // switcher per redirect su funzioni specifiche (a seconda del tipo di gps data)
		$type=$this->nmeaType($input);
		$input=substr($input,7,strlen($input)); //elimino la stringa di tipologia dati
		$input=trim(substr($input,0,strlen($input)-5));  //elimino il checksum finale

		switch($type){
	
		// GPGGA
			case "GPGGA":
				$this->GPGGA($input);
			break;
		// GPGLL	
			case "GPGLL":
				$this->GPGLL($input);
			break;

		//GPGSA
			case "GPGSA":
				$this->GPGSA($input);
			break;
		
		//GPGSV (dati non significativi su satelliti)
			case "GPGSV":
				//$this->GPGSV($input);
				return;
			break;
		
		//GPRMC
			case "GPRMC":
				$this->GPRMC($input);
			break;

		//GPVTG
			case "GPVTG":
				$this->GPVTG($input);
			break;

		// DEFAULT			
			default:
				return;
			break;
		}
	}


	function GPGGA($geostr){ // parsing GPGGA
		$split=explode(",",$geostr);
		$this->collect['GPGGA']=true;
		$this->spec['utc']=$this->utc2local($split[0]);
		$this->spec['lat']=$this->raw2lat($split[1]);
		$this->spec['ns']=$split[2];
		$this->spec['long']=$this->raw2long($split[3]);
		$this->spec['ew']=$split[4];
		$this->spec['gpsqual']=$this->gpsQuality($split[5]);
		$this->spec['numsat']=$split[6];
		$this->spec['hdp']=$split[7];
		$this->spec['alt']=$split[8];
		$this->spec['un_alt']=$split[9];
		$this->spec['geoidal']=$split[10];
		$this->spec['un_geoidal']=$split[11];
		$this->spec['dgps']=$split[12];
		$this->spec['diffstat']=$split[13];
	}


	function GPGLL($geostr){ //parsing GPGLL
		$split=explode(",",$geostr);
		$this->collect['GPGLL']=true;
		$this->spec['status']=$this->dataStatus($split[4]);
	}


	function GPGSA($geostr){  //parsing GPGSA
		$split=explode(",",$geostr);
		$this->collect['GPGSA']=true;
		$this->spec['selectmode']=$split[0];
		$this->spec['mode']=$split[1];
		$this->spec['sat1']=$split[2];
		$this->spec['sat2']=$split[3];
		$this->spec['sat3']=$split[4];
		$this->spec['sat4']=$split[5];
		$this->spec['sat5']=$split[6];
		$this->spec['sat6']=$split[7];
		$this->spec['sat7']=$split[8];
		$this->spec['sat8']=$split[9];
		$this->spec['sat9']=$split[10];
		$this->spec['sat10']=$split[11];
		$this->spec['sat11']=$split[12];
		$this->spec['sat12']=$split[13];
		$this->spec['pdop']=$split[14];
		$this->spec['hdop']=$split[15];
		$this->spec['vdop']=$split[16];
	}


	function GPGSV($geostr){  //parsing GPGSV
		$split=explode(",",$geostr);
		$this->collect['GPGSV']=true;
		$this->spec['satmessages']=$split[0];
		$this->spec['messnum']=$split[1];
		$this->spec['satview']=$split[2];
		$this->spec['satnum']=$split[3];
		$this->spec['elevdeg']=$split[4];
		$this->spec['azimuthdeg']=$split[5];
		$this->spec['snr']=$split[6];
	}


	function GPRMC($geostr){  //parsing GPRMC
		$split=explode(",",$geostr);
		$this->collect['GPRMC']=true;
		$this->spec['statusrmc']=$split[1];
		$this->spec['speed']=$split[6];
		$this->spec['track']=$split[7];
		$this->spec['date']=$this->data2db($split[8]);
		$this->spec['magvar']=$split[9];
		$this->spec['mag_ew']=$split[10];
	}


	function GPVTG($geostr){  //parsing GPVTG
		$split=explode(",",$geostr);
		$this->collect['GPVTG']=true;
		$this->spec['trkdeg1']=$split[0];
		$this->spec['t']=$split[1];
		$this->spec['trkdeg2']=$split[2];
		$this->spec['m']=$split[3];
		$this->spec['spdknots']=$spdk=$split[4];
		$this->spec['knots']=$split[5];
		$this->spec['spdkmph']=$split[6];
		$this->spec['kph']=$split[7];
	}


	function getSpecArray(){ // restituisci array spec
		return $this->spec;
	}

	
	function getCollectArray(){ // restituisci array collect
		return $this->collect;
	}

			
	function checkCollectionData(){  // verifica collezione dati (se array completo allora procedi al salvataggio e in seguito allo svuotamento array)
		if(($this->collect['GPGGA']==1)&&($this->collect['GPGSA']==1)&&($this->collect['GPRMC']==1)&&($this->collect['GPVTG']==1)){
			if($this->cnt==$this->loop){ // loop secondi salvataggio db (un record ogni $loop secondi)
				$this->saveGpsData(); 
				$this->unsetCollectionData();
				$this->cnt=0;
			}
		$this->cnt++;
		}
	}
	
	
	function saveGpsData(){ // salva collezione dati su db (se status dati è valido altrimenti skip)
		global $spec;
		if((($this->spec['status']=="Data Valid")||($this->spec['statusrmc']=="A"))&&($this->spec['gpsqual']!="Fix not available")){ // se i dati gps sono validi...
			$this->setDataDB();
			$this->connectDB();
			$_query="INSERT INTO gpstrack(id,data,utc,lat,ns,lon,ew,altitude,geoidal,spd_over_grnd,spdkmph,spdknts,deg_compass,magnetic_var,gps_quality,dgps,dgps_stat,hdp,pdop,hdop,vdop,sat1,sat2,sat3,sat4,sat5,sat6,sat7,sat8,sat9,sat10,sat11,sat12) VALUES(NULL,'".$this->spec['date']."','".$this->spec['utc']."','".$this->spec['lat']."','".$this->spec['ns']."','".$this->spec['long']."','".$this->spec['ew']."','".$this->spec['alt']."','".$this->spec['geoidal']." ".$this->spec['un_geoidal']."','".$this->spec['speed']."','".$this->spec['spdkmph']."','".$this->spec['spdknots']."','".$this->spec['track']."','".$this->spec['magvar']." ".$this->spec['mag_ew']."','".$this->spec['gpsqual']."','".$this->spec['dgps']."','".$this->spec['diffstat']."','".$this->spec['hdp']."','".$this->spec['pdop']."','".$this->spec['hdop']."','".$this->spec['vdop']."','".$this->spec['sat1']."','".$this->spec['sat2']."','".$this->spec['sat3']."','".$this->spec['sat4']."','".$this->spec['sat5']."','".$this->spec['sat6']."','".$this->spec['sat7']."','".$this->spec['sat8']."','".$this->spec['sat9']."','".$this->spec['sat10']."','".$this->spec['sat11']."','".$this->spec['sat12']."');";
			//echo "<hr/>".$_query;
			mysql_query($_query,$this->lid);
			echo $this->reportSaveData();
			$this->unsetNmeaParsed(); // svuoto array dati gps parsed			
		}
	}
	
	function reportSaveData(){
		global $spec;
		$report="<b>".$this->spec['gpsqual']."</b>";
		$report.="<br/> Data: ".$this->spec['date'];
		$report.="<br/> Utc: ".$this->spec['utc'];
		$report.="<br/> Latitude: ".$this->spec['lat']." ".$this->spec['ns'];
		$report.="<br/> Longitude: ".$this->spec['long']." ".$this->spec['ew'];
		$report.="<br/> Altitude: ".$this->spec['alt']." m";
		$report.="<br/> Velocità su terreno: ".$this->spec['speed']." km/h";
		$report.="<br/> Velocità: ".$this->spec['spdkmph']." km/h";
		$report.="<br/> Direzione bussola: ".$this->spec['track']."&deg;";
		$report.="<br/> Separazione geoidale: ".$this->spec['geoidal']." m";
		$report.="<br/> GPS Differenziale: ".$this->spec['dgps']." (Stazione: ".$this->spec['diffstat'].")";		
		$report.="<br/> Diluizione orizzontale di precisione: ".$this->spec['hdp']." m";		
		$report.="<br/> PDOP: ".$this->spec['pdop']." | HDOP: ".$this->spec['hdop']." | VDOP: ".$this->spec['vdop'];
		$report.="<br/> Sat1: ".$this->spec['sat1']." | Sat2: ".$this->spec['sat2']." | Sat3: ".$this->spec['sat3']." | Sat4: ".$this->spec['sat4']." | Sat5: ".$this->spec['sat5']." | Sat6: ".$this->spec['sat6']." | Sat7: ".$this->spec['sat7']." | Sat8: ".$this->spec['sat8']." | Sat9: ".$this->spec['sat9']." | Sat10: ".$this->spec['sat10']." | Sat11: ".$this->spec['sat11']." | Sat12: ".$this->spec['sat12']."";
		$report.="<br/><b><i>LOOP CICLI: ".$this->cnt."</i></b><br/><hr/>";
		return $report;		
	}
	
	
	function raw2lat($deg){ // converti dato grezzo latitudine
		$deg=$this->flushZero($deg);
		$deg1=explode(".",$deg);
		$deg1=$deg1[0];
		$deg1=substr($deg1,0,2);
		$deg2=str_replace(".","",$deg);
		$deg2=substr($deg2,2,strlen($deg2));
		return trim($deg1.".".$deg2);
	}
	

	function raw2long($deg){ // converti dato grezzo longitudine
		$deg=$this->flushZero($deg);
		$deg1=explode(".",$deg);
		$deg1=$deg1[0];
		$deg1=substr($deg1,0,3);
		$deg2=str_replace(".","",$deg);
		$deg2=substr($deg2,3,strlen($deg2));
		return trim($deg1.".".$deg2);
	}
	

	function flushZero($deg){ //elimina 0 non significativi da coordinate
		$flag=true;
		$flushed="";
		for($i=0;$i<strlen($deg);$i++){
			if($deg{$i}!="0"){
				$flag=false;
			}
			if(($deg{$i}=="0")&&($flag==true)){
				$deg{$i}=str_replace("0","",$deg{$i});
			}
		$flushed.=$deg{$i};	
		}	
		$deg="";
		return $flushed;
	}
	
	
	function utc2local($utc){ //converti ora UTC in locale (GMT+1--> fuso amsterdam, parigi, roma, ...)
	//AGGIUNGI FUNZIONE SET_LOCALE
		$utc=explode(".",$utc);
		$utc=$utc[0];
		return $utc;
	}
	
	
	function data2db($dt){ //converti data in formato db
		sscanf($dt, "%2d%2d%2d", $gg, $mm, $aa);
		if(strlen($mm)==1) $mm="0".$mm;
		if(strlen($gg)==1) $gg="0".$gg;
		if(strlen($aa)==1) $aa="0".$aa;
		$dt="20".$aa."-".$mm."-".$gg;
		return $dt;
	}
	
		
	function gpsQuality($qual){  // determina tipologia qualità dati gps in ingresso
		if($qual==0) $gpsqual="Fix not available";
		if($qual==1) $gpsqual="GPS fix";
		if($qual==2) $gpsqual="Differential GPS fix";
		return $gpsqual;	
	}
	
	
	function dataStatus($status){ // determina validità dati gps in ingresso
		if($status=="A") $status="Data Valid";
		if($status=="V") $status="Data Invalid";
		return $status;	
	}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////// FUNZIONI PARTICOLARI RETRIEVE.PHP//////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

	function activeSession(){
		session_start();
		if(!isset($_SESSION["int_user"])){
			$_SESSION["poi_id"] = "";
			$_SESSION["int_user"] = 1;
			$_SESSION["ok"]=0;
		}	
	}
	
	function extractLastRecord(){
		$this->setDataDB();
		$this->connectDB();
		$qtrack="SELECT * FROM gpstrack ORDER BY id DESC;";
		$rtrack=mysql_query($qtrack,$this->lid);
		$ltrack=mysql_fetch_array($rtrack);
		$lat=number_format($ltrack['lat'],5,".",1);
		$ns=$ltrack['ns'];
		$lon=number_format($ltrack['lon'],5,".",1);
		$ew=$ltrack['ew'];
		$alt=$ltrack['altitude'];
		$hdop=$ltrack['hdp'];
		$speeddb=$ltrack['spd_over_grnd'];
		$sat=array("1"=>$ltrack['sat1'],"2"=>$ltrack['sat2'],"3"=>$ltrack['sat3'],"4"=>$ltrack['sat4'],"5"=>$ltrack['sat5'],"6"=>$ltrack['sat6'],"7"=>$ltrack['sat7'],"8"=>$ltrack['sat8'],"9"=>$ltrack['sat9'],"10"=>$ltrack['sat10'],"11"=>$ltrack['sat11'],"12"=>$ltrack['sat12']);
		$satview=$this->satView($sat);
		return array("lat"=>$lat,"ns"=>$ns,"lon"=>$lon,"ew"=>$ew,"altitude"=>$alt,"speed"=>$speeddb,"hdop"=>$hdop,"satview"=>$satview,"qual"=>$qual);
	}
	
	function scanPOI($latdb,$londb){
		$_query="SELECT * FROM poi ORDER BY id DESC;";
		$r=mysql_query($_query,$this->lid);		
		while($l=mysql_fetch_array($r)){
			$idpoi=$l['id'];
			$nome=$l['titolo'];
			$link=$l['link'];
			$x0=$l['x0'];
			$y0=$l['y0'];
			$r0=$l['r'];
			$res=$this->cirCalc($latdb,$londb,$x0,$y0);
			if(($res < $r0)||($res == $r0)){
				$point=array("id"=>$idpoi,"titolo"=>$nome,"link"=>$link,"x0"=>$x0,"y0"=>$y0,"xn"=>$latdb,"yn"=>$londb);
			}
		}
		return $point;
	}

//LONGITUDINE EW X / LATITUDINE NS Y
	function cirCalc($lat,$lon,$x0,$y0){		
		$cal_x=(double)($lon-$x0)*($lon-$x0);
		$cal_y=(double)($lat-$y0)*($lat-$y0);
		$res=sqrt($cal_x+$cal_y);
		return $res;
	}
	
	function checkPoint($pois){
		$cc=count($pois['id']);

		if($cc==0){
			$goto="default.php";
			$idpoi="0";	
		}
		if($cc==1){
			$goto=$pois['link'];
			$idpoi=$pois['id'];
		}
		if($cc>1){
			for($i=0;$i<$cc;$i++){
				$tempx=(($pois[$i]['xn'] - $pois[$i]['x0']) * ($pois[$i]['xn'] - $pois[$i]['x0']));
				$tempy=(($pois[$i]['yn'] - $pois[$i]['y0']) * ($pois[$i]['yn'] - $pois[$i]['y0']));
				$res=sqrt($tempx + $tempy);
				if($i==0){
					$ps=$res;
					$goto=$pois[$i]['link'];
					$idpoi=$pois[$i]['id'];
				}
				elseif(($i>0)&&($res<$ps)){
					$ps=$res;
					$goto=$pois[$i]['link'];
					$idpoi=$pois[$i]['id'];
				}
			}			
		}
		return array("id"=>$idpoi,"link"=>$goto);
	}	
	
	
	function satView($satlist){
		$c=1;
		for($i=0;$i<count($satlist);$i++){
			if($satlist[$i]!="") $c++;
		}
		return $c;	
	}

	function checkRay($r){
		$this->setDataDB();
		$this->connectDB();
		$qtrack="SELECT * FROM poi WHERE r='$r';";
		return mysql_num_rows(mysql_query($qtrack,$this->lid));		
	}

}

?>
Return current item: PHP GPS Locator