Search code examples
rlatitude-longitudergdalutm

Convert lat long to utm NAD83 zone 20


I have 2 similar data sets, where I want to convert my latitude and longitude to UTM (NAD83 Zone 20N). One of my data sets has the raw data Data set 1, and for the other one I have calculated the average position based on a given time interval Data set 2.

The second data set does have a few NAs in the mean lat and long sections, yet I would need to find a way in the code to accept the NAs and convert all the other lat/longs.

I have tried using different codes that I have found on the Internet, but they are giving me errors or won't convert lat/long to UTM.

Code 1

setwd("~/Documents/UVI/Thesis/Data/Analyses/Practice/MCP_Lsynagris")

tagdata<-read.csv("Data/allcombinedMAforSA.csv", header=T, sep=",", strip.white=T)
tagdata$detection_time_ast<-as.POSIXct(tagdata$detection_time_ast, format="%Y-%m-%d %H:%M:%S",tz="UTC")

tagdata<-tagdata[order(tagdata$detection_time_ast),]

cord.dec = SpatialPoints(cbind(tagdata$long_nad83, -tagdata$lat_nad83), proj4string = CRS("+proj=longlat"))
#Transfoming coordinate to UTM using ESPG 26920 for NAD83 Zone 20N.
cord_UTM<-spTransform(cord.dec, CRS("+init=esp:26290"))
cord_UTM

For the first set of codes I received the following error

> cord_UTM<-spTransform(cord.dec, CRS("+init=esp:26290"))
Error in spTransform(cord.dec, CRS("+init=esp:26290")) : 
  error in evaluating the argument 'CRSobj' in selecting a method for function 'spTransform': Error in CRS("+init=esp:26290") : no system list, errno: 2
> cord_UTM
Error: object 'cord_UTM' not found

Code 2

tagdata<-read.csv("Data/allcombinedMAforSA.csv", header=T, sep=",", strip.white=T)

tagdata$detection_time_ast<-as.POSIXct(tagdata$detection_time_ast, format="%Y-%m-%d %H:%M:%S",tz="UTC")

tagdata<-tagdata[order(tagdata$detection_time_ast),]

coordinates(tagdata) <- c("long_nad83", "lat_83")
proj4string(tagdata) <- CRS("+proj=longlat +datum=NAD83")  

res <- spTransform(tagdata, CRS("+proj=utm +zone=20 ellps=NAD83"))
res
as(res, "SpatialPoints") 

Here are the following errors I received in the second code

Error in `[.data.frame`(object, , value) : undefined columns selected
> proj4string(tagdata) <- CRS("+proj=longlat +datum=NAD83")  
Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘proj4string<-’ for signature ‘"data.frame", "CRS"’
> 
> res <- spTransform(tagdata, CRS("+proj=utm +zone=20 ellps=NAD83"))
Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘spTransform’ for signature ‘"data.frame", "CRS"’
> res
Error: object 'res' not found

> as(res, "SpatialPoints")
Error in .class1(object) : object 'res' not found

Is there a better way to go about converting my lat/long to UTM NAD83 Zone 20 for either dat set? As well as, for the code to accept that there are NA's in the average lat/long columns for my second data set.


Solution

  • I got this code from an online source (don't have the reference handy):

    LongLatToUTM<-function(x,y,zone){
      require(sp)
      xy <- data.frame(ID = 1:length(x), X = x, Y = y)
      coordinates(xy) <- c("X", "Y")
      proj4string(xy) <- CRS("+proj=longlat +datum=WGS84")  ## for example
      res <- spTransform(xy, CRS(paste("+proj=utm +zone=",zone," ellps=WGS84",sep='')))
      return(as.data.frame(res))
    }
    

    Note that this is for WGS84.