dmstodecimal.php
Code (php)
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
41
42
43
44
45
46
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
41
42
43
44
45
46
<?php
function DMSToDecimal($dms) {
//preg_match('/([NS])(\d+)° (\d+\.\d+)/', $dms, $latMatches);
//preg_match('/([EW])(\d+)° (\d+\.\d+)/', $dms, $lonMatches);
preg_match('/([NS])(\d+)[^0-9]+(\d+\.\d+)/', $dms, $latMatches);
preg_match('/([EW])(\d+)[^0-9]+(\d+\.\d+)/', $dms, $lonMatches);
if (!empty($latMatches)) {
$latDirection = $latMatches[1];
$latDegrees = (float)$latMatches[2];
$latMinutes = (float)$latMatches[3];
$latitude = ($latDirection === 'N') ? ($latDegrees + ($latMinutes / 60)) : -($latDegrees + ($latMinutes / 60));
} else {
return false; // Invalid latitude format
}
if (!empty($lonMatches)) {
$lonDirection = $lonMatches[1];
$lonDegrees = (float)$lonMatches[2];
$lonMinutes = (float)$lonMatches[3];
$longitude = ($lonDirection === 'E') ? ($lonDegrees + ($lonMinutes / 60)) : -($lonDegrees + ($lonMinutes / 60));
} else {
return false; // Invalid longitude format
}
return ['latitude' => $latitude, 'longitude' => $longitude];
}
$dmsLatitude = "N53° 34.127";
$dmsLongitude = "E006° 44.986";
$coordinates = DMSToDecimal($dmsLatitude . " " . $dmsLongitude);
if ($coordinates) {
$latitude = $coordinates['latitude'];
$longitude = $coordinates['longitude'];
echo "Breedtegraad (decimaal): $latitude<br>";
echo "Lengtegraad (decimaal): $longitude<br>";
} else {
echo "Ongeldige DMS-coördinaten.";
}
?>
function DMSToDecimal($dms) {
//preg_match('/([NS])(\d+)° (\d+\.\d+)/', $dms, $latMatches);
//preg_match('/([EW])(\d+)° (\d+\.\d+)/', $dms, $lonMatches);
preg_match('/([NS])(\d+)[^0-9]+(\d+\.\d+)/', $dms, $latMatches);
preg_match('/([EW])(\d+)[^0-9]+(\d+\.\d+)/', $dms, $lonMatches);
if (!empty($latMatches)) {
$latDirection = $latMatches[1];
$latDegrees = (float)$latMatches[2];
$latMinutes = (float)$latMatches[3];
$latitude = ($latDirection === 'N') ? ($latDegrees + ($latMinutes / 60)) : -($latDegrees + ($latMinutes / 60));
} else {
return false; // Invalid latitude format
}
if (!empty($lonMatches)) {
$lonDirection = $lonMatches[1];
$lonDegrees = (float)$lonMatches[2];
$lonMinutes = (float)$lonMatches[3];
$longitude = ($lonDirection === 'E') ? ($lonDegrees + ($lonMinutes / 60)) : -($lonDegrees + ($lonMinutes / 60));
} else {
return false; // Invalid longitude format
}
return ['latitude' => $latitude, 'longitude' => $longitude];
}
$dmsLatitude = "N53° 34.127";
$dmsLongitude = "E006° 44.986";
$coordinates = DMSToDecimal($dmsLatitude . " " . $dmsLongitude);
if ($coordinates) {
$latitude = $coordinates['latitude'];
$longitude = $coordinates['longitude'];
echo "Breedtegraad (decimaal): $latitude<br>";
echo "Lengtegraad (decimaal): $longitude<br>";
} else {
echo "Ongeldige DMS-coördinaten.";
}
?>