Page Contents
Converting geographical coordinates from Degree, Minutes, Seconds (DMS) format to decimal format is a common requirement in many applications involving mapping, GPS, and location-based services. PHP provides functions to easily perform this conversion, allowing developers to manipulate and display coordinates more conveniently. Building further on the DDtoDMS($dec) function, if you would like to obtain a representational string as seen on Google Maps, you could use the below function as an extension:
Understanding the Code
The given PHP code offers two essential functions:
- DMStoDD($deg, $min, $sec)
- DDtoDMS($dec)
The DMStoDD function converts DMS (Degrees/Minutes/Seconds) format to decimal format, while DDtoDMS does the opposite conversion—decimal to DMS.
Converting DMS to Decimal
The DMStoDD function takes three parameters: degrees, minutes, and seconds, and calculates the decimal value. It applies the formula:
1 2 3 4 5 6 7 8 9 |
function DMStoDD($deg,$min,$sec) { // Converting DMS ( Degrees / minutes / seconds ) to decimal format return $deg+((($min*60)+($sec))/3600); } |
This function adds the degrees to the result of converting minutes and seconds into fractions of a degree.
Converting Decimal to DMS
The DDtoDMS function performs the reverse operation. Given a decimal value, it calculates the degrees, minutes, and seconds.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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); } |
This function extracts the degrees from the decimal value, converts the remaining decimal part into minutes and seconds, and returns an array containing these values.
Enhancing Functionality
Additionally, there’s an extended function DDtoDMS_string that converts coordinates into a representational string format commonly used in mapping services like Google Maps.
The function formats latitude and longitude into a human-readable string in DMS format, including North/South (for latitude) and East/West (for longitude) indicators.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
function DDtoDMS_string($latitude=false, $longitude=false) { $result = array(); # latitude (N or S) if($latitude) { $degrees = DDtoDMS($latitude); # data manipulation (2 digits, round, ...) $degrees['min'] = sprintf('%02d',$degrees['min']); $degrees['sec'] = sprintf('%04.1f',number_format($degrees['sec'], 1)); # N or S $north_south = ($degrees['deg'] < 0) ? 'S' : 'N'; array_push($result, abs($degrees['deg']).'°'.$degrees['min'].'\''.$degrees['sec'].'"'.$north_south); } # longitude (E or W) if($longitude) { $degrees = DDtoDMS($longitude); # data manipulation (2 digits, round, ...) $degrees['min'] = sprintf('%02d',$degrees['min']); $degrees['sec'] = sprintf('%04.1f',number_format($degrees['sec'], 1)); # E or W $east_west = ($degrees['deg'] < 0) ? 'W' : 'E'; array_push($result, abs($degrees['deg']).'°'.$degrees['min'].'\''.$degrees['sec'].'"'.$east_west); } return implode(' ', $result); } |
Practical Usage
Here are some examples of using these functions to convert coordinates:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# North West echo DDtoDMS_string(38.686290, -47.464092); // Result: 38°41'10.6"N 47°27'50.7"W # North East echo DDtoDMS_string(34.505935, 134.732914); // Result: 34°30'21.4"N 134°43'58.5"E # South West echo DDtoDMS_string(-51.102776, -68.786031); // Result: 51°06'10.0"S 68°47'09.7"W # South East echo DDtoDMS_string(-23.724127, 110.561906); // Result: 23°43'26.9"S 110°33'42.9"E |
Conclusion
Converting geographical coordinates between DMS and decimal formats is crucial for various applications. The provided PHP functions offer an efficient way to perform these conversions, making it easier to handle and display location-based data. Whether for mapping services, GPS applications, or general coordinate manipulation, these functions facilitate seamless conversions between different coordinate representations in PHP.
Here is completed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
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); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
function DDtoDMS_string($latitude=false, $longitude=false) { $result = array(); # latitude (N or S) if($latitude) { $degrees = DDtoDMS($latitude); # data manipulation (2 digits, round, ...) $degrees['min'] = sprintf('%02d',$degrees['min']); $degrees['sec'] = sprintf('%04.1f',number_format($degrees['sec'], 1)); # N or S $north_south = ($degrees['deg'] < 0) ? 'S' : 'N'; array_push($result, abs($degrees['deg']).'°'.$degrees['min'].'\''.$degrees['sec'].'"'.$north_south); } # longitude (E or W) if($longitude) { $degrees = DDtoDMS($longitude); # data manipulation (2 digits, round, ...) $degrees['min'] = sprintf('%02d',$degrees['min']); $degrees['sec'] = sprintf('%04.1f',number_format($degrees['sec'], 1)); # E or W $east_west = ($degrees['deg'] < 0) ? 'W' : 'E'; array_push($result, abs($degrees['deg']).'°'.$degrees['min'].'\''.$degrees['sec'].'"'.$east_west); } return implode(' ', $result); } |
Examples:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# North West echo DDtoDMS_string(38.686290, -47.464092); # Result: 38°41'10.6"N 47°27'50.7"W # North East echo DDtoDMS_string(34.505935, 134.732914); # Result: 34°30'21.4"N 134°43'58.5"E # South West echo DDtoDMS_string(-51.102776, -68.786031); # Result: 51°06'10.0"S 68°47'09.7"W # South East echo DDtoDMS_string(-23.724127, 110.561906); # Result: 23°43'26.9"S 110°33'42.9"E |
Or use it separately:
1 2 3 4 5 6 7 8 9 |
$latitude = 51.120578; $longitude = 4.276586; echo '<p>latitude: '.$latitude.' == '.DDtoDMS_string($latitude).'</p>'; echo '<p>longitude: '.$longitude.' == '.DDtoDMS_string(false, $longitude).'</p>'; |
Gives you:
1 2 3 4 5 6 |
latitude: 51.120578 == 51°07'14.1"N longitude: 4.276586 == 4°16'35.7"E |