Hallo,

heeft er iemand een script of functie om decimale coordinaten ( breedtegraad :52.09065 lengtegraad :5.12132) om te zetten naar h ddd° mm.mmm' (graden en minuten) breedtegraad :N52° 05.439 lengetgraad :E005° 07.279 en omgekeerd.

Dank bij voorbaat
je kan proberen om de reguliere expressie (zie Ivo P) aan te passen. Misschien ben je al op 99% van een werkende code, en is dit de oplossing.
verander


function DMSToDecimal($dms) {
            echo $dms ;
            preg_match('/([NS])(\d+) (\d+\.\d+)/', $dms, $latMatches);
            preg_match('/([EW])(\d+) (\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];
        }


naar dit:

function DMSToDecimal($dms) {
            echo $dms ;
            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];
        }
E vH

dit is momenteel de code

if(!function_exists('DMSToDecimal')) {
        function DMSToDecimal($dms) {

            preg_match('/([NS])(\d+) (\d+\.\d+)/', $dms, $latMatches);
            preg_match('/([EW])(\d+) (\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=$data["Breedtegraad"];
    $dmsLongitude=$data["Lengtegraad"];


    $coordinates = DMSToDecimal($dmsLatitude . " " . $dmsLongitude);
echo var_dump($coordinates);
    if ($coordinates) {
        $latitude = $coordinates['latitude'];
        $longitude = $coordinates['longitude'];
        
        echo "Breedtegraad (decimaal): $latitude<br>";
        echo "Lengtegraad (decimaal): $longitude<br>";
    } else {
        echo "Ongeldige DMS-cordinaten.";
    }

// Place event code here.
// Use "Add Action" button to add code snippets.

return true;
}

Berta Pappens op 08/11/2023 11:11:55

E vH

dit is momenteel de code:


verander hem naar:

if(!function_exists('DMSToDecimal')) {
        function DMSToDecimal($dms) {

            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=$data["Breedtegraad"];
    $dmsLongitude=$data["Lengtegraad"];


    $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-cordinaten.";
    }

// Place event code here.
// Use "Add Action" button to add code snippets.

return true;
}


Allemaal,

Denk dat we er zijn :
output met de var_dump erbij .

N51° 51.802 E004° 32.262array(2) { ["latitude"]=> float(51.863366666667) ["longitude"]=> float(4.5377) } Breedtegraad (decimaal): 51.863366666667
Lengtegraad (decimaal): 4.5377
N50° 54.062 E006° 54.411array(2) { ["latitude"]=> float(50.901033333333) ["longitude"]=> float(6.90685) } Breedtegraad (decimaal): 50.901033333333
Lengtegraad (decimaal): 6.90685
N50° 21.471 E015° 47.393array(2) { ["latitude"]=> float(50.35785) ["longitude"]=> float(15.789883333333) } Breedtegraad (decimaal): 50.35785
Lengtegraad (decimaal): 15.789883333333
N49° 32.138 E017° 43.389array(2) { ["latitude"]=> float(49.535633333333) ["longitude"]=> float(17.72315) } Breedtegraad (decimaal): 49.535633333333
Lengtegraad (decimaal): 17.72315

nogmaals allemaal heel hartelijk bedankt om mij te helpen als oud leek en heel veel begrip voor jullie geduld.
Jullie zijn professionele tegenover een nietsnut.
Kudos voor Ivo :-)

En top dat je het niet opgaf Berta!
Graag gedaan, Berta!

[size=xsmall]Toevoeging op 08/11/2023 11:37:15:[/size]

Anyway, misschien is het de moeite waard om de functie te delen in de scripts-sectie hier op PHPhulp.
Ariën,
Natuurlijk moet ik dat doen of ga jij dit doen ?
Tenslotte heb ik dit niet gemaakt.
Jij bent niks verplicht ;-)

Reageren