将经纬度转换为 utm NAD83 zone 20

Convert lat long to utm NAD83 zone 20

我有 2 个类似的数据集,我想将我的纬度和经度转换为 UTM(NAD83 Zone 20N)。我的一个数据集有原始数据 Data set 1, and for the other one I have calculated the average position based on a given time interval Data set 2.

第二个数据集在平均纬度和经度部分确实有一些 NA,但我需要在代码中找到一种方法来接受 NA 并转换所有其他 lat/longs。

我尝试使用在 Internet 上找到的不同代码,但它们给我错误或无法将 lat/long 转换为 UTM。

代码 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

对于第一组代码,我收到以下错误

> 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

代码 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") 

这是我在第二个代码中收到的以下错误

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

是否有更好的方法将我的 lat/long 转换为 UTM NAD83 Zone 20 的任一数据集?此外,对于接受我的第二个数据集的平均 lat/long 列中有 NA 的代码。

我从网上获得了这段代码(手头没有参考资料):

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))
}

请注意,这是针对 WGS84