R:SpatialPointsDataFrame 代码不再起作用。错误为!res[[1]]:无效的参数类型。

8

我一直在遵循这个工作流程在R语言中将东北坐标系转换成经纬度。直到今天,它一直正常工作。以下是一个可重现的示例:

require(rgdal)
# create test coordinates
x <- 259269 y <- 074728

# create test dataframe 
dat <- data.frame(x, y) 
class(dat) # "data.frame"

### shortcuts 
ukgrid <- "+init=epsg:27700" 
latlong <- "+init=epsg:4326"

### Create coordinates object 
coords <- cbind(Easting = as.numeric(as.character(x)),
                Northing = as.numeric(as.character(y)))
class(coords) # matrix
dat_SP <- SpatialPointsDataFrame(coords,
                              data = dat,
                              proj4string = CRS("+init=epsg:27700"))

# Error in !res[[1]] : invalid argument type

# Following steps ----

# Convert
dat_SP_LL <- spTransform(dat_SP, CRS(latlong)

# replace Lat, Long
dat_SP_LL@data$Long <- coordinates(dat_SP_LL)[, 1]
dat_SP_LL@data$Lat <- coordinates(dat_SP_LL)[, 2]

我认为这可能与proj4string参数有关,但我一直无法解决它。欢迎任何帮助。

我的会话信息:

> sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18362)

Matrix products: default

locale:
[1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252   
[3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] forcats_0.5.0      stringr_1.4.0      dplyr_0.8.3        purrr_0.3.3       
 [5] readr_1.3.1        tibble_3.0.1       tidyverse_1.3.0    ggspatial_1.1.2   
 [9] tmap_3.0           rnrfa_2.0.3        gdalUtils_2.0.3.2  zoon_0.6.5        
[13] biomod2_3.4.6      sdm_1.0-89         SDMTools_1.1-221.1 SSDM_0.2.8        
[17] odbc_1.2.2         DBI_1.1.0          rgeos_0.5-3        rgdal_1.5-8       
[21] tidyr_1.1.0        ggplot2_3.3.1      knitr_1.25         raster_3.0-7      
[25] sp_1.3-1          
2个回答

8

我遇到了同样的问题。这个错误与sp::CRS有关。我通过重新安装'sp'包来解决它。


我重新安装了各种空间包,包括sp,看起来似乎解决了问题。谢谢。可能旧版本的sp不再受支持? - RWard

0

我遇到了同样的问题。它来自于CRS函数。如果你看一下函数,它来自于第34行(我在下面的代码中用星号标记了),但我不知道该如何修复它。这可能是一个错误。我主要写这个“答案”是为了引起更多的注意,而且我没有评论的能力。

编辑:你可以在CRS函数中设置doCheckCRSArgs = F,但当我尝试spTransform时仍然会出现错误。 source crs creation failed: generic error of unknown origin

function (projargs = NA_character_, doCheckCRSArgs = TRUE) 
{
  if (!is.na(projargs) && !nzchar(projargs)) 
    projargs <- NA_character_
  stopifnot(is.logical(doCheckCRSArgs))
  stopifnot(length(doCheckCRSArgs) == 1L)
  stopifnot(is.character(projargs))
  if (!is.na(projargs)) {
    if (length(grep("^[ ]*\\+", projargs)) == 0) 
      stop(paste("PROJ4 argument-value pairs must begin with +:", 
        projargs))
  }
  if (!is.na(projargs)) {
    if (length(grep("latlon", projargs)) != 0) 
      stop("northings must follow eastings: ", projargs)
    if (length(grep("lonlat", projargs)) != 0) {
      projargs <- sub("lon", "long", projargs)
      warning("'lonlat' changed to 'longlat': ", projargs)
    }
  }
  if (is.na(projargs)) 
    uprojargs <- projargs
  else uprojargs <- paste(unique(unlist(strsplit(projargs, 
    " "))), collapse = " ")
  if (length(grep("= ", uprojargs)) != 0) 
    stop(paste("No spaces permitted in PROJ4 argument-value pairs:", 
      uprojargs))
  if (length(grep(" [:alnum:]", uprojargs)) != 0) 
    stop(paste("PROJ4 argument-value pairs must begin with +:", 
      uprojargs))
  if (doCheckCRSArgs) {
    if (!is.na(uprojargs) && requireNamespace("rgdal", quietly = TRUE)) {
      res <- rgdal::checkCRSArgs(uprojargs)
      ********if (!res[[1]])*********
        stop(res[[2]])
      uprojargs <- res[[2]]
    }
  }
  res <- new("CRS", projargs = uprojargs)
  res
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接