2011-12-01 55 views
1

我們有一臺設備輸出其GPS座標值作爲數值,以ISO 6709緯度/經度格式Lat =±DDMM.MMMM & Lon =±DDDMM。 MMMM將ISO 6709格式化的GPS座標轉換爲R中的十進制度

在R中是否有任何包含函數(或自定義函數)的包會將其轉換爲十進制度格式? (即:±DD.DDDDDD &±DDD.DDDDDD)

一個例子將是LAT & LON(2433.056,-8148.443)將被轉換爲(24.55094,-81.80739)。

+0

不能直接關心你的問題,但有R中,可以從緯度/經度轉換成各種預測一個完整的系統。 –

+0

Thx用於評論。我查看了「MapProj」和「MapTools」軟件包,但沒有看到他們能夠完成上述任務。如果可以,請告訴。 – cpuguru

+0

嘗試sp包。 – mdsumner

回答

3

您可以使用類似read.csvread.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) 
} 
+0

不錯的工作 - 不知道「sign」函數,試圖用if語句和<0邏輯來實現。 – cpuguru

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; 
} 
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; 
} 
相關問題