<?php

class CGISCatfinder
	{
	var $mlIndex=0;
	var $moCoords=array();
	var $oCatfinder=false;
	var $oGeoCoder=false;
	function gasGetCarCatfinderForSpanData($asSpan,$lCarPK)
		{
		$sQuery="SELECT 
			'".$asSpan["StartTS"]."' AS StartTS, 
			'".$asSpan["EndTS"]."' as EndTS,RemotePK as CatfinderPK,
			tdCatfinder.CFSerial
			FROM 
				tdCarCatfinder,tdCatfinder
			WHERE
				tdCarCatfinder.CatfinderPK=tdCatfinder.CatfinderPK AND 
				UNIX_TIMESTAMP(tdCarCatfinder.StartDate)<".$asSpan["StartTS"]." AND
				(UNIX_TIMESTAMP(tdCarCatfinder.EndDate)>".$asSpan["EndTS"]." OR IFNULL(EndDate,-1)=-1) AND CarPK=".$lCarPK;
		if($oResult=$this->oSystem->oDB->goQuery($sQuery))
			{
			
			return $oResult->gaaGetAllRecords();
			}
		return false;
		}
	function CGISCatfinder(&$oSystem)
		{
		$this->oSystem=&$oSystem;
		$this->oDB=new CDatabase("gis");
		}
	function gbLoadGeoCoder()
		{
		$sFile=PATHFW."GisCatfinder/geocode/class.gis.geocode.php";
		if(!class_exists("CGISGeoCode"))
			{
			if(file_exists($sFile))
				require_once($sFile);
			}
		if(class_exists("CGISGeoCode"))
			{
			$oGeoCoder=new CGISGeoCode($this->oSystem,$this->oDB);
			return $oGeoCoder;
			}
		return false;
		}
	function gLoadCatFinder($oCar)
		{
		$sQuery="SELECT * FROM tdCatFinder WHERE CatfinderPK=".$oCar->GISCatfinderPK;
		if($oResult=$this->oDB->goQuery($sQuery))
			{
			$this->oCatfinder=$oResult->goGetRecord();
			$this->oCatfinder->TITLEBOX=$oCar->QSExternalCarName;
			$this->oCatfinder->ContractPK="NIX";
			return true;
			}
		return false;
		}
	function goGetCatfinderCurrent($lCatfinderPK)
		{
		// erwartet die CatfinderPK in der Positionsdatenbank, entspricht der RemotePK in den anderens Systemen!
		$sQuery="SELECT * FROM tdCatFinder WHERE CatfinderPK=".$lCatfinderPK;
		if($oResult=$this->oDB->goQuery($sQuery))
			{
			if($oCatfinder=$oResult->goGetRecord())
				{
				return $oCatfinder;
				}
			else
				{
	
				}
			}
		return false;
		}
	function gsGetImageVerlauf($lStartTime,$lEndTime,$x,$y)
		{
		$sQuery="SELECT *,SQRT(POWER(".$this->oCatfinder->Longitude."-(LonOL+((LonUR-LonOL)/2)),2)+POWER(".$this->oCatfinder->Latitude."-(LatUR+((LatOL-LatUR)/2)),2)) as LEN1 FROM tdMapFiles WHERE ".$this->oCatfinder->Latitude."<latOL and ".$this->oCatfinder->Latitude.">latUR and ".$this->oCatfinder->Longitude.">lonOL and ".$this->oCatfinder->Longitude."<lonUR ORDER BY LEN1 ASC LIMIT 0,1";
		if($oResult=$this->oDB->goQuery($sQuery))
			{
			$oMap=$oResult->goGetRecord();
			$sTableName="tdPositions_".date("Ymd",$this->oCatfinder->LastGPSTS);
			$sQuery="SELECT * FROM $sTableName WHERE CatfinderPK=".$this->oCatfinder->CatfinderPK." AND GPS_TS>".$lStartTime;
			if($oResult=$this->oDB->goQuery($sQuery))
				{
				$aoTour=$oResult->gaoGetAllRecords();
				}
			
			$oIMG=new CMapImage($this->oCatfinder,$oMap,$x,$y,$aoTour);
			}
		}
	function gsGetImagePositionRAW($asData,$x,$y,$dist=0)		
		{
		$dblLatitude=$asData["Latitude"];
		$dblLongitude=$asData["Longitude"];
		$this->dist=$dist;
		$sQuery="SELECT *,SQRT(POWER(".$dblLongitude."-(LonOL+((LonUR-LonOL)/2)),2)+POWER(".$dblLatitude."-(LatUR+((LatOL-LatUR)/2)),2)) as LEN1 FROM tdMapFiles WHERE ".$dblLatitude."<latOL and ".$dblLatitude.">latUR and ".$dblLongitude.">lonOL and ".$dblLongitude."<lonUR ORDER BY LEN1 ASC LIMIT 0,1";
		
		if($oResult=$this->oDB->goQuery($sQuery))
			{
				
			$oMap=$oResult->goGetRecord();
			$this->oCatfinder->MYKEY=$oGis->mCurrentKEY;
			$asData["TIMEDIST"]=$dist;
			$oIMG=new CMapImage(new CRawPosition($asData),$oMap,$x,$y,false);
			}
		else
			echo mysql_error();
		}
	function gsGetImagePosition($x,$y)
		{
		$sQuery="SELECT *,SQRT(POWER(".$this->oCatfinder->Longitude."-(LonOL+((LonUR-LonOL)/2)),2)+POWER(".$this->oCatfinder->Latitude."-(LatUR+((LatOL-LatUR)/2)),2)) as LEN1 FROM tdMapFiles WHERE ".$this->oCatfinder->Latitude."<latOL and ".$this->oCatfinder->Latitude.">latUR and ".$this->oCatfinder->Longitude.">lonOL and ".$this->oCatfinder->Longitude."<lonUR ORDER BY LEN1 ASC LIMIT 0,1";
		if($oResult=$this->oDB->goQuery($sQuery))
			{
				
			$oMap=$oResult->goGetRecord();
			$this->oCatfinder->MYKEY=$oGis->mCurrentKEY;
			$oIMG=new CMapImage($this->oCatfinder,$oMap,$x,$y,false);
			}
		}
	function gasGetVerlaufsData($lStartTime,$lEndTime)
		{
		$asTableList=$this->gasGetRelevantTables($lStartTime,$lEndTime);
		}
	
	function gaoLoadSpanData($as,$bSavePositions=false)
		{
		if($bSavePositions===true)
			{
			$this->aoPositions=array();
			}
		// $as["StartTS"] [EndTS]
		$aoReturn=array();
		
		if($as[0]["CatfinderPK"]=="132")
		{
			
			
		}
		if($aoResultSets=$this->gasLoadSpan($as))
			{
			foreach($aoResultSets as $sKey=>$oResultSet)
				{
				if($oResultSet["ResultSet"]!==false)
					{
					$oCatfinder=$this->goGetCatfinderCurrent($oResultSet["CatfinderPK"]);
					$oDatahandler=$this->goLoadCatfinderDatahandler($oCatfinder);
					if($oDatahandler->gbSetPositionObjects($oResultSet["ResultSet"]))
						{
						$aoReturn[$sKey]=$oDatahandler->gaoGetSpanData($bSavePositions);
						if($bSavePositions===true)
							{	
							$this->aoPositions=$this->aoPositions+$oDatahandler->aoPositions;	
							}
						}
					unset($oDatahandler);
					}
				else
					{
					$aoReturn[$sKey]=false;
					}
				}
			}
		return $aoReturn;
		}
	function goGetHistogrammData($asSpan)
		{
		
		$aoData=$this->gaoLoadSpanData($asSpan,true); // positionsdaten speichern
		
		$oHistogram=new CHistogrammData($aoData,$this->aoPositions);
		return $oHistogram;
		}
	function gasLoadSpan($asZeiten)
		{
		// asZeiten :: UTC 
		$aoResult=array();
		
		foreach($asZeiten as $sKey=>$asSpan)
			{
			$asQueries=$this->gasGetQueries($asSpan["CatfinderPK"],$asSpan["StartTS"],$asSpan["EndTS"]);
			
			if(is_array($asQueries))
				{
				$sQuery=join("\n UNION \n",$asQueries);
				$sQuery.="ORDER BY GPS_TS";
				
				$aoResult[$sKey]["CatfinderPK"]=$asSpan["CatfinderPK"];
				$aoResult[$sKey]["ResultSet"]=$oResult=$this->oDB->goQuery($sQuery);
				if($aoResult[$sKey]["ResultSet"]===false ||$aoResult[$sKey]["ResultSet"]->glGetNumberOfRecords()==0)
					{
					
					$aoResult[$sKey]["error"]=utf8_encode("Für den Zeitraum vom ".CDateTime::gsGetDateFromUTCStampUser($asSpan["StartTS"])." bis ".CDateTime::gsGetDateFromUTCStampUser($asSpan["EndTS"])." liegen keine Daten vor.");
					}
				}
			}
		if(sizeof($aoResult)==0)
			return false;
		
		return $aoResult;
		}
	function goLoadCatfinderDatahandler($oCatfinder)
		{
		
		$sName=$oCatfinder->DataHandler==""?"Standard":$oCatfinder->DataHandler;
		$sClassName="CGISCatFinderDataHandler".$sName;
		if(!class_exists($sClassName))
			{
			$sFile=PATHFW."GisCatfinder/datahandler/class.".strtolower($sName).".php";
			if(file_exists($sFile))
				require_once($sFile);		
			}
		if(class_exists($sClassName))
			return new $sClassName($this->oSystem,$this->oDB,$oCatfinder);
		return false;
		}
	function gSwap(&$a,&$b)
		{
		$buffer=$a;
		$a=$b;
		$b=$buffer;
		}
	function gasGetQueries($lCatfinderPK,$lStart,$lEnd)
		{
		
		if($lEnd<$lStart)
			$lEnd=$this->gSwap($lStart,$lEnd);	
		if($lEnd>CDateTime::glGetUTCFromTS())
			$lEnd=CDateTime::glGetUTCFromTS();
		$sEnd=date("Ymd",$lEnd);
		$lCurrent=$lStart;
		$sCurrent=date("Ymd",$lCurrent);
		$bBreak=false;
		$i=0;
		while($sCurrent!=$sEnd && $bBreak===false)
			{
			$i++;
			if($i>20) $bBreak=true;
			if($lCatfinderPK==132 || $this->oDB->glGetNumberOfRecords("tdPositionStores","CatfinderPK=".$lCatfinderPK." AND StoreTable='tdPositions_".$sCurrent."'")>0)
				{
				$asReturn[]=$this->getQuery("tdPositions_".$sCurrent,$lCatfinderPK,$lStart,$lEnd);
				}
			else
				{
				}
			$lCurrent=$lCurrent+86400;
			$sCurrent=date("Ymd",$lCurrent);
			}
		if($lCatfinderPK==132 || $this->oDB->glGetNumberOfRecords("tdPositionStores","CatfinderPK=".$lCatfinderPK." AND StoreTable='tdPositions_".$sEnd."'")>0)
			$asReturn[]=$this->getQuery("tdPositions_".$sEnd,$lCatfinderPK,$lStart,$lEnd);
		else
			{
			//mail("thorsten.k@kk-gbr.de","Fehlerx",mysql_error().$this->oDB->msQuery);
			}
		
		return $asReturn;
		}
	function gAddCoord($oCoord)
		{		
		if($this->mlIndex-10>=0)
			unset($this->moCoords[$this->mlIndex-10]);
		$this->moCoords[$this->mlIndex]=$oCoord;
		$this->mlIndex++;
		}
	function getQuery($sTable,$lCatfinderPK,$lStart,$lEnd)
		{
		return "(SELECT *,IF(IFNULL(HDOP,-1)=-1,'false',HDOP) as HDOP,IFNULL(MaxSpeed,Speed) as MaxSpeed,IFNULL(MinSpeed,Speed) as MinSpeed,'".$sTable."' as TableName FROM ".$sTable." WHERE CatfinderPK=".$lCatfinderPK." AND GPS_TS>=".$lStart." AND GPS_TS<=".$lEnd.")";
		}
	function goGetPositionType($oCoord,$oPrev)
		{
		//$this->gAddCoord($oCoord);
		if($oCoord->Speed==0)
			{
			if($oPrev!==false)
				{
				//$o=$this->gbGetStopPunkt();
				return new CPositionType(1,"Stoppunkt\n".$o->dblMax."\n".$o->dblMin."\n".$oCoord->DistanceToPrevious." km\n".sizeof($this->moCoords),$oCoord);
				}
			return new CPositionType(1,"Stoppunkt",$oCoord);
			}
		return new CPositionType(0,"Stoppunkt",$oCoord);
		}
	function gbGetStopPunkt()
		{
		$lGesamt=sizeof($this->moCoords);
		if($lGesamt>0)
			{
			foreach($this->moCoords as $oCoord)
				{
				$sumLat+=$oCoord->Latitude;
				$sumLon+=$oCoord->Longitude;
				}
			$avgLat=$sumLat/$lGesamt;
			$avgLon=$sumLon/$lGesamt;
			$oMinMax=$this->goGetMinMaxDistance($avgLat,$avgLon);
			return $oMinMax;
			}
		}
	function goGetMinMaxDistance($bezugLat,$bezugLon)
		{
		// startbedingungen
		$oCoord=$this->moCoords[$this->mlIndex-1];
		$dblMin=$this->getDistance($oCoord,$bezugLat,$bezugLon);
		$dblMax=$dblMin;
		foreach($this->moCoords as $oCoord)
			{
			$dblDistance=$this->getDistance($oCoord,$bezugLat,$bezugLon);
			$dblMin=$dblMin>$dblDistance?$dblDistance:$dblMin;
			$dblMax=$dblMax<$dblDistance?$dblDistance:$dblMax;
			}
		return new CMinMax($dblMin,$dblMax);			
		}
	function getDistance($oCoord,$dblLat,$dblLon)
		{
		if($oCoord===false)
			return "0";
		$lat1=(double)$oCoord->Latitude;
		$lon1=(double)$oCoord->Longitude;
 		$lat2=(double)$dblLat;
 		$lon2=(double)$dblLon;
		$lEarthRadius=6367.452024;
		$lat1=($lat1/180) * M_PI;
		$lat2=($lat2/180) * M_PI;
		$lon1=($lon1/180)*M_PI;
		$lon2=($lon2/180)*M_PI;
		$distance=acos(sin($lat1)*sin($lat2)+(cos($lat1)*cos($lat2) * cos($lon2-$lon1)))*$lEarthRadius;	
		if((string)$distance=="NAN")
			return 0;
		return $distance;
		}
	}
class CMinMax
	{
	var $dblMin=0;
	var $dblMax=0;
	function CMinMax($dblMin,$dblMax)
		{
		$this->dblMin=$dblMin;
		$this->dblMax=$dblMax;
		}
	}
class CPositionType
	{
	var $sType;
	var $sText;
	var $masPositions=array();
	function CPositionType($sType,$sText,$oCoord,$asPositions=array())
		{
		$this->sType=$sType;
		$this->sText=$sText;
		$this->oCoord=$oCoord;
		$this->masPositions=$asPositions;
		}
	function gbHasPositions()
		{
		// tritt dann ein, wenn ein Stopppunkt verworfen wurde
		return sizeof($this->masPositions)>0;
		}
	function goGetPosition()
		{
		return array_shift($this->masPositions);
		}
	}

class CPunktWolke
	{
	var $oFirstCoord;
	var $mlIndex=0;
	var $masCoords=array();
	function CPunktWolke(&$oCatfinder)
		{
		$this->oCatfinder=$oCatfinder;
		}
	function setFirstCoord($oCoord,$oPrev)
		{
		$this->oFirstCoord=$oCoord;
		$this->oPrevFirstCoord=$oPrev;
		}	
	function setCoord($oCoord)
		{
		$this->masCoords[$this->mlIndex]=$oCoord;
		$this->mlCurrentIndex=$this->mlIndex;
		$this->mlIndex++;
		}
	}	

class CMapImage
{
	function CMapImage($oCatfinder,$oMap,$x,$y,$aoTour)
	{
		global $oSystem;
		$this->oSystem=$oSystem;
		$this->aoTour=$aoTour;
		$this->x=$x;
		$this->y=$y;
		$this->oMap=$oMap;
		$this->gesamtBreit=abs($this->oMap->latUR)-abs($this->oMap->latOL);
		$this->gesamtHoch=abs($this->oMap->lonUR)-abs($this->oMap->lonOL);
		$this->oCatfinder=$oCatfinder;	
		if(strtolower($_ENV["COMPUTERNAME"])=="tk_mobil")
		{
		
			$sMapFile=PATHMAPSERVICES."".substr($oMap->MapFile,0,strrpos($oMap->MapFile,".")).".gif";
			$this->msFontFile=PATHMAPSERVICES."font/arial.ttf";
		}
		else
		{
			$sMapFile=PATHMAPSERVICES."bitmaps/mp/png/".substr($oMap->MapFile,0,strrpos($oMap->MapFile,".")).".png";
			$this->msFontFile=PATHMAPSERVICES."fonts/arial.ttf";
		}
		if(file_exists($sMapFile))
		{
			$this->pImg=imagecreatefromgif($sMapFile);
			$this->putCurrentPos();
		}
		else
		{
		}
	}	
	
	function getyPosition($dblLatitude)
	{
		$olMinSuche=abs($this->oMap->latOL-$dblLatitude);
		return abs($olMinSuche/$this->gesamtBreit*1000);
	}
	function getxPosition($dblLongitude)
	{
		$olMinSuche=abs($this->oMap->lonOL-$dblLongitude);
		return abs($olMinSuche/$this->gesamtHoch*1000);
	}
	function putCurrentPos()
	{
		if($this->aoTour!=false)
		{
			imagesetthickness($this->pImg,3);
			$catColor=imagecolorallocate($this->pImg,255,255,0);
			$oPosPrev=array_shift($this->aoTour);
			foreach($this->aoTour as $oPos)
			{			
				$x1=$this->getxPosition($oPosPrev->Longitude);
				$y1=$this->getyPosition($oPosPrev->Latitude);
				$x2=$this->getxPosition($oPos->Longitude);
				$y2=$this->getyPosition($oPos->Latitude);
				imageline($this->pImg,$x1,$y1,$x2,$y2,$catColor);
				$oPosPrev=$oPos; 
			}
		}
		imagesetthickness($this->pImg,1);	
		$y=$this->getyPosition($this->oCatfinder->Latitude);//  abs($olMinSuche/$gesamtBreit*1000);
		$x=$this->getxPosition($this->oCatfinder->Longitude);
		$colBlack=imagecolorallocate($this->pImg,255,0,0);
		$colWhite=imagecolorallocate($this->pImg,0,0,0);
		/*
		imagearc($this->pImg,$x,$y,10,10 , 0,360,$colBlack);
			imagearc($this->pImg,$x,$y,200,200,0,360,$colBlack);
			imagearc($this->pImg,$x,$y,201,201,0,360,$colWhite);
			imagearc($this->pImg,$x,$y,400,400,0,360,$colBlack);
			imagearc($this->pImg,$x,$y,401,401,0,360,$colWhite);
			imagearc($this->pImg,$x,$y,402,402,0,360,$colBlack);
			imagearc($this->pImg,$x,$y,403,403,0,360,$colWhite);
			imageline($this->pImg,0,$y,999,$y,$colBlack);
			imageline($this->pImg,0,$y+1,999,$y+1,$colBlack);
			imageline($this->pImg,$x,0,$x,999,$colBlack);
			imageline($this->pImg,$x+1,0,$x+1,999,$colBlack);
		*/
		$this->setMessage($this->oCatfinder->TITLEBOX,"",$x,$y);
		
		if($this->x===false)
		{	
			header("Content-Type: image/png");
			imagepng($this->pImg);
		}
		else
		{
			
			$xHalbe=$this->x/2;
			$yHalbe=$this->y/2;
			$im2=imagecreatetruecolor($this->x,$this->y);
			$white=imagecolorallocate($im2,255,255,255);
			imagefilledrectangle($im2,0,0,$this->x,$this->y,$white);
			$xSrc=$x-$xHalbe;
			if($xSrc<0) $xSrc=0;
			$ySrc=$y-$yHalbe;
			if($ySrc<0) $ySrc=0;
			imagecopy($im2,$this->pImg,0,0,$xSrc,$ySrc,$this->x,$this->y);
			$c=imagecolorallocate($im2,0,0,0);
			$w=imagecolorallocate($im2,255,255,255);
			imageline($im2,0,7,0,$this->y-7,$c);
			imageline($im2,7,0,$this->x-7,0,$c);
			imageline($im2,$this->x-1,7,$this->x-1,$this->y-7,$c);
			imageline($im2,7,$this->y-1,$this->x-7,$this->y-1,$c);
			imagearc($im2,7,7,14,14,180,270,$c);
			imagearc($im2,7,$this->y-8,14,14,90,180,$c);
			imagearc($im2,$this->x-8,$this->y-8,14,14,0,90,$c);
			imagearc($im2,$this->x-8,7,14,14,270,0,$c);
			imagefilltoborder($im2,0,0,$c,$w);
			imagefilltoborder($im2,$this->x-1,0,$c,$w);
			imagefilltoborder($im2,$this->x,$this->y,$c,$w);
			imagefilltoborder($im2,0,$this->y,$c,$w);
			imageline($im2,$this->x-4,1,$this->x,1,$w);
			imageline($im2,$this->x-3,2,$this->x,2,$w);
			imageline($im2,$this->x-2,3,$this->x,2,$w);
			imageline($im2,$this->x-1,3,$this->x-1,6,$w);
			$sText="Generiert am ".date("d.m.Y \u\m H:i:s");
			$asSize=imagettfbbox(10,0,$this->msFontFile,$sText);
			
			
			$sWidth=abs($asSize[2])+abs($asSize[0]);
			$sHeight=abs($asSize[5])+abs($asSize[1]);
			
			imagettftext($im2,10,0,$this->x-$sWidth-10,$this->y-$sHeight,$c,$this->msFontFile,$sText);
			header("Content-Type: image/png");
			imagepng($im2);
			
		}
	}
	function setMessage($sTitle,$sMessage,$xPos,$yPos)
	{			
		$yBaseLine=$yPos-40;
		$xBase=$xPos;
		$black=imagecolorallocate($this->pImg,0,0,0);
		$white=imagecolorallocate($this->pImg,255,255,255);
		if($this->x!==false)
		{
			
			$lxFaktor=$this->oCatfinder->Faktor;
			$lyFaktor=$this->oCatfinder->Faktor;
		}
		else
		{
			$lxFaktor=2;
			$lyFaktor=2;
		}
		$asPoly[0]=$xPos;
		$asPoly[1]=$yPos;
		$asPoly[2]=$xPos+(20*$lxFaktor);
		$asPoly[3]=$yPos-(40*$lyFaktor);
		$asPoly[4]=$xPos-(20*$lxFaktor);;
		$asPoly[5]=$yPos-(40*$lyFaktor);
		$asPoly[6]=$xPos-(20*$lxFaktor);;
		$asPoly[7]=$yPos-(80*$lyFaktor);
		$asPoly[8]=$xPos+(100*$lxFaktor);
		$asPoly[9]=$yPos-(80*$lyFaktor);
		$asPoly[10]=$xPos+(100*$lxFaktor);
		$asPoly[11]=$yPos-(40*$lyFaktor);
		$asPoly[12]=$xPos+(40*$lxFaktor);
		$asPoly[13]=$yPos-(40*$lyFaktor);
		$asPoly[14]=$xPos;
		$asPoly[15]=$yPos;
		$white=imagecolorallocate($this->pImg,255,255,255);
		$red=imagecolorallocate($this->pImg,255,0,0);
		$blue=imagecolorallocate($this->pImg,0,0,255);
		imagefilledellipse($this->pImg,$xPos,$yPos,12,12,$red);
		imageellipse($this->pImg,$xPos,$yPos,12,12,$black);
		imagefilledpolygon($this->pImg,$asPoly,8,$white);
		imagepolygon($this->pImg,$asPoly,8,$black);
		$lSize=8*$lyFaktor;
		imagettftext($this->pImg,$lSize,0,$xPos+3-(20*$lxFaktor),$yPos-(68*$lyFaktor),$black,$this->msFontFile,$this->oCatfinder->TITLEBOX);
		if($this->oSystem->oClientData->gvGetVariable("xxx",false)=="CLIENT")
		{
			$sString=CDatetime::gsGetDateFromUTCStamp($this->oCatfinder->LastGPSTS,"\a\m d.m.y H:i:s");
			imagettftext($this->pImg,$lSize,0,$xPos+3-(20*$lxFaktor),$yPos-(56*$lyFaktor),$black,$this->msFontFile,$sString);
			imagettftext($this->pImg,$lSize,0,$xPos+3-(20*$lxFaktor),$yPos-(44*$lyFaktor),$black,$this->msFontFile,"v:".$this->oCatfinder->CurrentSpeed." km/h");
			
		}
		else
		{
			$sString=CDatetime::gsGetDateFromUTCStamp($this->oCatfinder->LastGPSTS,"\a\m d.m.y H:i:s");
			imagettftext($this->pImg,$lSize,0,$xPos+3-(20*$lxFaktor),$yPos-(56*$lyFaktor),$black,$this->msFontFile,$sString);
			imagettftext($this->pImg,$lSize,0,$xPos+3-(20*$lxFaktor),$yPos-(44*$lyFaktor),$black,$this->msFontFile,"v:".$this->oCatfinder->CurrentSpeed." km/h");
			//	imagestring($im2,2,10,10,$this->oCatfinder->MYKEY,$c);
			
		}
	}
}
	
class CPositionObject
	{
	// ist ein interface, b
	var $lStartTS;			#
	var $lStopTS=false;	#
	var $dblLatitude;		#
	var $dblLongitude;		#
	var $sNodeText;		#
	var $lSpeed;		
	var $lMaxSpeed;
	var $lMinSpeed;
	var $lDirection;
	function CPositionObject($oBegin,$oEnd,$lType,$lDistance,$dblLatitude,$dblLongitude,$lDirection,$sNodeText,$lSpeed=false,$lStartTS,$lStopTS=false,$lMaxSpeed=0,$lMinSpeed=0)
		{
		$this->oBeginCoord=$oBegin;
		$this->oEndCoord=$oEnd;
		$this->lStartTS		=$lStartTS;
		$this->lStopTS		=$lStopTS;
		$this->dblLatitude		=$dblLatitude;
		$this->dblLongitude	=$dblLongitude;
		$this->sNodeText		=$sNodeText;
		$this->lSpeed			=$lSpeed;
		$this->lMaxSpeed		=$lMaxSpeed;
		$this->lType			=$lType;
		$this->lMinSpeed		=$lMinSpeed;
		$this->lDirection		=$lDirection;
		$this->DistanceToPrevious=$lDistance;
		
		}
	function glGetLatest()
		{
		if($this->lStopTS!==false)
			{
			
			return $this->lStopTS;
			}
		else
			return $this->lStartTS;
		}
	function glGetFirstTS()
		{
		return $this->lStartTS;
		}
	function glGetLastTS()
		{
		}
	
	}
class CGisMathHelper
	{
	function gsGetDauer($lDurance)
		{
		
		}
	function getDistanceFormCoordLatLon($oCoord,$dblLat,$dblLon)
		{
		if($oCoord===false)
			return "0";
		$lat1=(double)$oCoord->Latitude;
		$lon1=(double)$oCoord->Longitude;
		$lat2=(double)$dblLat;
 		$lon2=(double)$dblLon;
 		return $this->getDistanceFromLatLon($lat1,$lon1,$lat2,$lon2);
		}
	function getDistanceFromCoords($oCoord1,$oCoord2)
		{
		$lat1=(double)$oCoord1->Latitude;
		$lon1=(double)$oCoord1->Longitude;
		$lat2=(double)$oCoord2->Latitude;
 		$lon2=(double)$oCoord2->Longitude;
 		return $this->getDistanceFromLatLon($lat1,$lon1,$lat2,$lon2);
		}
	function getDistanceFromLatLon($lat1,$lon1,$lat2,$lon2)
		{
		$lEarthRadius=6367.452024;
		$lat1=($lat1/180) * M_PI;
		$lat2=($lat2/180) * M_PI;
		$lon1=($lon1/180)*M_PI;
		$lon2=($lon2/180)*M_PI;
		$distance=acos(sin($lat1)*sin($lat2)+(cos($lat1)*cos($lat2) * cos($lon2-$lon1)))*$lEarthRadius;	
		if((string)$distance=="NAN")
			return 0;
		return $distance;
		}
	function gdblGetAngle($oCoord1,$oCoord2,$oCoord3)
		{
		// system auf scheitelpunkt auf 0 beziehen
		
		
		$vektor1=$this->vektorSubstr($oCoord1,$oCoord2);
		$vektor2=$this->vektorSubstr($oCoord3,$oCoord2);
		$betrag1=$this->betrag($vektor1);
		$betrag2=$this->betrag($vektor2);
		if($betrag1==0)
			return false;
		if($betrag2==0)
			return false;
		$cosinusAlpha=$this->skalarProdukt($vektor1,$vektor2)/($betrag1*$betrag2);
		$winkelRadians=acos($cosinusAlpha);
		return rad2deg($winkelRadians);
		}
	function vektorSubstr($oCoord1,$basis)	
		{
		return new CVektor($oCoord1->Latitude-$basis->Latitude,$oCoord1->Longitude-$basis->Longitude);
		}
	function skalarProdukt($oCoord1,$oCoord2)
		{
		return ($oCoord1->Longitude*$oCoord2->Longitude)+($oCoord1->Latitude*$oCoord2->Latitude);
		}
	function betrag($oCoord)
		{
		return sqrt(pow($oCoord->Latitude,2)+pow($oCoord->Longitude,2));
		}
	}
class CVektor
	{
	var $Latitude;
	var $Longitude;
	function CVektor($Latitude,$Longitude)
		{
		$this->Latitude=$Latitude;
		$this->Longitude=$Longitude;
		}
	}
	
class CHistogrammData
	{
	function CHistogrammData($aoStopPos,$aoPositions)
		{
		
		$this->aoStopPositions=$aoStopPos;
		$this->aoPositions=$aoPositions;
		}
	}
	
	
class CRawPosition
{
	function CRawPosition($asData)
	{
		$this->Latitude=$asData["Latitude"];
		$this->Longitude=$asData["Longitude"];
		$this->LastGPSTS=$asData["Uhrzeit"];
		$this->CurrentSpeed=$asData["Speed"];
		$this->TITLEBOX=$asData["Title"];
		$this->Faktor=$asData["Faktor"];
		$this->CFSerial=$asData["CFSerial"];
	}
}