Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Degree, Minutes, Seconds (DMS) to decimal in PHP

Tags:

php

maps

api

dms

Currently, I'm learning to use Google Maps API. From what I read, the API require the latitude and longitude in Decimal Degree (DD).

In my database, the data is stored as DMS.

Example, 110° 29' 01.1"

I would to ask if you guys have any DMS to DD in php. And, the converter must accept from a single string like the example above.

Regards

like image 472
Azlan Nohara Avatar asked Mar 11 '14 04:03

Azlan Nohara


People also ask

What is 156.742 degrees in to DMS?

0.52*60 = 31.2, so the whole number 31 equals seconds. Decimal degrees 156.742 converts to 156 degrees, 44 minutes and 31 seconds, or 156° 44' 31".

Can DMS have decimals?

DMS use North/South, East/West explicitly to indicate the direction of the offset from Greenwich and the Equator (e.g. 45°45'32.4″N 009°23'39.9″E). Decimal Degrees are a very simple way to represent coordinates.

What is DMS degree minute second?

Degrees Minutes Seconds (DMS) Degrees, Minutes, and Seconds, or DMS, is the oldest format for geographic coordinates and you will still see this format in use on paper and even in movies where navigators will give their location in DMS. Latitude: 40° 51' 59" N. Longitude: 124° 4' 58" W.


2 Answers

You can try if this is working for you.

<?php

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

    // Converting DMS ( Degrees / minutes / seconds ) to decimal format
    return $deg+((($min*60)+($sec))/3600);
}    

function DDtoDMS($dec)
{
    // Converts decimal format to DMS ( Degrees / minutes / seconds ) 
    $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 90
Romi Avatar answered Sep 23 '22 03:09

Romi


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

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 36
bicycle Avatar answered Sep 20 '22 03:09

bicycle