#Translating coordinates from one system to another #Input data set: A file containing coordinates to be translated, negative numbers are used for latitudes... # south of the equator or longitudes west of Greenwich # -In case of 'degree--minute--second' format two or three tab delimited columns... # are required, giving degrees, minutes, and second, respectively... # (two columns if no seconds are given) # -In case of 'degree--minutes.minutes-fraction' format two tab delimited columns... # are required, containing degrees and minutes (as decimal fraction with full stop... # as decimal separator), respectively # -In case of 'degree.degree-fraction' format one column containing the degrees (with full... # stop as decimal separator) is required #Author: Manuel Weinkauf (manuel.weinkauf@uni-tuebingen.de) #Version: 1.0 #Date: 28 August 2010 #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++# #This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.# #To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/. # #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++# #Creation of test dataset DMS_Dat<-matrix(c(52,8,12,18,34,59,-1,28,3,-61,12,18),4,3,byrow=TRUE) DMS_Dat2<-matrix(c(52,8,18,34,-1,28,-61,12),4,2,byrow=TRUE) DMM_Dat<-matrix(c(52,8.2,18,34.983,-1,28.05,-61,12.3),4,2,byrow=TRUE) DD_Dat<-matrix(c(52.1367,18.5831,-1.4675,-61.2050),4,1,byrow=TRUE) ************************************************************************************** #Setting working directory setwd("C:/R_TestData") ######################################################################### # Function to translate coordinates from one system into another # # Necessary input variables: # # InputType: type of input coordinates # # *string* # # possible values: # # DMS=Degree--Minute--(Second) # # DMM=Degree--Minute.Minute-Fraction # # DD=Decimal Degrees # # OutputType: type of input coordinates # # *string* # # possible values: # # DMS=Degree--Minute--(Second) # # DMM=Degree--Minute.Minute-Fraction # # DD=Decimal Degrees # # Input: Name of input matrix, header assumed # # *string* or *matrix* # # Output: Name of output matrix # # *string* # # Import: Do you want to import a txt file? # # *logical* # # TRUE=import data file (Input=*string*) # # FALSE=do not import data file (Input=*matrix*) # # default=TRUE # # Export: Do you want to export a txt file? # # *logical* # # TRUE=export data file # # FALSE=do not export data file # # default=TRUE # # Output data: 95% confidence intervals # # Input dataset: Matrix object, samples in columns... # # data values of specimens in rows # # All columns must have the same length... # # empty fields to be coded as -99.9 # ######################################################################### Trans<-function(InputType,OutputType,Input,Output=NULL,Import=TRUE,Export=TRUE){ if (any((InputType == "DMS"), (InputType == "DMM"), (InputType == "DD"))) {} else {stop("ERROR: InputType must be either DMS, DMM, or DD")} if (any((OutputType == "DMS"), (OutputType == "DMM"), (OutputType == "DD"))) {} else {stop("ERROR: OutputType must be either DMS, DMM, or DD")} if (InputType == OutputType) {stop("InputType and OutputType must be different")} if (Import == TRUE) {Input<-read.table(Input,header=TRUE)} #Code for input in degree--minute--second... {if (InputType == "DMS") { #...and output in degree--minute.minute-fraction {if (OutputType == "DMM") { Temp<-matrix(NA,(dim(Input)[1]),3) Res<-matrix(NA,(dim(Input)[1]),2) Temp[,1]<-Input[,1] Temp[,2]<-Input[,2] Temp[,3]<-round(((Input[,3])/60)*10) Res[,1]<-Temp[,1] Res[,2]<-paste(Temp[,2],".",Temp[,3],sep="") } #...and output in decimal degrees else if (OutputType == "DD") { Temp<-matrix(NA,(dim(Input)[1]),4) Res<-matrix(NA,(dim(Input)[1]),1) {if ((dim(Input)[2]) == 3) { Temp[,1]<-Input[,1] Temp[,2]<-(Input[,2])/(60/100) Temp[,3]<-(Input[,3])/((60*60)/100) Temp[,4]<-round((Temp[,2]+Temp[,3])*100) Res[,1]<-paste(Temp[,1],".",Temp[,4],sep="") } else if ((dim(Input)[2]) == 2) { Temp[,1]<-Input[,1] Temp[,2]<-(Input[,2])/(60/100) Temp[,3]<-c(0) Temp[,4]<-round((Temp[,2]+Temp[,3])*100) Res[,1]<-paste(Temp[,1],".",Temp[,4],sep="") }} }} } #Code for input in degree--minute.minute-fraction... else if (InputType == "DMM") { #...and output in degree--minute--second {if (OutputType == "DMS") { Res<-matrix(NA,(dim(Input)[1]),3) Res[,1]<-Input[,1] Res[,2]<-trunc(Input[,2]) Res[,3]<-(Input[,2]-trunc(Input[,2]))*60 } #...and output in decimal degrees else if (OutputType == "DD") { Temp<-matrix(NA,(dim(Input)[1]),6) Res<-matrix(NA,(dim(Input)[1]),1) Temp[,1]<-Input[,1] Temp[,2]<-trunc(Input[,2]) Temp[,3]<-round((Input[,2]-trunc(Input[,2]))*60) Temp[,4]<-(Temp[,2])/(60/100) Temp[,5]<-(Temp[,3])/((60*60)/100) Temp[,6]<-round((Temp[,4]+Temp[,5])*100) Res[,1]<-paste(Temp[,1],".",Temp[,6],sep="") }} } #Code for input in decimal degrees... else if (InputType == "DD") { #...and output in degree--minute--second {if (OutputType == "DMS") { Res<-matrix(NA,(dim(Input)[1]),3) Res[,1]<-trunc(Input[,1]) Res[,2]<-trunc(abs(Input[,1])*60)%%60 Res[,3]<-(abs(Input[,1])*(60*60))%%60 } #...and output in degree--minute.minute-fraction else if (OutputType == "DMM") { Temp<-matrix(NA, dim(Input)[1],4) Res<-matrix(NA,(dim(Input)[1]),2) Temp[,1]<-trunc(Input[,1]) Temp[,2]<-trunc(abs(Input[,1])*60)%%60 Temp[,3]<-(abs(Input[,1])*(60*60))%%60 Temp[,4]<-round(((Temp[,3])/60)*10) Res[,1]<-Temp[,1] Res[,2]<-paste(Temp[,2],".",Temp[,4],sep="") }} } } Results<-Res {if (Export == TRUE & is.null(Output) == FALSE) {write.table(Results,Output,sep="\t")} else if (Export == TRUE & is.null(Output) == TRUE) {stop("If you want to export data you must give a name for the output file!")} else {Results<-Res}} } #-------------------------------------------- #Example Trans("DMS","DMM",DMS_Dat,Import=FALSE,Export=FALSE) Trans("DD","DMM","Test.txt",Import=TRUE,Export=FALSE) Trans("DMS","DD",DMS_Dat,Import=FALSE,Export=FALSE) Trans("DMS","DD",DMS_Dat2,Import=FALSE,Export=FALSE) Trans("DMM","DMS",DMM_Dat,Import=FALSE,Export=FALSE) Trans("DMM","DD",DMM_Dat,Import=FALSE,Export=FALSE) Trans("DD","DMS",DD_Dat,Import=FALSE,Export=FALSE) Trans("DD","DMM",DD_Dat,Import=FALSE,Export=FALSE) Trans("DD","DMM","Test.txt","Res.txt") Trans("DD","DMM","Test.txt",Export=FALSE)