Location: PHPKode > scripts > GPX Mapper & CMS > gpx-mapper-cms/cls.gpxBuilder.php
<?

/*		
	GPX Builder 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 gpxBuilder{

	var $config; // parametri di configurazione
	var $lid;	// variabile connessione db
	var $gpxtrackfile; // nome file estrazione GPS track
	var $tracklon; // collector long
	var $tracklat; // collector lat
	var $trackdatetime; // collector datetime
	var $gpxfolder; // folder file data
	var $maxlat;
	var $maxlon;
	var $minlat;
	var $minlon;
	var $idcontext;
	var $titlecontext;
	var $descrcontext;

	var $linkp;
	var $author;
	var $keywords;
	var $creator;	
	
	function gpxBuilder($config,$link,$author,$keywords,$creator,$luogo){
		$this->config=$config;
		$this->gpxfolder=$this->config['gpxfolder'];
		$this->linkp=$link;
		$this->author=$author;
		$this->keywords=$keywords;
		$this->creator=$creator;
		$this->idcontext=$luogo;
	}

	function connectDB(){  //connessione al db 
		$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 makeRefFiles($track){ // genera riferimenti file POI e tracciati
		if($track!="") $this->gpxtrackfile=$track; // nome file estrazione GPS track
	}

	
	function calcR ($lat){  // CALCOLA IN BASE ALLA CURVATURA DELLA TERRA
		/*
		$a = 6378.137;
		$e2 = 0.081082 * 0.081082;
		the radius of curvature of an ellipsoidal Earth in the plane of the
		meridian is given by
		R' = a * (1 - e^2) / (1 - e^2 * (sin(lat))^2)^(3/2)
		where a is the equatorial radius,
		b is the polar radius, and
		e is the eccentricity of the ellipsoid = sqrt(1 - b^2/a^2)
		a = 6378 km (3963 mi) Equatorial radius (surface to center distance)
		b = 6356.752 km (3950 mi) Polar radius (surface to center distance)
		e = 0.081082 Eccentricity
		
		$lat = $lat * M_PI / 180.0;
		$sc = sin ($lat);
		$x = $a * (1.0 - $e2);
		$z = 1.0 - $e2 * $sc * $sc;
		$y = pow ($z, 1.5);
		$r = $x / $y;
		$r = $r * 1000.0;
		return $r;*/
		return $lat;
	}
	
	function buildGPX(){
		$this->setContextData();
	 	$wptlist=$this->extractPOI();
		$tracklist=$this->extractGPSTrack().$this->closeGPX();
		$gpxhead=$this->headerGPX();
		$gpx=$gpxhead.$wptlist.$tracklist;
		$this->saveGPX($gpx);
	}
	
	function setContextData(){
		$q="SELECT * FROM context WHERE id='".$this->idcontext."';";
		$r=mysql_query($q,$this->lid);
		$l=mysql_fetch_array($r);
		$this->titlecontext=$l['titolo'];
		$this->descrcontext=$l['descr'];
	}

	
	function headerGPX(){
		$xml="<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"yes\"?>
		<gpx version=\"1.1\" creator=\"".$this->creator."\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"		xmlns=\"http://www.topografix.com/GPX/1/1\"	 xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd\">
		<metadata>
		<name>".$this->gpxtrackfile."</name>
		<desc>".$this->descrcontext."</desc>
		<author>
		<name>".$this->author."</name>
		<link href=\"".$this->linkp."\">
		<text>".$this->titlecontext."</text>
		</link>
		</author>
		 <time>".$this->setTime()."</time>
		 <keywords><![CDATA[".$this->keywords."]]></keywords>
		 <bounds minlat=\"".$this->calcR($this->minlat)."\" minlon=\"".$this->minlon."\" maxlat=\"".$this->calcR($this->maxlat)."\" maxlon=\"".$this->maxlon."\"/>
		 </metadata>";
		 return $xml;
	}
	
	function closeGPX(){
		return "\n</gpx>";
	}
	
	function extractPOI(){ // estrazione POI da db ---> salvataggio txt
		$q="SELECT * FROM poi WHERE luogo='".$this->idcontext."' ORDER BY id ASC;";
		$r=mysql_query($q,$this->lid);
		$afp="";
		$n=1;
			while($l=mysql_fetch_array($r)){
				$lat=$this->convertCoord($this->calcR($l['lat0']),$l['ns']);
				$lon=$this->convertCoord($l['lon0'],$l['ew']);
				$afp.="<wpt lat=\"$lat\" lon=\"$lon\">\n
					  <ele></ele>\n
					  <time></time>\n
					  <magvar></magvar>\n
					  <geoidheight></geoidheight>\n
					  <name>WPT".$this->addzero(3,$n)."</name>\n
				      <cmt>".$l['titolo']."</cmt>\n
					  <desc>".$l['r']."|".$l['lon0']."|".$this->calcR($l['lat0'])."|".$l['lon1']."|".$this->calcR($l['lat1'])."</desc>\n
					  <src></src>\n
					  <link>".$l['link']."</link>\n
					  <sym>".$this->addzero(3,$n)." ".$l['titolo']."</sym>\n
					  </wpt>\n";
					  $n++;
			}
		return $afp;		
	 }
	 
	 function extractGPSTrack(){  // estrai GPS track e genera file TXT
	     $q="SELECT * FROM gpstrack WHERE luogo='".$this->idcontext."' ORDER BY id ASC;";
		 $r=mysql_query($q,$this->lid);
		 $afp="<rte>\n
		 <name>GPX DRAWER ".strtoupper($this->titlecontext)."</name>\n
		 <desc><![CDATA[".$this->descrcontext."]]></desc>\n
		 <extensions>\n
		 <gpx_style:color>00ffff</gpx_style:color>\n
		 </extensions>\n
		 </rte>\n
		 <trk>\n
		 <name>GPX DRAWER ".strtoupper($this->titlecontext)."</name>\n
		 <desc>".$this->descrcontext."</desc>\n
		 <extensions>\n
		 <gpx_style:color>eeeeaf</gpx_style:color>\n
		 </extensions>\n
		 <trkseg>\n";
		 $n=1;
		 while($l=mysql_fetch_array($r)){
		 	$lat=$this->convertCoord($this->calcR($l['lat']),$l['ns']);
			$lon=$this->convertCoord($l['lon'],$l['ew']);
			$lat_a[]=$lat;
			$lon_a[]=$lon;
			// LAT - LON - DATA UTC - ALTITUDE - SPEEDKMPH
			$afp.="
			<trkpt lat=\"$lat\" lon=\"$lon\">\n
			<ele>".$l['altitude']."</ele>\n
			<time>".$l['datetrkpt']."T".$l['utc']."Z</time>\n
			<magvar>".$l['magnetic_var']."</magvar>\n
			<geoidheight>".$l['geoidal']."</geoidheight>\n
			<name>T-".$this->addZero(6,$n)."</name>\n
			<cmt>Trackpoint ".$this->addZero(6,$n)."</cmt>\n
			<desc>".$l['spd_over_grnd']."|".$l['deg_compass']."</desc>\n 
			<sym>Dot</sym>\n
			<fix>".$l['gps_quality']."</fix>\n
			<sat>".$this->satCount($l['sat1'],$l['sat2'],$l['sat3'],$l['sat4'],$l['sat5'],$l['sat6'],$l['sat7'],$l['sat8'],$l['sat9'],$l['sat10'],$l['sat11'],$l['sat12'])."</sat>\n
			<hdop>".$l['hdop']."</hdop>\n
			<vdop>".$l['vdop']."</vdop>\n
			<pdop>".$l['pdop']."</pdop>\n
			<dpgpsid>".$l['dgps_stat']."</dpgpsid>\n
			</trkpt>\n\n";
			$n++;
		 }
		$this->maxlat=max($lat_a);
		$this->maxlon=max($lon_a);
		$this->minlat=min($lat_a);
		$this->minlon=min($lon_a);
		return $afp."</trkseg>\n</trk>\n";
	 }
	
	
	function saveGPX($gpxfile){
		$f=$this->gpxfolder."/".$this->gpxtrackfile;
		$myfile = @fopen($f, "w+"); // apro il file specificato
		if (!$myfile){
		  $this->text.="Operazione non valida.";
		}
		else{
			@fputs($myfile,$gpxfile); //inserisco i dati estratti
			@fclose($myfile); // chiudo il file
			$this->text.="<h4>GPX assemblato:<br/>".$this->gpxtrackfile."</h4>";
			$this->text.="<br/><a href=\"".$this->config['gpxfolder']."/".$this->gpxtrackfile."\">view gpx file</a>";
		}	
	}
	
	 function convertCoord($coord,$orient){ // converte coordinate South o East
		if(($orient=="S")||($orient=="W")){
			$coord="-".$coord;
		 }
		return $coord;
	 }
	 
	 function satCount($sat1,$sat2,$sat3,$sat4,$sat5,$sat6,$sat7,$sat8,$sat9,$sat10,$sat11,$sat12){
	 	$sat="";
		if(!empty($sat1)) $sat.="1:$sat1,";
		if(!empty($sat2)) $sat.="2:$sat2,";
		if(!empty($sat3)) $sat.="3:$sat3,";
		if(!empty($sat4)) $sat.="4:$sat4,";
		if(!empty($sat5)) $sat.="5:$sat5,";
		if(!empty($sat6)) $sat.="6:$sat6,";
		if(!empty($sat7)) $sat.="7:$sat7,";
		if(!empty($sat8)) $sat.="8:$sat8,";
		if(!empty($sat9)) $sat.="9:$sat9,";
		if(!empty($sat10)) $sat.="10:$sat10,";
		if(!empty($sat11)) $sat.="11:$sat11,";
		if(!empty($sat12)) $sat.="12:$sat12";
		if(substr($sat,strlen($sat)-1,strlen($sat))==",") $sat=substr($sat,0,strlen($sat)-1);
		return $sat;
	 }

	function convertDate($date){ // converti data in UNIX_TIMESTAMP
		$date=str_replace("-",":",$date);
		$d=explode(" ",$date);
		$g=explode(":",$d[0]);
		$h=explode(":",$d[1]);
		$date=mktime($h[0],$h[1],$h[2],$g[1],$g[2],$g[0]);
		return $date;
	}
	
	function setTitle($filename){
		$title=explode("/",$filename);
		$title=$title[count($title)-1];
		$title=explode(".",$title);
		return strtoupper($title[0])."::GPSDRAWER";
	}


	function outputText(){ // stampa testo output
		echo $this->text;
	}
	
	function setTime(){
		$time=date('Y-m-d')."T".date('H:i:s')."Z";
		return $time;
	}
	
	function addZero($num,$int){
			for($i=0;$i<$num;$i++){
				$int="0".$int;
			}
			return $int;
	}
}

?>
Return current item: GPX Mapper & CMS