将lat long转换为在r中的utm NAD83 zone 20

时间:2017-12-06 21:05:17

标签: r latitude-longitude rgdal utm

我有2个类似的数据集,我想将纬度和经度转换为UTM(NAD83区域20N)。我的一个数据集具有原始数据(Data set 1),另一个数据集基于给定的时间间隔(Data set 2)计算了平均位置。第二个数据集在平均纬度和长度部分确实有几个NA,但我需要在代码中找到一种接受NA并转换所有其他纬度/长度的方法。 我曾尝试使用我在互联网上找到的不同代码,但他们给了我错误或者不会拉/长。

代码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?同样,代码接受我的第二个数据集的平均纬度/长度列中有NA。先感谢您。

1 个答案:

答案 0 :(得分:0)

我从某个地方获得了这个代码(一旦找到它就会引用)并且它对我来说很好用:

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