EndCoordinates.R 1.6 KB

123456789101112131415161718192021222324252627282930313233
  1. EndCoordinates <-
  2. function(LoadDat, FileSep = NULL, Distance = 1000, Angle = 90, AngleUnits = 'radians'|'degrees', Dir = ".",
  3. FileName = "Transect Coordinates")
  4. {
  5. if(!is.object(LoadDat) & !is.character(LoadDat)) stop("Data must be the name of an object in R, or a file path.")
  6. if(is.object(LoadDat)) x <- data.frame(LoadDat)
  7. if(is.character(LoadDat)){
  8. if(!file.exists(LoadDat)) stop("Character string input for LoadDat argument does not resemble an existing file path.")
  9. if(is.null(FileSep)) stop("Data is a file path. Must also specify its delimiter (FileSep).")
  10. x <- read.delim(LoadDat, sep = FileSep)
  11. }
  12. if(!file.exists(Dir)) stop("Character string input for Dir argument does not resemble an existing file path.")
  13. if(AngleUnits == 'radians' & Angle > (2 * pi)) stop('Not sensible radian values. Did you mean degrees?')
  14. if(AngleUnits == 'degrees' & Angle > 360) stop('Not sensible degrees values. Check input.')
  15. if(AngleUnits == 'radians') angle.rad <- Angle
  16. if(AngleUnits == 'degrees') angle.rad <- Angle / (180 / pi)
  17. lat.rad <- x$start.lat / (180 / pi)
  18. delta.lat.metres <- round(Distance * cos(angle.rad))
  19. delta.long.metres <- round(Distance * sin(angle.rad))
  20. delta.lat.degrees <- delta.lat.metres / (111.2 * 1000)
  21. delta.long.degrees <- delta.long.metres / ((111.2 * 1000) * cos(lat.rad))
  22. end.lat <- x$start.lat + delta.lat.degrees
  23. end.long <- x$start.long + delta.long.degrees
  24. x <- cbind(x, end.lat, end.long)
  25. write.csv(x, file = paste(Dir, "/", FileName, Distance, "m", Angle, AngleUnits, Sys.Date(), ".csv", sep = ""),
  26. row.names = FALSE)
  27. }