2011-10-28 26 views

Trả lời

14

Bạn có thể tìm thấy chức năng để làm điều đó here

<?php 

function DMStoDEC($deg,$min,$sec) 
{ 

// Converts DMS (Degrees/minutes/seconds) 
// to decimal format longitude/latitude 

    return $deg+((($min*60)+($sec))/3600); 
}  

function DECtoDMS($dec) 
{ 

// Converts decimal longitude/latitude to DMS 
// (Degrees/minutes/seconds) 

// This is the piece of code which may appear to 
// be inefficient, but to avoid issues with floating 
// point math we extract the integer part and the float 
// part by using a string function. 

    $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); 
}  

?> 
+0

Cảm ơn! Điều đó dường như hoạt động! – ragulka

+1

Bạn được chào đón! :) – SERPRO

+0

@SERPRO: bạn có biết làm thế nào để giảm sự khác biệt là do chuyển đổi giữa các độ và DMS? – secretlm

6

Các lat/lon coords được viết bằng (khoảng nói) một cơ sở-60 numeral system. Dưới đây là cách bạn chuyển đổi chúng:

function fraction_to_min_sec($coord) 
{ 
    $isnorth = $coord>=0; 
    $coord = abs($coord); 
    $deg = floor($coord); 
    $coord = ($coord-$deg)*60; 
    $min = floor($coord); 
    $sec = floor(($coord-$min)*60); 
    return array($deg, $min, $sec, $isnorth ? 'N' : 'S'); 
    // or if you want the string representation 
    return sprintf("%d&deg;%d'%d\"%s", $deg, $min, $sec, $isnorth ? 'N' : 'S'); 
} 

Tôi nói chức năng của tôi có độ ổn định số tốt hơn so với @ SeRPRo.

+0

Mã trong câu trả lời của tôi không phải của tôi, đó là một chức năng được tìm thấy. Về chức năng của bạn, tôi không thể nói nó tốt hơn hay không, nhưng tôi không biết biến "$ toàn bộ" đến từ đâu. – SERPRO

+0

Xin lỗi, đó là lỗi đánh máy. –

+0

Hey, nếu tôi muốn tìm hiểu xem đó là phía tây hay phía đông thì sao? – ragulka

1

Đây là nơi bạn chuyển vào vĩ độ, kinh độ trong các giá trị DMS và trả về chuỗi DMS đã chuyển đổi. Dễ dàng và đơn giản

function DECtoDMS($latitude, $longitude) 
{ 
    $latitudeDirection = $latitude < 0 ? 'S': 'N'; 
    $longitudeDirection = $longitude < 0 ? 'W': 'E'; 

    $latitudeNotation = $latitude < 0 ? '-': ''; 
    $longitudeNotation = $longitude < 0 ? '-': ''; 

    $latitudeInDegrees = floor(abs($latitude)); 
    $longitudeInDegrees = floor(abs($longitude)); 

    $latitudeDecimal = abs($latitude)-$latitudeInDegrees; 
    $longitudeDecimal = abs($longitude)-$longitudeInDegrees; 

    $_precision = 3; 
    $latitudeMinutes = round($latitudeDecimal*60,$_precision); 
    $longitudeMinutes = round($longitudeDecimal*60,$_precision); 

    return sprintf('%s%s° %s %s %s%s° %s %s', 
     $latitudeNotation, 
     $latitudeInDegrees, 
     $latitudeMinutes, 
     $latitudeDirection, 
     $longitudeNotation, 
     $longitudeInDegrees, 
     $longitudeMinutes, 
     $longitudeDirection 
    ); 

} 
0

Đây là điều ngược lại khi bạn có DMS chuỗi và cần nó như là số float (chứa các ký tự unicode):

//e.g. 
$dec = dms_to_dec("-18° 51' 30.5697\""); 

/** 
* Convert a coordinate in dms to dec 
* 
* @param string $dms coordinate 
* @return float 
*/ 
function dms_to_dec($dms) 
{ 
    $dms = stripslashes($dms); 
    $neg = (preg_match('/[SWO]/i', $dms) == 0) ? 1 : -1; 
    $dms = preg_replace('/(^\s?-)|(\s?[NSEWO]\s?)/i', '', $dms); 
    $pattern = "/(\\d*\\.?\\d+)(?:[°ºd: ]+)(\\d*\\.?\\d+)*(?:['m′: ])*(\\d*\\.?\\d+)*[\"s″ ]?/i"; 
    $parts = preg_split($pattern, $dms, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); 
    if (!$parts) { 
     return; 
    } 
    // parts: 0 = degree, 1 = minutes, 2 = seconds 
    $d = isset($parts[0]) ? (float)$parts[0] : 0; 
    $m = isset($parts[1]) ? (float)$parts[1] : 0; 
    if (strpos($dms, ".") > 1 && isset($parts[2])) { 
     $m = (float)($parts[1] . '.' . $parts[2]); 
     unset($parts[2]); 
    } 
    $s = isset($parts[2]) ? (float)$parts[2] : 0; 
    $dec = ($d + ($m/60) + ($s/3600))*$neg; 
    return $dec; 
} 
Các vấn đề liên quan