我们有一台设备输出其GPS坐标作为数值,以ISO 6709 lat / lon格式输入Lat =±DDMM.MMMM& Lon =±DDDMM.MMMM
R中是否有包含函数(或自定义函数)的软件包将其转换为十进制度格式? (即:±DD.DDDDDD&±DDD.DDDDDD)
一个例子是lat& lon(2433.056,-8148.443)将被转换为(24.55094,-81.80739)。
答案 0 :(得分:3)
您可以使用read.csv
或read.delim
等内容读取文件中的值。
然后从DDMM.MMMM和DDDMM.MMMM转换你可以使用这样的东西(当然根据输入/输出的形式修改):
convertISO6709 <- function( lat, lon ) {
# will just do lat and lon together, as the process is the same for both
# It's simpler to do the arithmetic on positive numbers, we'll add the signs
# back in at the end.
latlon <- c(lat,lon)
sgns <- sign(latlon)
latlon <- abs(latlon)
# grab the MM.MMMM bit, which is always <100. '%%' is modular arithmetic.
mm <- latlon %% 100
# grab the DD bit. Divide by 100 because of the MM.MMMM bit.
dd <- (latlon - mm)/100
# convert to decimal degrees, don't forget to add the signs back!
out_latlon <- (dd+mm/60) * sgns
return(out_latlon)
}
答案 1 :(得分:0)
这可能不是最优雅的PHP代码,但确实有效:
function convertISO6709($coord)
{
$abs_coord = abs($coord);
$sign = ($abs_coord == $coord) ? 1 : -1;
$m = 2;
$dlen = 2;
$s = 0;
$seconds = 0;
switch (strlen($abs_coord)) {
case 4 :
break;
case 5 :
$dlen = 3;
$m = 3;
break;
case 6 :
$s = 4;
break;
}
$degrees = substr($abs_coord, 0, $dlen);
$minutes = substr($abs_coord, $m, 2);
if ($s != 0) {
$seconds = substr($abs_coord, $s, 2);
}
return ($degrees + ($minutes / 60) + ($seconds / 3600)) * $sign;
}
答案 2 :(得分:0)
我有一个类似的问题从FedEx WS获取坐标。我使用此函数从+ 19.467945-99.14357 /:
这样的字符串中获取值function convertCoordISO6709($coord)
{
//$coord example
//$coord = +19.467945-99.14357/
$returnArray[0] = 1;//result non 0 means error
$returnArray[1] = 0;//Lat
$returnArray[2] = 0;//long
$coord = trim($coord,"/"); //Strip / sign
//look for + - sign
$lat_sign = substr($coord,0,1); //strip and save the first sign (latitude value)
$sub_coord = substr($coord,1,strlen($coord));
if(count(explode("+",$sub_coord)) == 2) //Second value is + for the longitude
{
$coords=explode("+",$sub_coord);
$returnArray[0] = 0;
$returnArray[1] = $lat_sign.$coords[0];
$returnArray[2] = "+".$coords[1];
}
else //Second value is - for the longitude
{
$coords=explode("-",$sub_coord);
$returnArray[0] = 0;
$returnArray[1] = $lat_sign.$coords[0];
$returnArray[2] = "-".$coords[1];
}
return $returnArray;
}