####################################### ##Function to convert coordinates ##to WGS84 in R. It uses the web service ##build by Anders Larsson, Uppsala ##Universitet, ##http://ormbunkar.se/koordinater/ ##Note that the XML package is ##required. ##Written by Gustaf.Granath@ebc.uu.se ####################################### library(XML) #test data lat= c(6665950,6695950) #coord in RT90 lon= c(1340562,1360562) #coord in RT90 #function getCoord<-function(lat, lon,simplify) { ifelse("package:XLM" %in% search()==FALSE,library(XML),"") latlon<-cbind(lat, lon) Cclass <- c("numeric","numeric","character","character","character","character", "numeric","numeric","character","character","character","character","character","character","character") res<-as.list(rep(NA,nrow(latlon))) for (i in 1:nrow(latlon)) { doc<-paste("http://ormbunkar.se/koordinater/GeorefServlet?lat=", latlon[1,1],"&lon=", latlon[1,2],"&from_coord_sys=AUTO&to_coord_sys=EPSG:4326",sep="") table <- getNodeSet(htmlParse(doc),"//table") [[1]] res[[i]]<-readHTMLTable(table,colClasses=Cclass,stringsAsFactors = FALSE) } res<-do.call("rbind",res) short.res<-res[,c(7,8)] colnames(short.res)<-c("New_lat","New_lon") if(simplify==TRUE) {return(short.res)} else {return(res)} } #Test function #Get only coordinates (simplify=TRUE) getCoord(lat=lat, lon=lon, simplify=TRUE) #Get all information (simplify=FALSE) getCoord(lat=lat, lon=lon, simplify=FALSE)