PHP Script To Analyse GPS Tracks for Vehicle Movement

Print Friendly, PDF & Email

See the script in action

 park_speed
$parking_flag=0; //Set when vehicle is parked
$motion_flag=0; //set when vehicle is in motion

echo "\n";
echo "\n
		  \n
		  \n
		  \n
		  \n
		  \n
		  \n
		  \n
		  \n
		  \n
		  \n
		  \n
		  \n
		  \n
		  \n
	 \n";

$display_flag=0;
while ($row = @mysql_fetch_assoc($result)){

$row_count++;
$speed=$row['gps_speed'];
$id=$row['id'];
$lat=degree2decimal($row['lat']);//convert to decimal degrees
$lon=degree2decimal($row['lon']);
$gps_time=$row['gps_time'];
$tstamp=strtotime("$date $gps_time"); //gen unix time stamp
$color="#FFFFFF";
$remarks="";
//determine parking boundary line condition
	if ($speed <= $park_speed ){
		$color="#00FF00"; //Default Color
		$display_flag=0;//Default display state		
		
		/* vehicle is considered to be parked if its speed if it is 
		 * below $park_speed for more than $parking_timeout
		 * As soon as a fist parking event is generated save its
		 * time stamp and set the parking flag
		 */
		 if ($stop_flag==0){$move_flag=0;$start_time=$tstamp;$stop_flag=1;}
		 $tdiff=$tstamp-$start_time; //calculate time elapsed since flag raised
		 // Only show parking events whose $tdiff>$parking_timeout
		 
		 if ($stop_flag==1 && $tdiff>=$parking_timeout && $parking_flag==0){
			$display_flag=1;		 
			$remarks="Vehicle Parked";
			$parking_flag=1;
			$motion_flag=0;
			$move_flag=0;
			}
		 		 
		
	}
	
	
	
	
	
	
	
	
	
	
	
//determine vehicle motion boundary line condition
	if ($speed>$park_speed){
		$color="#FF0000"; //Default Color
		$display_flag=0;//Default display state		
		/*Due to gps drift there can be spikes in gps_speed which
		 * may trigger false movement alarm. Hence vehicle is deemed 
		 * to be in motion only when it travels a 
		 * distance >=$dist_trigger
		 * As soon as movement is detected raise save its coordinates
		*/
		if ($move_flag==0){$slat=$lat;$slon=$lon;$move_flag=1;$stop_flag=0;}
		//calculate distance travelled
		$dist=get_distance($slat,$slon,$lat,$lon);
		
		if ($dist>=$dist_trigger && $motion_flag==0){
			$display_flag=1;
			$stop_flag=0;
			$remarks="Vehicle in Motion";
			$motion_flag=1;
			$parking_flag=0;
			
		}
		
		
		
		
		}
		 
	

	if ($display_flag==1){
		echo "\n
			\n
			\n
			\n
			\n
			\n
			\n
			\n
			\n
			\n
			\n
			\n
			\n
			\n
			\n
		\n";
		}
					

} 
echo "
Sl NoTimeStart TimeTimestampTimeDiffStart LatStart LonLatitudeLongitudeSpeedS_FlagM_FlagDistRemarks
$row_count$gps_time$start_time$tstamp$tdiff$slat$slon$lat$lon$speed$stop_flag$move_flag$dist$remarks
\n"; function degree2decimal($deg_coord) { //reference http://www.directionsmag.com/site/latlong-converter //GPS/NMEA fixes are in Degree Minutes.m format //for google maps we need to convert them to decimal degress //sample format of gps 4533.35 is 45 degrees and 33.35 minutes //formula is as follows //Degrees=Degrees //.d = M.m/60 //Decimal Degrees=Degrees+.d //first we need to split the input value into Degrees and Minutes.m $dot_pos=strpos($deg_coord,'.'); $degree=(int)($deg_coord/100); //simple way $minutes= $deg_coord-($degree*100); $dotdegree=$minutes/60; $decimal=$degree+$dotdegree; $direction=substr($deg_coord,-1); //South latitudes and West longitudes need to return a negative result if (($direction=="S") or ($direction=="W")) { $decimal=$decimal*(-1);} $decimal=number_format($decimal,4,'.',''); //truncate decimal to 4 places return $decimal; } //haversine calculation to find distance between 2 latlon coordinates //Ref: http://stackoverflow.com/questions/27928/how-do-i-calculate-distance-between-two-latitude-longitude-points function get_distance($s_lat,$slon,$e_lat,$e_lon){ $e_radius=6371000; //radius of earth in meters $latdiff=deg2rad(($s_lat-$e_lat)); $londiff=deg2rad(($s_lon-$e_lon)); $a=sin($latdiff/2) * sin($latdiff/2)+ cos(deg2rad($e_lat)) * cos(deg2rad($s_lat)) * sin($londiff/2) * sin($lodiff/2); $c=2 * atan2(sqrt($a),sqrt(1-$a)); $d= $e_radius* $c; return $d; } ?>
Ishan Karve

About Ishan Karve

Ishan Karve is just an every day normal guy next door who happens to be an Electronics Engineer by profession and dabbles with PHP, Javascript, C++ and python. His interests vary as seasons change.. they change from astronomy to soul searching. This site is just a reflection of what he does to keep his mind engaged when he is not occupied by work and family. He is an extremely objective guy and is always ready for some good arguments.. of course over a glass of 40% proof alcohol.
This entry was posted in BASH, Programming and tagged , , , , , , , . Bookmark the permalink.

One Response to PHP Script To Analyse GPS Tracks for Vehicle Movement

  1. Vinode Singh Ujlain says:

    You are reading the GPS tracks from database. Thus either there is a latency (some other code i.e not PHP takes the NMEA tracks realtime & dumps into DB) or the tracks are read offline.
    I am not sure how this can be done , but it would be intresting if somehow the GPS sentences can be read realtime by PHP.
    I am at Pune , drop in for 40% or more alcohol.
    regards – UZI

Leave a Reply

Your email address will not be published. Required fields are marked *