Converting ISO 6709 Formatted GPS Coordinates to Decimal Degrees in R

StackOverflow https://stackoverflow.com/questions/8335146

  •  26-10-2019
  •  | 
  •  

سؤال

We have a piece of equipment that outputs its GPS coordinates as numeric values to file in an ISO 6709 lat/lon format of Lat = ±DDMM.MMMM & Lon = ±DDDMM.MMMM

Are there any packages with functions (or custom functions) in R that will convert this to a Decimal degrees format? (ie: ±DD.DDDDDD & ±DDD.DDDDDD)

An example would be that lat & lon (2433.056, -8148.443) would be converted to (24.55094, -81.80739).

هل كانت مفيدة؟

المحلول

You could read in the values from the file using something like read.csv or read.delim.

Then to convert from DDMM.MMMM and DDDMM.MMMM you could use something like this (of course modify as needed for the form of your input/outputs):

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

نصائح أخرى

This may not be the most elegant PHP code, but it does work:

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

I has a similar issue getting coordinates from FedEx WS. I used this function to get the values from a string like +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;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top