【问题标题】:Converting Degree, Minutes, Seconds (DMS) to decimal in PHP在 PHP 中将度、分、秒 (DMS) 转换为十进制
【发布时间】:2014-04-14 12:16:23
【问题描述】:

目前,我正在学习使用 Google Maps API。根据我的阅读,API 需要十进制度 (DD) 的纬度和经度。

在我的数据库中,数据存储为 DMS。

例如,110° 29' 01.1"

我想问你们是否有任何 DMS 到 DD 中的 php。而且,转换器必须像上面的示例一样从单个字符串中接受。

问候

【问题讨论】:

标签: php api maps dms


【解决方案1】:

如果这对你有用,你可以试试。

<?php

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

    // Converting DMS ( Degrees / minutes / seconds ) to decimal format
    return $deg+((($min*60)+($sec))/3600);
}    

function DDtoDMS($dec)
{
    // Converts decimal format to DMS ( Degrees / minutes / seconds ) 
    $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);
}    

?>

【讨论】:

  • 但我不知道如何将输入分开。例如 110° 29' 01.1" --> 度 110,分 29,秒 01.1
  • 那个帖子使用了javascript函数。我更喜欢 PHP 函数,因为我在 phpmyadmin 中检索数据
  • 在 PHP 中。 $vars=preg_split("/[^\d\w]+/",$longitude);然后使用 $vars[0], $vars[1], $vars[2]
  • 如果使用此解决方案,请注意如果使用 (-) 表示西风经度,它将不起作用。
【解决方案2】:

这是您在 DMS 值中传递纬度、经度并返回转换后的 DMS 字符串的地方。简单易行

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

}

【讨论】:

    【解决方案3】:

    我编写了一个 PHP 函数来完成问题所要求的工作:将一个以度/分/秒为单位的字符串转换为十进制度。它接受多种不同格式的字符串,并遵循方向 (NSEW)。

    代码如下:

    <?php
    function convertDMSToDecimal($latlng) {
        $valid = false;
        $decimal_degrees = 0;
        $degrees = 0; $minutes = 0; $seconds = 0; $direction = 1;
        // Determine if there are extra periods in the input string
        $num_periods = substr_count($latlng, '.');
        if ($num_periods > 1) {
            $temp = preg_replace('/\./', ' ', $latlng, $num_periods - 1); // replace all but last period with delimiter
            $temp = trim(preg_replace('/[a-zA-Z]/','',$temp)); // when counting chunks we only want numbers
            $chunk_count = count(explode(" ",$temp));
            if ($chunk_count > 2) {
                $latlng = $temp; // remove last period
            } else {
                $latlng = str_replace("."," ",$latlng); // remove all periods, not enough chunks left by keeping last one
            }
        }
    
        // Remove unneeded characters
        $latlng = trim($latlng);
        $latlng = str_replace("º","",$latlng);
        $latlng = str_replace("'","",$latlng);
        $latlng = str_replace("\"","",$latlng);
        $latlng = substr($latlng,0,1) . str_replace('-', ' ', substr($latlng,1)); // remove all but first dash
    
        if ($latlng != "") {
            // DMS with the direction at the start of the string
            if (preg_match("/^([nsewNSEW]?)\s*(\d{1,3})\s+(\d{1,3})\s+(\d+\.?\d*)$/",$latlng,$matches)) {
                $valid = true;
                $degrees = intval($matches[2]);
                $minutes = intval($matches[3]);
                $seconds = floatval($matches[4]);
                if (strtoupper($matches[1]) == "S" || strtoupper($matches[1]) == "W")
                    $direction = -1;
            }
            // DMS with the direction at the end of the string
            if (preg_match("/^(-?\d{1,3})\s+(\d{1,3})\s+(\d+(?:\.\d+)?)\s*([nsewNSEW]?)$/",$latlng,$matches)) {
                $valid = true;
                $degrees = intval($matches[1]);
                $minutes = intval($matches[2]);
                $seconds = floatval($matches[3]);
                if (strtoupper($matches[4]) == "S" || strtoupper($matches[4]) == "W" || $degrees < 0) {
                    $direction = -1;
                    $degrees = abs($degrees);
                }
            }
            if ($valid) {
                // A match was found, do the calculation
                $decimal_degrees = ($degrees + ($minutes / 60) + ($seconds / 3600)) * $direction;
            } else {
                // Decimal degrees with a direction at the start of the string
                if (preg_match("/^(-?\d+(?:\.\d+)?)\s*([nsewNSEW]?)$/",$latlng,$matches)) {
                    $valid = true;
                    if (strtoupper($matches[2]) == "S" || strtoupper($matches[2]) == "W" || $degrees < 0) {
                        $direction = -1;
                        $degrees = abs($degrees);
                    }
                    $decimal_degrees = $matches[1] * $direction;
                }
                // Decimal degrees with a direction at the end of the string
                if (preg_match("/^([nsewNSEW]?)\s*(\d+(?:\.\d+)?)$/",$latlng,$matches)) {
                    $valid = true;
                    if (strtoupper($matches[1]) == "S" || strtoupper($matches[1]) == "W")
                        $direction = -1;
                    $decimal_degrees = $matches[2] * $direction;
                }
            }
        }
        if ($valid) {
            return $decimal_degrees;
        } else {
            return false;
        }
    }
    ?>
    

    在 Github 上有测试用例:https://github.com/prairiewest/PHPconvertDMSToDecimal

    【讨论】:

    【解决方案4】:

    解决了。

     <?php
    
     function DMStoDD($input)
    {
        $deg = " " ;
        $min = " " ;
        $sec = " " ;  
        $inputM = " " ;        
    
    
        print "<br> Input is ".$input." <br>";
    
        for ($i=0; $i < strlen($input); $i++) 
        {                     
            $tempD = $input[$i];
             //print "<br> TempD [$i] is : $tempD"; 
    
            if ($tempD == iconv("UTF-8", "ISO-8859-1//TRANSLIT", '°') ) 
            { 
                $newI = $i + 1 ;
                //print "<br> newI is : $newI"; 
                $inputM =  substr($input, $newI, -1) ;
                break; 
            }//close if degree
    
            $deg .= $tempD ;                    
        }//close for degree
    
         //print "InputM is ".$inputM." <br>";
    
        for ($j=0; $j < strlen($inputM); $j++) 
        { 
            $tempM = $inputM[$j];
             //print "<br> TempM [$j] is : $tempM"; 
    
            if ($tempM == "'")  
             {                     
                $newI = $j + 1 ;
                 //print "<br> newI is : $newI"; 
                $sec =  substr($inputM, $newI, -1) ;
                break; 
             }//close if minute
             $min .= $tempM ;                    
        }//close for min
    
            $result =  $deg+( (( $min*60)+($sec) ) /3600 );
    
    
            print "<br> Degree is ". $deg*1 ;
            print "<br> Minutes is ". $min ;
            print "<br> Seconds is ". $sec ;
            print "<br> Result is ". $result ;
    
    
    return $deg + ($min / 60) + ($sec / 3600);
    
       }
    ?>
    

    【讨论】:

      【解决方案5】:

      这很好用:

      <?php echo "<td> $deg&#176 $min' $sec&#8243 </td>";  ?> 
      

      其中 deg、min 和 sec 是角度坐标。

      【讨论】:

        猜你喜欢
        • 2021-08-19
        • 1970-01-01
        • 2013-03-23
        • 2021-11-27
        • 2018-10-16
        • 2020-02-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多