代码之家  ›  专栏  ›  技术社区  ›  Mareike Donají Duffing Romero

将lat long转换为utm NAD83区域20

  •  0
  • Mareike Donají Duffing Romero  · 技术社区  · 7 年前

    我有两个类似的数据集,我想把我的纬度和经度转换成UTM(NAD83 Zone 20N)。我的一个数据集包含原始数据 Data set 1 ,对于另一个,我根据给定的时间间隔计算了平均位置 Data set 2 .

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

    我曾尝试使用在互联网上找到的不同代码,但它们给了我错误,或者无法将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
    

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

    1 回复  |  直到 4 年前
        1
  •  0
  •   M--    4 年前

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

    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 .