User Tools

Site Tools


programming:php

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
programming:php [2023/02/16 18:48] – [Enable ZEND opcache] Jan Formanprogramming:php [2025/04/23 08:59] (current) – [Hello World] Jan Forman
Line 125: Line 125:
 $greeter->sayHello("World"); $greeter->sayHello("World");
 ?> ?>
 +</code>
 +
 +====== WGS84 DMS to DD converter ======
 +<code>
 +<?php
 +
 +/**
 + * Converts WGS84 coordinates from Degrees, Minutes, Seconds (DMS) format
 + * to Decimal Degrees (DD) format.
 + *
 + * @param string $dms Latitude or longitude in DMS format (e.g., "49°44'55.23\"N", "15°33'12.87\"E").
 + * @return float|null Decimal degrees, or null if the input format is invalid.
 + */
 +function dmsToDd(string $dms): ?float
 +{
 +    // Regular expression to parse DMS format
 +    $pattern = '/^(\d+)°(\d+)\'([\d.]+)"([NSEW])$/i';
 +
 +    if (preg_match($pattern, $dms, $matches)) {
 +        $degrees = intval($matches[1]);
 +        $minutes = intval($matches[2]);
 +        $seconds = floatval($matches[3]);
 +        $direction = strtoupper($matches[4]);
 +
 +        $dd = $degrees + $minutes / 60 + $seconds / 3600;
 +
 +        // Adjust sign based on direction
 +        if ($direction === "S" || $direction === "W") {
 +            $dd *= -1;
 +        }
 +
 +        return $dd;
 +    } else {
 +        return null; // Invalid format
 +    }
 +}
 +
 +$source = "49°26'07.78\"N,12°52'01.14\"E";
 +
 +$source = explode("\n", $source);
 +$i = 0;
 +while ($source[$i]) {
 +    $xy = explode(",", $source[$i]);
 +    echo dmsToDd($xy[0]) . ";" . dmsToDd($xy[1]) . "\n";
 +    $i++;
 +}
 </code> </code>
  
programming/php.txt · Last modified: 2025/04/23 08:59 by Jan Forman