首页 > 解决方案 > 将度分和十分之一秒转换为十进制

问题描述

受到一些将度分和(十分之一/十进制)秒转换为十进制格式的代码的阻碍:

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 = preg_replace('/\./', ' ', $latlng, $num_periods - 1); // 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 = 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
    elseif (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("/^([nsewNSEW]?)\s*(\d+(?:\.\d+)?)$/",$latlng,$matches)) {
            $valid = true;
            if (strtoupper($matches[1]) == "S" || strtoupper($matches[1]) == "W")
                $direction = -1;
            $decimal_degrees = $matches[2] * $direction;
        }
        // Decimal degrees with a direction at the end of the string
        elseif (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;
        }
    }
}
if ($valid) {
    return $decimal_degrees;
} else {
    return false;
}
}

我的问题既是我的数学技能,也是我修改代码以适应我的用例的能力。

经过一番调查,我们正在输入度、分、秒,但秒是十分之一,所以 1 分钟是 100,而不是纬度 1 分钟是 60。

我尝试了很多变化,这让我头晕目眩!

我想我需要将秒数从十进制格式更改回秒格式,然后用它来将整体更改为十进制。

我很确定这是我需要修改的:

$decimal_degrees = ($degrees + ($minutes / 60) + ($seconds / 3600)) * $direction;

由于秒是十进制格式,我认为 / 3600 可能是错误的。

任何帮助都会很棒!

标签: phpmathdegrees

解决方案


排序!

我必须将分钟和秒合并在一起,并正确计算将其更改为十进制。这是从 GPS 单元或导航图获取度、分、小数秒并将条目输出为小数的完整代码。

他们的关键部分是这个部分......

     //do the calculation
     $minsSecsCombined = $minutes.'.'.$seconds; //Combine the minutes and seconds together
     $math = (($minsSecsCombined / 3600) * 60) * 10000; //Math

     $degrees = $degrees.'.'.$math; //Combine the degrees and result together
     $degrees = number_format((float)$degrees, 6, '.', ''); //Ensure the result is classed as a number

     $decimal_degrees = $degrees * $direction; //Put them all together

这是完整的代码......

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 = preg_replace('/\./', ' ', $latlng, $num_periods - 1); // 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 = 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 = $matches[2];
         $minutes = $matches[3];
         $seconds = $matches[4];
         if (strtoupper($matches[1]) == "S" || strtoupper($matches[1]) == "W")
             $direction = -1;
     }
     // DMS with the direction at the end of the string
     elseif (preg_match("/^(-?\d{1,3})\s+(\d{1,3})\s+(\d+(?:\.\d+)?)\s*([nsewNSEW]?)$/",$latlng,$matches)) {
         $valid = true;
         $degrees = $matches[1];
         $minutes = $matches[2];
         $seconds = $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
         $minsSecsCombined = $minutes.'.'.$seconds; //Combine the minutes and seconds together

 //hack to sort missing 0 - If a latitude or longitude starts with a 0 the calculation strips it out making it up to 9miles off location once calculated. This code cheats by re-adding the 0
 if (substr($minutes, 0, 1 ) === "0") {
   $math = '0'.$math;
 }
         $math = (($minsSecsCombined / 3600) * 60) * 10000; //Math

         $degrees = $degrees.'.'.$math; //Combine the degrees and result together
         $degrees = number_format((float)$degrees, 6, '.', ''); //Ensure the result is classed as a number

         $decimal_degrees = $degrees * $direction; //Put them all together


     } else {
         // Decimal degrees with a direction at the start 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;
         }
         // Decimal degrees with a direction at the end of the string
         elseif (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;
         }
     }
 }
 if ($valid) {
     return $decimal_degrees;
 } else {
     return false;
 }
 }

推荐阅读