Konvertieren von ISO 6709 Formatierten GPS -Koordinaten in r in r
Frage
Wir haben ein Gerät, das seine GPS -Koordinaten als numerische Werte ausgibt ISO 6709 LAT/LON -Format von lat = ± ddmmmmmmm & lon = ± dddmmmmmmmmmmmmmmmm
Gibt es Pakete mit Funktionen (oder benutzerdefinierten Funktionen) in R, die dies in ein Dezimalgrad -Format umwandeln? (dh: ± dd.ddddd & ± ddd.dddddd)
Ein Beispiel wäre, dass Lat & Lon (2433.056, -8148.443) in (24.55094, -81.80739) konvertiert werden würde.
Lösung
Sie könnten in den Werten aus der Datei mit etwas wie lesen read.csv
oder read.delim
.
Um dann von ddmm.mmmmm und dddmm.mmmm zu konvertieren, können Sie so etwas verwenden (natürlich ändern Sie nach Bedarf für die Form Ihrer Eingabe/Ausgänge):
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)
}
Andere Tipps
Dies ist vielleicht nicht der eleganteste PHP -Code, funktioniert jedoch:
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;
}
Ich habe ein ähnliches Problem, das Koordinaten von FedEx WS erhält. Ich habe diese Funktion verwendet, um die Werte aus einer Zeichenfolge zu erhalten wie +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;
}