Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Format Latitude and Longitude with degrees, minuets and seconds

How can I convert this:

26.72773551940918

Into something like this:

22°12'42"N

The trick here is that the coordinates are, actually Latitude and Longitude, I just need to format them correctly.

like image 669
ragulka Avatar asked Oct 28 '11 09:10

ragulka


People also ask

How do you write latitude and longitude in minutes and seconds?

Start with your line of latitude, writing the degrees, then the minutes, then the seconds. Then, add the North or South as the direction. Then, write a comma followed by your line of longitude in degrees, then minutes, then seconds. Then, add East or West as the direction.


3 Answers

You can find functions to do that here

<?php

function DMStoDEC($deg,$min,$sec)
{

// Converts DMS ( Degrees / minutes / seconds ) 
// to decimal format longitude / latitude

    return $deg+((($min*60)+($sec))/3600);
}    

function DECtoDMS($dec)
{

// Converts decimal longitude / latitude to DMS
// ( Degrees / minutes / seconds ) 

// This is the piece of code which may appear to 
// be inefficient, but to avoid issues with floating
// point math we extract the integer part and the float
// part by using a string function.

    $vars = explode(".",$dec);
    $deg = $vars[0];
    $tempma = "0.".$vars[1];

    $tempma = $tempma * 3600;
    $min = floor($tempma / 60);
    $sec = $tempma - ($min*60);

    return array("deg"=>$deg,"min"=>$min,"sec"=>$sec);
}    

?>
like image 160
SERPRO Avatar answered Oct 08 '22 15:10

SERPRO


The lat/lon coords are written in (roughly speaking) a base-60 numeral system. Here's how you convert them:

function fraction_to_min_sec($coord)
{
  $isnorth = $coord>=0;
  $coord = abs($coord);
  $deg = floor($coord);
  $coord = ($coord-$deg)*60;
  $min = floor($coord);
  $sec = floor(($coord-$min)*60);
  return array($deg, $min, $sec, $isnorth ? 'N' : 'S');
  // or if you want the string representation
  return sprintf("%d&deg;%d'%d\"%s", $deg, $min, $sec, $isnorth ? 'N' : 'S');
}

I say my function has better numerical stability than @SeRPRo's one.

like image 23
Leonid Shevtsov Avatar answered Oct 08 '22 14:10

Leonid Shevtsov


Here's one where you pass in the latitude,longitude in DMS values and returns the converted DMS string. Easy and simple

function DECtoDMS($latitude, $longitude)
{
    $latitudeDirection = $latitude < 0 ? 'S': 'N';
    $longitudeDirection = $longitude < 0 ? 'W': 'E';

    $latitudeNotation = $latitude < 0 ? '-': '';
    $longitudeNotation = $longitude < 0 ? '-': '';

    $latitudeInDegrees = floor(abs($latitude));
    $longitudeInDegrees = floor(abs($longitude));

    $latitudeDecimal = abs($latitude)-$latitudeInDegrees;
    $longitudeDecimal = abs($longitude)-$longitudeInDegrees;

    $_precision = 3;
    $latitudeMinutes = round($latitudeDecimal*60,$_precision);
    $longitudeMinutes = round($longitudeDecimal*60,$_precision);

    return sprintf('%s%s° %s %s %s%s° %s %s',
        $latitudeNotation,
        $latitudeInDegrees,
        $latitudeMinutes,
        $latitudeDirection,
        $longitudeNotation,
        $longitudeInDegrees,
        $longitudeMinutes,
        $longitudeDirection
    );

}
like image 3
bicycle Avatar answered Oct 08 '22 16:10

bicycle