Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Saturday, February 26, 2011

PHP Code Sample : Convert GPS / NMEA Lat Long to Decimal Degrees

This piece of code converts NMEA/GPS Lat -Long to Decimal Degrees. Code comments are self explanatory. The code is not graceful, but it works.
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 degrees
//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
$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;
}




Monday, April 5, 2010

Decode AIS String using PHP

This is for all who may want to decode an AIS String but never knew how to do.

For all who don't know what is AIS.. try this link
http://www.imo.org/Safety/mainframe.asp?topic_id=754
http://en.wikipedia.org/wiki/Automatic_ ... ion_System



this code is tested for type 1 messages and further development has been stalled. I had just coded it in my free time on ship. It is scripted in PHP. but you may adapt for c++ or VB.

100% original
//code to decode ais messages
/*
? Receives a broadcast message,
? Organises the binary bits of the Message Data into 6-bit strings,
? Converts the 6-bit strings into their representative "valid characters" – see IEC 61162-1,
table 7,
? Assembles the valid characters into an encapsulation string, and
? Transfers the encapsulation string using the VDM sentence formatter.

sample message
!AIVDM,1,1,,A,1P000Oh1IT1svTP2r:43grwb0Eq4,0*01

*/
$ais = "1P000Oh1IT1svTP2r:43grwb0Eq4";
$aisdata168=NULL;//six bit array of ascii characters
$ais_nmea_array = str_split($ais);
foreach ($ais_nmea_array as $value)
{
$dec=ascii_2_dec($value);
$bit8=asciidec_2_8bit($dec);
$bit6=dec_2_6bit($bit8);
echo $value ."-" .$bit6 ."
";
$aisdata168 .=$bit6;
}
echo $aisdata168 . "
";

echo "mmsi= " . bindec(substr($aisdata168,9,29)) . "
";
echo "cog= " . bindec(substr($aisdata168,116,12))/10 . "
";
echo "sog= " . bindec(substr($aisdata168,50,10))/10 . "
";

function ascii_2_dec($chr)
{
$dec=ord($chr);//get decimal ascii code
$hex=dechex($dec);//convert decimal to hex
return ($dec);
}

function asciidec_2_8bit($ascii)
{
if ($ascii<48){}
else
{
if($ascii>119){}
else
{
if($ascii>87 && $ascii <96){}
else
{
$ascii=$ascii+40;
if ($ascii>128){$ascii=$ascii+32;}
else{$ascii=$ascii+40;}
}
}
}
return ($ascii);
}
function dec_2_6bit($dec)
{
$bin=decbin($dec);
return(substr($bin, -6));
}