I would like to ask for help with an error using RQGIS package. Obviously, I have a problem with CRS. Trying to run simple saga tool through RQGIS command it ends just before producing of result with an error:
Error: invalid crs: +init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0, reason: no system list, errno: 22
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)
And result of trackback from 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 my session ifo .txt
In fact this is not a RQGIS problem but a problem when trying to save your coordinates. Since you provide run_qgis()
with an sp
object, it converts it into an sf
-object, and subsequently tries to save it with sf::write_sf()
.
You read your data in as sites
but in the next line you use sites_spac
, ie, an object that was not previously defined. Assuming that this isn't your problem and that your projection specification is correct, try:
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)
Two more recommendations: (1) If your file is already stored on disk, use the path as input argument in run_qgis()
. If you load it into R run_qgis()
will only save it back to disk which is inefficient. (2) A reproducible example would help a lot. Otherwise I am somewhat guessing what your problem is.
Finally, if your files are not that big, there is also the raster::extract()
command for extracting values to points from raster objects.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.