RQGIS调用saga插件错误 - crs无效,没有系统列表,错误:22

时间:2017-10-20 10:56:48

标签: python r gdal qgis

我想使用RQGIS包请求错误帮助。 显然,我有CRS的问题。 尝试通过RQGIS命令运行简单的saga工具,它在生成结果之前结束并出现错误:

错误:crs:+ init = epsg:4326 + proj = longlat + datum = WGS84 + no_defs + ellps = WGS84 + towgs84 = 0,0,0,原因:没有系统列表,错误:22 < / strong>

My code: 
# reading table of coordinates
sites<-read.table("DATA_sites_coordinates.txt",header = T, sep = "\t",stringsAsFactors = F,dec = ".")
# converting to spatial class
coordinates(sites_spac) <- ~longitude+latitude
#projecting to WGS 84 (I know form author the data has this projection)
proj4string(sites_spac) <- CRS("+init=epsg:4326")
# reading tif of chelsa climatic data
cehlsa<-raster("C:/Users/manma/Desktop/chelsa/CHELSA_bio_1.tif")
# projecting raster (I know from metadata the projection is WGS 84)
proj4string(cehlsa) <- CRS("+init=epsg:4326")
# setting parameters of saga add grid values to point
args <- get_args_man(alg = "saga:addgridvaluestopoints")
# defining the parameters
args$GRIDS <-cehlsa
args$SHAPES<-sites_spac
args$RESULT<-"C:/Users/manma/Dropbox/MODELING/rcode/extracted"
args$INTERPOL<-3
extr <- run_qgis(alg = "saga:addgridvaluestopoints",params = args, load_output = TRUE)

来自R的跟踪结果:

15: stop(paste0("invalid crs: ", x, ", reason: ", is_valid$result), 
    call. = FALSE)
14: make_crs(value)
13: `st_crs<-.sfc`(`*tmp*`, value = "+init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0")
12: `st_crs<-`(`*tmp*`, value = "+init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0")
11: (function (..., crs = NA_crs_, precision = 0) 
{
    lst = list(...)
    if (length(lst) == 1 && is.list(lst[[1]]) && !inherits(lst[[1]], 
        "sfg") && (length(lst[[1]]) == 0 || inherits(lst[[1]][[1]], 
        "sfg") || is.null(lst[[1]][[1]]))) 
        lst = lst[[1]]
    stopifnot(is.numeric(crs) || is.character(crs) || inherits(crs, 
        "crs"))
    a = attributes(lst)
    is_null = vapply(lst, is.null, TRUE)
    lst = unclass(lst)
    lst = lst[!is_null]
    attributes(lst) = a
    sfg_classes = vapply(lst, class, rep(NA_character_, 3))
    cls = if (length(lst) == 0) 
        c("sfc_GEOMETRY", "sfc")
    else {
        single = if (!is.null(attr(lst, "single_type"))) 
            attr(lst, "single_type")
        else length(unique(sfg_classes[2L, ])) == 1L
        attr(lst, "single_type") = NULL
        if (single) 
            c(paste0("sfc_", sfg_classes[2L, 1L]), "sfc")
        else c("sfc_GEOMETRY", "sfc")
    }
    if (any(is_null)) {
        ret = vector("list", length(is_null))
        ret[!is_null] = lst
        ret[is_null] = list(typed_empty(cls))
        attributes(ret) = attributes(lst)
        lst = ret
    }
    class(lst) = cls
    if (!missing(precision) || is.null(attr(lst, "precision"))) 
        attr(lst, "precision") = precision
    attr(lst, "bbox") = compute_bbox(lst)
    if (is.na(crs) && !is.null(attr(lst, "crs"))) 
        crs = attr(lst, "crs")
    st_crs(lst) = crs
    if (inherits(lst, "sfc_GEOMETRY")) 
        attr(lst, "classes") = vapply(lst, class, rep(NA_character_, 
            3))[2L, ]
    if (is.null(attr(lst, "n_empty")) || any(is_null)) {
        attr(lst, "n_empty") = sum(vapply(lst, sfg_is_empty, 
            TRUE))
        if (length(u <- unique(sfg_classes[1L, ])) > 1) 
            stop(paste("found multiple dimensions:", paste(u, 
                collapse = " ")))
    }
    lst
})(c(-121.916389, 52.315), c(-120.298056, 50.938889), c(-122.607594, 
54.368902), c(-126.309444, 54.611389), c(-120.3525, 50.894167
), c(-120.424167, 50.851111), c(-112.552224, 37.113448), c(-70.524528, 
-41.0335), c(-115.79572, 33.754262), c(10.498595, 32.98347), 
    c(-1.999301, 34.309672), c(-115.79572, 33.754262), c(116.015833, 
    48.609778), c(119.177417, -29.311377), c(-101.248559, 23.204697
    ), c(-111.696845, 37.086092), c(-112.02167, 37.507278), c(116.786556, 
    49.031528), c(-112.02167, 37.507278), c(-110.516686, 38.003108
10: do.call(st_sfc, c(lst, crs = x@proj4string@projargs))
9: st_as_sfc.SpatialPoints(sp::geometry(x), ...)
8: st_as_sfc(sp::geometry(x), ...)
7: st_as_sf.Spatial(params[[i]])
6: st_as_sf(params[[i]])
5: FUN(X[[i]], ...)
4: lapply(seq_along(params), function(i) {
   tmp <- class(params[[i]])
   if (tmp == "list" && type_name[i] == "multipleinput") {
       names(params[[i]]) <- paste0("inp", 1:length(params[[i]]))
       out <- save_spatial_objects(params = params[[i]])
       return(paste(unlist(out), collapse = ";"))
   }
   if (any(tmp %in% c("sfc_GEOMETRY", "sfc_GEOMETRYCOLLECTION"))) {
       stop("RQGIS does not support GEOMETRY or GEOMETRYCOLLECTION classes")
   }
   if (any(grepl("^Spatial(Points|Lines|Polygons)DataFrame$", 
       tmp)) | any(tmp %in% c("sf", "sfc", "sfg"))) {
       params[[i]] <- st_as_sf(params[[i]])
       fname <- file.path(tempdir(), paste0(names(params)[i], 
           ".shp"))
       cap <- capture.output({
           suppressWarnings(test <- try(write_sf(params[[i]], 
               fname, quiet = TRUE), silent = TRUE))
       })
       if (inherits(test, "try-error")) {
           while (tolower(basename(fname)) %in% tolower(dir(tempdir()))) {
               fname <- paste0(gsub(".shp", "", fname), 1, ".shp")
           }
           write_sf(params[[i]], fname, quiet = TRUE)
       }
       fname
   }
   else if (tmp == "RasterLayer") {
       fname <- file.path(tempdir(), paste0(names(params)[[i]], 
           ".tif"))
       suppressWarnings(test <- try(writeRaster(params[[i]], 
           filename = fname, format = "GTiff", prj = TRUE, overwrite = TRUE), 
           silent = TRUE))
       if (inherits(test, "try-error")) {
           while (tolower(basename(fname)) %in% tolower(dir(tempdir()))) {
               fname <- paste0(gsub(".tif", "", fname), 1, ".tif")
           }
           writeRaster(params[[i]], filename = fname, format = "GTiff", 
               prj = TRUE, overwrite = TRUE)
       }
       fname
   }
   else {
       params[[i]]
   }
   })
3: save_spatial_objects(params = params, type_name = out$type_name)
2: pass_args(alg, ..., params = params, qgis_env = qgis_env)
1: run_qgis(alg = "saga:addgridvaluestopoints", params = args, load_output =TRUE)

Here我的会话ifo .txt

1 个答案:

答案 0 :(得分:0)

实际上这不是 RQGIS 问题,但在尝试保存坐标时出现问题。由于您向run_qgis()提供了sp个对象,因此会将其转换为sf - 对象,然后尝试使用sf::write_sf()进行保存。

您在sites中读取数据,但在下一行中使用sites_spac,即之前未定义的对象。假设这不是您的问题,并且您的投影规范是正确的,请尝试:

library("RQGIS")
library("sf")
sites = read.table("DATA_sites_coordinates.txt", header = TRUE, 
                   sep = "\t", stringsAsFactors = FALSE, dec = ".")
sites_spac = st_as_sf(sites, coords = c("longitude", "latitude"))
sites_spac = st_set_crs(sites_spac, 4326)
write_sf(sites_spac, file.path(tempdir(), "points.shp"))
# in case, write_sf worked as expected, you can now run RQGIS
extr = run_qgis(alg = "saga:addgridvaluestopoints",
                GRIDS = "C:/Users/manma/Desktop/chelsa/CHELSA_bio_1.tif",
                SHAPES = file.path(tempdir(), "points.shp"),
                # note that you have to specify an output shapefile, not just a directory as you did in your example code
                RESULT = file.path(tempdir(), "out.shp"),
                INTERPOL = 3,
                load_output = TRUE)

另外两条建议:(1)如果您的文件已存储在磁盘上,请使用run_qgis()中的路径作为输入参数。如果将其加载到R run_qgis()中,则只会将其保存回磁盘,效率很低。 (2)可重复的例子会有很大帮助。否则我有点猜测你的问题是什么。

最后,如果您的文件不是那么大,那么还有raster::extract()命令用于从栅格对象中提取值。