【发布时间】:2022-01-14 18:06:51
【问题描述】:
我是 Star 新手,所以希望这是一个简单的答案,只是我无法正确理解 Star 的工作流程。
R 版本:4.1.1
星星版本:0.5-5
library(stars)
library(starsdata) #install.packages("starsdata", repos = "http://gis-bigdata.uni-muenster.de", type = "source")
#Create the rasters to read in as proxy
granule = system.file("sentinel/S2A_MSIL1C_20180220T105051_N0206_R051_T32ULE_20180221T134037.zip", package = "starsdata")
s2 = paste0("SENTINEL2_L1C:/vsizip/", granule, "/S2A_MSIL1C_20180220T105051_N0206_R051_T32ULE_20180221T134037.SAFE/MTD_MSIL1C.xml:10m:EPSG_32632")
r1<-read_stars(s2,,RasterIO=list(bands=1),proxy=T)
r2<-read_stars(s2,,RasterIO=list(bands=2),proxy=T)
r3<-read_stars(s2,,RasterIO=list(bands=3),proxy=T)
write_stars(r1,dsn="r1.tif")
write_stars(r2,dsn="r2.tif")
write_stars(r3,dsn="r3.tif")
然后我从我的环境中清除对象并重新启动 R 会话。
#I clear all the objects and restart my R session here.
library(stars)
foo<-read_stars(c("r1.tif","r2.tif","r3.tif"),proxy=T)
r1<- foo[1]*0
r1[foo[1] > 4000 & foo[2] < 3000] <- 1
r1[foo[1] > 4000 & foo[2] >= 3000 & foo[2] <= 8000] <- 2
r1[foo[1] > 4000 & foo[2] > 8000 & foo[3] < 2000] <- 4
r1[foo[1] > 4000 & foo[2] > 8000 & foo[3] >= 2000] <- 2
# plot(r1) #this works just fine if you run it
#why doesn't the below work?
write_stars(r1,dsn="out.tif")
尝试写出文件会导致以下错误:
Error in st_as_stars.list(mapply(fun, x, i, value = value, SIMPLIFY = FALSE), :
!is.null(dx) is not TRUE
如果我不是写出文件,而是绘制栅格,它就可以正常工作/按预期工作。 也许问题只是我不明白这个答案也适用于我: How to reassign cell/pixel values in R stars objects
【问题讨论】:
-
嗨@SIBeckers,如果没有minimal reproducible example,总是很难有效地提供帮助。也就是说,我想您应该使用以下代码解决您的问题:
write_stars(r1, dsn="out.tif") -
嘿@lovalery,明白了。将尝试使用一些有意义的代理栅格作为示例进行更新,但绝对不仅仅是使用文件名参数而不是 dsn。同样的错误仍然存在。
-
嗨@SIBeckers。感谢您的反馈意见。因此,问题可能与您的
r1、r2和/或r3文件有关,因为您的其余代码都很好:在我这边,我设法使用提供的测试图像保存了您的代码结果库(即L7_ETMS.tif)和我在之前的评论中给你的代码行。 -
嗨@lovalery,我认为这个例子现在可以重现,我得到的错误与我在自己的数据中看到的一样。