This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
programming:php [2023/02/16 18:48] – [Enable ZEND opcache] Jan Forman | programming:php [2025/04/23 08:59] (current) – [Hello World] Jan Forman | ||
---|---|---|---|
Line 125: | Line 125: | ||
$greeter-> | $greeter-> | ||
?> | ?> | ||
+ | </ | ||
+ | |||
+ | ====== WGS84 DMS to DD converter ====== | ||
+ | < | ||
+ | <?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., " | ||
+ | * @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 = '/ | ||
+ | |||
+ | if (preg_match($pattern, | ||
+ | $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 === " | ||
+ | $dd *= -1; | ||
+ | } | ||
+ | |||
+ | return $dd; | ||
+ | } else { | ||
+ | return null; // Invalid format | ||
+ | } | ||
+ | } | ||
+ | |||
+ | $source = " | ||
+ | |||
+ | $source = explode(" | ||
+ | $i = 0; | ||
+ | while ($source[$i]) { | ||
+ | $xy = explode(",", | ||
+ | echo dmsToDd($xy[0]) . ";" | ||
+ | $i++; | ||
+ | } | ||
</ | </ | ||