php - 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 = 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;


}


}



我需要将秒数从十进制格式更改为秒格式,然后使用十进制格式将整个格式更改为十进制格式。

我确信这是我需要修改的:


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



因为秒是十进制格式,所以我认为3600可能是错误的。

时间:

这是完成的代码,用于从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;


 }


 }



...